accessing the value of control between seperate Forms
-
Hello , In this example I will pass Textbox1.text in Form1 to Form2 TextBox1 Form1 Code :
using System;
using System.Windows.Forms;namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form2 frm2;
public Form1()
{
InitializeComponent();
}private void button1\_Click(object sender, EventArgs e) { frm2 = new Form2(); frm2.MyVar = textBox1.Text; frm2.Show(); } private void textBox1\_TextChanged(object sender, EventArgs e) { if (frm2 != null) { frm2.MyVar = textBox1.Text; } } }
}
Form2 Code
using System;
using System.Windows.Forms;namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}public string MyVar { set { textBox1.Text = value; } } }
}
Hello and thanks to replied me. this is quite correct but suppose u want to send a plenty amount of textBox and and dataGridview and.... from a Form to another, can u tell me how can we have direct access to controls of that form. i am expert in Delphi and there is no such a problems there. thanks agian.
-
sorry, this is the second time that i ask this questio but i didn't get an explicit answer. anyway If we have 2 form (Form1 & Form2) whitin a project and we added a public object (for example textBox1) to Form1, how can we access to value of textBox1.text from Form2.i have done this in form2 by these codes { Form1 fr = new Form1(); . . . } but fr.textBox1.text doesn't show the actual value. it just show the prdefined value of it. please explicitly tell me how can i solve this.
The cleanest way to do this is by the use of delegates[^]. You create a public delegate in Form1 which Form2 can subscribe to, then when the textbox content changes, Form2 will get notified of the change and can copy the content into its own variable.
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
The cleanest way to do this is by the use of delegates[^]. You create a public delegate in Form1 which Form2 can subscribe to, then when the textbox content changes, Form2 will get notified of the change and can copy the content into its own variable.
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
Try this:
public class Form1 : Form
{
public delegate void StringTransportingEvent(string value);
public event StringTransportingEvent VariableChanged;override void OnLoad() { Form2 secondForm = new Form2(this); secondForm.Show(); } textBox1\_TextChanged(object sender, EventArgs e) { EventHandler eh = VariableChanged; if( eh != null) { eh(textBox1.Text); } }
}
public class Fomr2 : Form
{
public Form2(Form1 eventRaiser)
{
eventRaiser.VariableChanged += Form1_VariableChanged;
}private void Form1\_VariableChanged(string value) { this.TextBox1.Text = value; }
}
It creates a custom
delegate
that can transport astring
. Form1 raises a custom event "VariableChanged
" that Form2 subscribes to.Ciao, luker
-
If you go to the link I posted you will see a very comprehensive tutorial on the subject. While it does not answer your question directly, it is a good learning tool to help in developing your skills.
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
sorry, this is the second time that i ask this questio but i didn't get an explicit answer. anyway If we have 2 form (Form1 & Form2) whitin a project and we added a public object (for example textBox1) to Form1, how can we access to value of textBox1.text from Form2.i have done this in form2 by these codes { Form1 fr = new Form1(); . . . } but fr.textBox1.text doesn't show the actual value. it just show the prdefined value of it. please explicitly tell me how can i solve this.
faraz34 wrote:
but i didn't get an explicit answer
Yes you did. What you didn't get was an exact code sample you could copy'n'adapt to your code. Luc's link was very explicit and explained the concepts quite clearly. By doing this you are also making your forms completely dependant on one another. You now cannot change one form without affecting the other. This means you have a very bad data model. You shouldn't be accessing controls on one form from another. You should be putting the data from that form in a container object so that it can be passed back to or retrieved by the calling code.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Try this:
public class Form1 : Form
{
public delegate void StringTransportingEvent(string value);
public event StringTransportingEvent VariableChanged;override void OnLoad() { Form2 secondForm = new Form2(this); secondForm.Show(); } textBox1\_TextChanged(object sender, EventArgs e) { EventHandler eh = VariableChanged; if( eh != null) { eh(textBox1.Text); } }
}
public class Fomr2 : Form
{
public Form2(Form1 eventRaiser)
{
eventRaiser.VariableChanged += Form1_VariableChanged;
}private void Form1\_VariableChanged(string value) { this.TextBox1.Text = value; }
}
It creates a custom
delegate
that can transport astring
. Form1 raises a custom event "VariableChanged
" that Form2 subscribes to.Ciao, luker
-
faraz34 wrote:
but i didn't get an explicit answer
Yes you did. What you didn't get was an exact code sample you could copy'n'adapt to your code. Luc's link was very explicit and explained the concepts quite clearly. By doing this you are also making your forms completely dependant on one another. You now cannot change one form without affecting the other. This means you have a very bad data model. You shouldn't be accessing controls on one form from another. You should be putting the data from that form in a container object so that it can be passed back to or retrieved by the calling code.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
sorry, this is the second time that i ask this questio but i didn't get an explicit answer. anyway If we have 2 form (Form1 & Form2) whitin a project and we added a public object (for example textBox1) to Form1, how can we access to value of textBox1.text from Form2.i have done this in form2 by these codes { Form1 fr = new Form1(); . . . } but fr.textBox1.text doesn't show the actual value. it just show the prdefined value of it. please explicitly tell me how can i solve this.
Your original post contains some really good answers that can help you achieve what you are trying to accomplish. But your problem is that you expect too much spoon feeding and if this is the case, you're not going to stop littering the forum by asking the same question again and again until someone writes the entire code for you for free. People here try to teach you how to fish, but you're expecting the fish itself everytime. Pathetic.
-
Unless it's your wife, you don't call anyone in the Western world "dear".
A guide to posting questions on CodeProject[^]
Dave Kreskowiak