How can I get input from 2 forms?
-
Hi, I have two forms that both open windows, I need to get the response from a checkbox on Form2 and use it with Form1, Form2 is not a dialogue and needs to open all the time. Any ideas? Thanks.
-
Hi, I have two forms that both open windows, I need to get the response from a checkbox on Form2 and use it with Form1, Form2 is not a dialogue and needs to open all the time. Any ideas? Thanks.
Delegates.
Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.
-
Hi, I have two forms that both open windows, I need to get the response from a checkbox on Form2 and use it with Form1, Form2 is not a dialogue and needs to open all the time. Any ideas? Thanks.
As Christian said - use delegates. Simple example I've given before here[^]. Edited link!
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog)modified on Friday, August 22, 2008 3:23 AM
-
Hi, I have two forms that both open windows, I need to get the response from a checkbox on Form2 and use it with Form1, Form2 is not a dialogue and needs to open all the time. Any ideas? Thanks.
Create 1 form & 1 userform a couple of buttons etc,etc , put the userform inside the form and check this out ok?! I think you can read the code bellow. :-O ///////////////////////////////////////////// public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { userControl11.MyTextEvent += new UserControl1.MyDelegate(userControl11_TextEvent); userControl11.visibility_ev += new UserControl1.MyDelegate2(userControl11_visibility); } protected void userControl11_TextEvent(string textToSet) { textBox1.Text = textToSet; } protected void userControl11_visibility(bool visibility_b) { textBox1.Visible = visibility_b; } } /////////////////////////////////// public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } public delegate void MyDelegate(string textToSet); public event MyDelegate MyTextEvent; private void button1_Click(object sender, EventArgs e) { if (MyTextEvent != null) //Checks if anyone has registered the event. MyTextEvent("This is a sample text"); } public delegate void MyDelegate2(bool visibility_dl); public event MyDelegate2 visibility_ev; private void button2_Click(object sender, EventArgs e) { if (button2.Text == "Hide") { visibility_ev(false); button2.Text = "Show"; } else { visibility_ev(true); button2.Text = "Hide"; } } } //////////////////////////////////////////
-
As Christian said - use delegates. Simple example I've given before here[^]. Edited link!
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog)modified on Friday, August 22, 2008 3:23 AM
-
Create 1 form & 1 userform a couple of buttons etc,etc , put the userform inside the form and check this out ok?! I think you can read the code bellow. :-O ///////////////////////////////////////////// public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { userControl11.MyTextEvent += new UserControl1.MyDelegate(userControl11_TextEvent); userControl11.visibility_ev += new UserControl1.MyDelegate2(userControl11_visibility); } protected void userControl11_TextEvent(string textToSet) { textBox1.Text = textToSet; } protected void userControl11_visibility(bool visibility_b) { textBox1.Visible = visibility_b; } } /////////////////////////////////// public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } public delegate void MyDelegate(string textToSet); public event MyDelegate MyTextEvent; private void button1_Click(object sender, EventArgs e) { if (MyTextEvent != null) //Checks if anyone has registered the event. MyTextEvent("This is a sample text"); } public delegate void MyDelegate2(bool visibility_dl); public event MyDelegate2 visibility_ev; private void button2_Click(object sender, EventArgs e) { if (button2.Text == "Hide") { visibility_ev(false); button2.Text = "Show"; } else { visibility_ev(true); button2.Text = "Hide"; } } } //////////////////////////////////////////
no need for delegates, if form 1 called form 2 and you have problems passing values from form 2 to form 1 just make accessor methods from form 2 that after form 2 is closed, you can get the values from form 2.. you can also make use of again accessor methods if you want to pass values from form 1 to form 2 that before showing form2, set first the values then show form 2
-
no need for delegates, if form 1 called form 2 and you have problems passing values from form 2 to form 1 just make accessor methods from form 2 that after form 2 is closed, you can get the values from form 2.. you can also make use of again accessor methods if you want to pass values from form 1 to form 2 that before showing form2, set first the values then show form 2
don´t use 2 forms, use 1 form and load userforms inside, like what i showed you before. I would use many form, for exemple, if i wanted to create a chat application (to load private chat windows). But ... you are the boss :-D i don´t know what you are trying to achieve. Good Luck :cool: