1

In (Form1) i have a setting button, when i click on it a new form ( Form2 ) is shown, using these lines of code :

private void b7_Click(object sender, EventArgs e)
{
    Form3 frm = new Form3();
    frm.Show();

}

In form3, i have 6 text boxes, and two button, Save and Cancel.

enter image description here

What i'm trying to do is to provide this form to the user so he types the neccessary data into the form, then he click the Save Settings button. In Form1, i want to access to these text boxes to get their current values ( when user clicked save settings ). I tried to add a Form4 and named it ( MiddleForm), i added 6 text boxes to it, and in Form3 (The form in the image above) i wrote these line :

private void button2_Click(object sender, EventArgs e)
{
    MiddleForm mf = new MiddleForm();
    mf.textBox1.Text = keywrd1.Text;
    mf.textBox2.Text = keywrd2.Text;
    mf.textBox3.Text = keywrd3.Text;
    mf.textBox4.Text = keywrd4.Text;
    mf.textBox5.Text = keywrd5.Text;

    mf.textBox1.Text = thelink.Text;

    Close();


}

then i tried to access the values passed to the MiddleForm from Form1 (The form where i need to use the textboxes values), in Form1, i wrote these lines (for debug purposes only)

MiddleForm mf = new MiddleForm();

MessageBox.Show(mf.textBox1.Text); // to see whether there is something 

Unfortunately, it seems that nothing is passed to mf.TextBox1

enter image description here

How can i call the current values (Saved using save settings button) of Form3 From Form1 in order to use them in the rest of code.

Any help please on getting this to work ?

Mark Hall
  • 53,938
  • 9
  • 94
  • 111
Rafik Bari
  • 4,867
  • 18
  • 73
  • 123
  • 4
    Wow ... please name the UI elements something meaningful. My head is spinning trying to follow all the "form3"s, "button18_click"s, etc. – McGarnagle Apr 14 '12 at 23:07
  • possible duplicate of [How to copy a textbox value from Form1 to Form2?](http://stackoverflow.com/questions/3384961/how-to-copy-a-textbox-value-from-form1-to-form2) – H H Apr 14 '12 at 23:21

5 Answers5

5

Make 6 public properties in your Form3 like that:

public partial class Form3 : Form
{
    public string Value1
    {
        get { return this.keywrd1.Text; }
    }

    public string Value2
    {
        get { return this.keywrd2.Text; }
    }

    ...
}

After your Form3 is closed (but before disposed) you can access text values via properties. As pointed in another answer, use ShowDialog instead of Show and close Form3 inside it's own code.

private void b7_Click(object sender, EventArgs e)
{
    Form3 frm = new Form3();
    frm.ShowDialog();
    string value1 = frm.Value1;
    ...
}
Maciej
  • 7,871
  • 1
  • 31
  • 36
3

You need to do this:

var form = Form.ActiveForm as Form3;
String myText = form.txtBoxName.Text;
duncanportelli
  • 3,161
  • 8
  • 38
  • 59
3

You should make a public field that provides the values you want to get out of the form. If you go to the source of Form1, you should add in something like this:

public string TextValue1 {
    get {return TextBox1.Text;}
}

Now, you can use the Form1.TextBox1 to retrieve a string value from your Textbox.

Jeff Halverson
  • 253
  • 1
  • 8
3

You could try using ShowDialog it will create your Form as a Model Dialog box, you can then check the DialogResult to know wether the data was saved or the Form was canceled.

i.e.

private void button2_Click(object sender, EventArgs e)
{
    Form3 frm = new Form3();
    if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        savedSettings = frm.getTextBoxValues();
    }
}

Form3

public partial class Form3 : Form
{
    string[] textValues = new string[6];

    public Form3()
    {
        InitializeComponent();
    }
    public string[] getTextBoxValues()
    {
        return textValues;
    }

    private void saveSettings_Click(object sender, EventArgs e)
    {
        DialogResult = System.Windows.Forms.DialogResult.OK;
        textValues[0] = textBox1.Text;
        textValues[1] = textBox2.Text;
        textValues[2] = textBox3.Text;
        textValues[3] = textBox4.Text;
        textValues[4] = textBox5.Text;
        textValues[5] = textBox6.Text;
        this.Close();
    }

    private void cancelSettings_Click(object sender, EventArgs e)
    {
        DialogResult = System.Windows.Forms.DialogResult.Cancel;
        this.Close();
    }
}
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
3

Make public properties in Form3 like this

public string[] Keys
{
    get
    {
        return new string[] { tbKey1.Text, tbKey2.Text, tbKey3.Text,
                              tbKey4.Text, tbKey5.Text};
    }
}

public string Link { get { return tbLink.Text; } }

From Form1 you can access these properties like this

Form3 frm = new Form3();
if (frm.ShowDialog() == DialogResult.OK) {
    string[] keys = frm.Keys; 
    string link = frm.Link; 
}

Note: It is important that you use ShowDialog and not Show, since Show does not wait for the other form to close. Also, when "Save settings" is clicked in Form3, set the dialog result

DialogResult = DialogResult.OK;
Close();

so that you can check it in Form1 as shown above.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188