Click here to Skip to main content
15,893,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I pass data between two usercontrols in a winForm?

For example if I have two usercontrols named ucA and ucB.
ucA contains one textbox and one button.
ucB contains a label, and when clicking the button the text in the textbox should appear in the label in ucB.

Thanks in advance from a C# newbee. :)
Posted

ucA should raise an event that it's host control (Form?) subscribes to. ucB should have a public property who's setter sets the Label's Text.
The host control when instanciating ucB should maintain a private reference to it, when it handles the event raised in ucA it can then set the property of ucB.

Have a look at my Events Made Simple[^] article.

Edit: What the heck, I'm in a good mood this morning - here's a full working (WinForms) example.
using System.Drawing;
using System.Windows.Forms;

namespace UserControlInteraction
{
    public partial class Form1 : Form
    {
        private UCA ucA;
        private UCB ucB;

        public Form1()
        {
            InitializeComponent();
            ucA = new UCA();
            ucA.Size = new Size(150, 50);
            ucB = new UCB();
            ucB.Location = new Point(0, 50);
            Controls.AddRange(new Control[] { ucA, ucB });
            ucA.UpdateText += new EventHandler<TextEventArgs>(ucA_UpdateText);
        }

        void ucA_UpdateText(object sender, TextEventArgs e)
        {
            ucB.LabelText = e.Text;
        }
    }

    public class UCA : UserControl
    {
        public event EventHandler<TextEventArgs> UpdateText;

        private TextBox textBox;
        private Button button;

        public UCA()
        {
            textBox = new TextBox();
            textBox.Location = new Point(0, 0);
            button = new Button();
            button.Location = new Point(0, 24);
            button.Text = "&Update";
            button.Click += new EventHandler(button_Click);
            Controls.AddRange(new Control[] { textBox, button });
        }

        private void button_Click(object sender, EventArgs e)
        {
            OnUpdateText(new TextEventArgs(textBox.Text));
        }

        protected virtual void OnUpdateText(TextEventArgs e)
        {
            EventHandler<TextEventArgs> eh = UpdateText;
            if (eh != null)
                eh(this, e);
        }
    }

    public class UCB : UserControl
    {
        private Label label;

        public UCB()
        {
            label = new Label();
            label.Size = new Size(100, 13);
            Controls.Add(label);
        }

        public string LabelText
        {
            get { return label.Text; }
            set { label.Text = value; }
        }
    }

    public class TextEventArgs : EventArgs
    {
        private string text;

        public TextEventArgs(string text)
        {
            this.text = text;
        }

        public string Text
        {
            get { return text; }
        }
    }
}
 
Share this answer
 
v3
That is similar to what I have tried, but I had the event in the usercontrols codebehind.
I will test your solution and get back later, but I think that this will solve my problem.

And big thanks for the code, much easier to understand all technical terms when I can look at your code. :thumbsup:

Edit.
Works great, and thanks to your code I even think that I understand why it works. :-D
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900