0

I want to pass values between two Forms (c# both in active states). How can I do it?

I have two forms: Form1 and Form2.

Form1 contains a rich text box and a button. When I click on that button, Form2 should open and a text in rich text box should be sent to a textbox in Form2 and Form1 should remain opened as well being on back of Form2.

Form2 contains a text box and a button where user edits a text in textbox and when user clicks on a button then the edited text should be sent back to the rich text box in Form1 and the Form2 should close/ stay opened and Form1 should highlight the updated text in rich text box.

How can i do it? Can somebody help me to do this with a simple example?

  • Show us what you tried so far. – Microsoft DN Sep 18 '13 at 08:52
  • I have tried nothing as of yet because i don't know how to do that. Ijust want to achieve that goal as i am new to c#! Please help me! – Shahid Sultan Minhas Sep 18 '13 at 08:54
  • Also why are you asking the same question that is already asked. the exact word to word copy of your question is [HERE](http://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form-in-c-sharp-winforms-application) – Microsoft DN Sep 18 '13 at 08:58
  • Yeah i went through the question But that's not exactly what i am looking for nor the question mentioned earlier fulfills my requirments. By the way, if you have solution then don't spend time in fetching the matches rather provide answer. – Shahid Sultan Minhas Sep 18 '13 at 09:18

2 Answers2

0

Please changes the field names as you required. Also following code will update the rich text box value concurrently when textfield value in form2 is changed. You may want to do minor changes to trigger it on button change event.

Add the following method to your From1

private void SetChildFromValueToParent(object obj, EventArgs args)
    {
        //Read the child form's control value and set it to parent form field
        txtBox.Text = ((TextBox)(obj)).Value.ToString();
    }

Add the following logic to your Form1 button click which opens the Form2

private void button1_Click(object sender, EventArgs e)
{
    ChildForm childForm = new ChildForm();

    //Find the textbox control in the child form
    Control[] controls = childForm.Controls.Find("textBox", true);

    if (null != controls[0])
    {
        //Bind the method in the parent form to child form text control's TextChanged event
        controls[0].TextChanged += new System.EventHandler(SetChildFromValueToParent);
    }

    childForm.ShowDialog();
}

EDIT - Getting value on Button Click

   private void SetChildFromValueToParent(object obj, EventArgs args)
        {
             //Read the child form's control value and set it to parent form field
             Form2 from2 = new Form2();
             string richTextBox.Text =  ((TextBox)form2.Controls["textBox1"]).Text;            
        }
Kurubaran
  • 8,696
  • 5
  • 43
  • 65
  • Thanks Access not Denied but Granted! I will let you know after doing it! Thanks! – Shahid Sultan Minhas Sep 18 '13 at 09:41
  • Thanks i did it and working fine but i want to trigger this event with button control. so is the procedure same for the button control as what i feel that if i use a button control to trigger this event then it would look like this: Control[] controls = childForm.Controls.Find("button1", true); controls[0].onClick += new System.EventHandler(SetChildFromValueToParent); Is this right? – Shahid Sultan Minhas Sep 18 '13 at 10:09
0

In some ways I prefer the first answer, but here's an approach which will help you understand the relationship between the two forms. Note: To get this to work, both richtextboxes will need to be changed from private to internal:

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

    private void btnEditOnForm1_Click(object sender, EventArgs e)
    {
        var form2 = new Form2(richTextBoxOnForm1);
        form2.richTextBoxOnForm2.Text = richTextBoxOnForm1.Text;
        form2.ShowDialog(this);
    }
}

and

public partial class Form2 : Form
{
    private readonly RichTextBox _rtb;

    public Form2(RichTextBox pRTB)
    {
        InitializeComponent();
        _rtb = pRTB;
    }

    private void btnOkOnForm2_Click(object sender, EventArgs e)
    {
        _rtb.Text = richTextBoxOnForm2.Text;
        this.Close();
    }

    private void btnCancelOnForm2_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}

Note how Form2 needs a reference back to the richtextbox on Form1 in order to update it, so you need to tweak the contructor to accept that reference.

Alternatively, you could create a suitable function in Form1 to update the richtextbox there, and call it using

    private void btnOkOnForm2_Click(object sender, EventArgs e)
    {
        ((Form1)this.Owner).UpdateRichTextBoxOnForm1(richTextBoxOnForm2.Text);
        this.Close();
    }

making use of the fact that you included the sender ('this') in the ShowDialog call

        form2.ShowDialog(this);
n4m16
  • 121
  • 8
  • Thanks Sir! It worked! But can you tell me that if we look at the second answer given below by "AccessDenied" then, can you tell me that if i want to trigger this event with button control. so is the procedure same for the button control as what i feel that if i use a button control to trigger this event then it would look like this: Control[] controls = childForm.Controls.Find("button1", true); controls[0].onClick += new System.EventHandler(SetChildFromValueToParent); Is this right? – Shahid Sultan Minhas Sep 18 '13 at 10:39