How can we access to an object of a form from another form?
-
If we have 2 form (Form1 & Form2) whitin a project and we added an object (for example textBox1) to Form1, how can we access to textBox1 from Form2. i have done this in form2 by these codes { Form1 fr = new Form1(); . . . } but fr doesn't show textBox1 durring writing codes. i suppose fr.textBox1 must be correct but it is not. please tell ne how can i do that.
-
If we have 2 form (Form1 & Form2) whitin a project and we added an object (for example textBox1) to Form1, how can we access to textBox1 from Form2. i have done this in form2 by these codes { Form1 fr = new Form1(); . . . } but fr doesn't show textBox1 durring writing codes. i suppose fr.textBox1 must be correct but it is not. please tell ne how can i do that.
The simplest but wrongest way would be to make the TextBox public. A better way is to make a public property that allows the other form to get and/or set the Text property of the TextBox. There are other ways that may be more appropriate to your situation, but we would need to know more about what you are trying to do. However you do it, you may have to protect against cross-thread access problems.
-
If we have 2 form (Form1 & Form2) whitin a project and we added an object (for example textBox1) to Form1, how can we access to textBox1 from Form2. i have done this in form2 by these codes { Form1 fr = new Form1(); . . . } but fr doesn't show textBox1 durring writing codes. i suppose fr.textBox1 must be correct but it is not. please tell ne how can i do that.
You would have to change the access modifier for the textbox.
Why is common sense not common? Never argue with an idiot. They will drag you down to their level where they are an expert. Sometimes it takes a lot of work to be lazy Individuality is fine, as long as we do it together - F. Burns
-
You would have to change the access modifier for the textbox.
Why is common sense not common? Never argue with an idiot. They will drag you down to their level where they are an expert. Sometimes it takes a lot of work to be lazy Individuality is fine, as long as we do it together - F. Burns
-
The simplest but wrongest way would be to make the TextBox public. A better way is to make a public property that allows the other form to get and/or set the Text property of the TextBox. There are other ways that may be more appropriate to your situation, but we would need to know more about what you are trying to do. However you do it, you may have to protect against cross-thread access problems.
-
If we have 2 form (Form1 & Form2) whitin a project and we added an object (for example textBox1) to Form1, how can we access to textBox1 from Form2. i have done this in form2 by these codes { Form1 fr = new Form1(); . . . } but fr doesn't show textBox1 durring writing codes. i suppose fr.textBox1 must be correct but it is not. please tell ne how can i do that.
Pass value between forms using events[^] explains the correct way to do such things. What you had, creating yet another Form1, is not going to work, as you are looking at one instance of Form1 and programmatically changing another unrelated instance. Making Controls public would work but is hardly acceptable, as now "the whole world" could modify, move, resize, etc. said Control. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
If we have 2 form (Form1 & Form2) whitin a project and we added an object (for example textBox1) to Form1, how can we access to textBox1 from Form2. i have done this in form2 by these codes { Form1 fr = new Form1(); . . . } but fr doesn't show textBox1 durring writing codes. i suppose fr.textBox1 must be correct but it is not. please tell ne how can i do that.
-
If we have 2 form (Form1 & Form2) whitin a project and we added an object (for example textBox1) to Form1, how can we access to textBox1 from Form2. i have done this in form2 by these codes { Form1 fr = new Form1(); . . . } but fr doesn't show textBox1 durring writing codes. i suppose fr.textBox1 must be correct but it is not. please tell ne how can i do that.