Home>

I want to exchange data between two UserControls using c #.
Specifically, after entering data into the text box with UserControl3, pressing the UserControl3 button will display UserControl4 and hide UserControl3. At this time, I want to display the data entered in UserControl3 on the label of UserControl4.

Try it:
I tried the following program, but nothing is displayed on the label.
First of all, there are label2 and label3 as the initial values ​​as shown below, but that will disappear. I think that the actual substitution is an empty string.

// Form1.cs (part)
ctr1 = new UserControl1 ();
ctr2 = new UserControl2 ();
ctr3 = new UserControl3 ();
ctr4 = new UserControl4 ();
// UserControl3.cs (part)
private void button1_Click (object sender, EventArgs e)
        {
            Form1.ctr4.Visible = true;
            Form1.ctr3.Visible = false;
            Form1.ctr4.label2.Text = textBox1.Text;
            Form1.ctr4.label3.Text = textBox2.Text;
        }


UerControl4.cs (Design)

c#
  • Answer # 1

    First of all, I wonder if each user control is designed to access other user controls.
    I think each control should raise an event when it wants to do something, receive the event in the form where the user control is used, and take action on other controls.

    usercontrol3

    public event EventHandler OKClick;
    public string InputString1
    {
        get
        {
            return textBox1.Text;
        }
    }
    public string InputString2
    {
        get
        {
            return textBox2.Text;
        }
    }
    private void button1_Click (object sender, EventArgs e)
    {
        if (OKClick! = null)
        {
            OKClick (this, e);
        }
    }


    usercontrol4

    public string Message1
    {
        set
        {
            label2.Text = value;
        }
    }
    public string Message2
    {
        set
        {
            label3.Text = value;
        }
    }

    Form1

    private void usercontrol3_OKClick (object sender, EventArgs e)
    {
        ctr3.Visible = false;
        ctr4.Visible = true;
        ctr4.Message1 = ctr3.InputString1;
        ctr4.Message2 = ctr3.InputString2;
    }


  • Answer # 2

    I feel like I can't judge the current problems from the sources currently posted.
    I don't care about that for the time being.

    In any case, the method of "Implementing an event that directly manipulates another UserControl on the same Form in one UserControl" seems to be an implementation out of general theory.
    (What does it mean to implement such tightly coupled items as individual UserControls?)
    It looks to me in the future source code. It may be a little too early for humanity (too much to say!).

    In general, the interaction between elements in a Form will be resolved as an implementation of the Form.
    There are several ways to do this, but to give an example, it seems common to write the sample button1_Click event to a Form instead of a UserControl.
    I feel like it will help solve the problem naturally (maybe).