V
V
Viktor Familyevich2017-01-10 13:31:22
Programming
Viktor Familyevich, 2017-01-10 13:31:22

How to pass values ​​between forms?

Good afternoon. There are two forms

Form1 with value1 variable
Form2 with value2 variable

The problem is that value1 is not set immediately, but during code execution, and when declaring an instance of Form1 in the constructor for Form2, an error is generated.
How to pass value1 to Form2 and value2 to form1?
As I understand it, the second form is created at startup earlier than the first, and therefore the class declaration does not work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
Howard Roark, 2017-01-10
@Wintego

Use the "parent" property

/* в родительской форме */
Form2 f = new Form2();
f.Owner = this;
f.ShowDialog();

/* в дочерней форме */
Form1 main = this.Owner as Form1;
if(main != null)
{
    string s = main.textBox1.Text;
    main.textBox1.Text = "OK";
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question