naouf
naouf

Reputation: 637

How do I pass text from child to main form via a user control?

I am new to c#. I have the following in my project in windows forms:

Form1 with button and textbox.

User control with a buttton.

Form2 with button and textBox.

Screenshot

As shown in the screenshot: In form1, I click "Show User Control1" User Control1 pops up. Then in User Control1 I click Show Form2 form2 pops up.

In Form2 I enter values in textBox and when click "Send to textbox in form1" I want this text to be inserted into the textbox in Form1.

My question is: How can I send text from form2 to textbox in form1 via user control1? I just need to know some steps to follow or some code if it is possible to achieve this.

Please help me. Thank you

Form1:

public partial class Form1 : Form
{

    UserControl1 UC1 = new UserControl1();

    public Form1()
    {
        InitializeComponent();
    }


    private void button1_Click(object sender, EventArgs e)
    {
        Controls.Add(UC1);  //add a userControl
        UC1.Visible = true;
    }
}

User Control1:

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2();
        frm2.Show();
    }
}

Form2:

 public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
          // I want to send text to form1 when this button is clicked
    }
}

Upvotes: 0

Views: 199

Answers (2)

Tony Wu
Tony Wu

Reputation: 1107

You can do it by trigger event and event handler.

In Form2,

public delegate void SendTextF2(string YourStringFromTextBox);
public partial class Form2 : Form
{
    public event SendTextF2 UISendTextHandlerF2;
    public Form2(TextBox s)
    {/*unchange*/}

    private void button1_Click(object sender, EventArgs e)
    {
        if(UISendTextHandlerF2!=null)
            UISendTextHandlerF2(textBox1.Text);
    }
}

In UserControl1,

//New
public delegate void SendTextUC(string YourStringInTextBox);
public partial class UserControl1 : UserControl
{
    //New
    public event SendTextUC UISendTextHandlerUC;
    public UserControl1(TextBox r)
    {
        InitializeComponent();
        this.r = r;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2(r);
        frm2.Show();
        //Add event handler
        frm2.UISendTextHandlerF2 += SendText123;
    }

    //Event Handler for the event trigger in Form2
    void SendText123(string YourStringFromTextBox)
    {
        //Trigger Event
        if(UISendTextHandlerUC!=null)
            UISendTextHandlerUC(YourStringFromTextBox);
    }
}

In Form1,

public partial class Form1 : Form
{
    UserControl1 UC1;
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (UC1 == null)
        {
            UC1 = new UserControl1(textBox1);
            //Add event handler
            UC1.UISendTextHandlerUC += FinallyWeGetTheString;
        }
        Controls.Add(UC1);
        UC1.Visible = true;
    }

    //New
    void FinallyWeGetTheString(string YourStringFromTextBox)
    {
         textBox1.Text = YouStringFromTextBox;
    }
}

Upvotes: 1

Wajih
Wajih

Reputation: 4383

Add those line to your code:

public partial class Form1 : Form
{
    UserControl1 UC1;
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (UC1 == null)
        {
            UC1 = new UserControl1(textBox1);
        }
        Controls.Add(UC1);
        UC1.Visible = true;
    }
}

User Control:

public partial class UserControl1 : UserControl
{
    TextBox r;
    public UserControl1(TextBox r)
    {
        InitializeComponent();
        this.r = r;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2(r);
        frm2.Show();
    }
}

And Form2:

public partial class Form2 : Form
{
    TextBox s;
    public Form2(TextBox s)
    {
        InitializeComponent();
        this.s = s;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        String str = textBox1.Text;
        s.Text = str;
    }
}

Upvotes: 1

Related Questions