5
Answers

What am I missing? (C# forms question)

cschuler

cschuler

19y
3.2k
1
Howdy all, I am new to C#, but have been programming in other languages (Perl,C,PHP) for a while. I have built a couple C# apps but noticed in the latest that I needed to set a value in the main form from a child form. I.e MainForm -> (drop-down option) Edit -> (new form) Preferences -> Set values that MainForm needs to perform operation x. I found a method for doing this was as bellow: My question is... Isn't there a better way? It would seem logical to call the new form and then be able to reference the objects that it contains (TextBox, RadioButton, Etc..) from within the MainForm. Or am I missing something. not thinking along windows forms lines or what have you?... anyway just thought I would ask. Class app{ /*MainForm*/ private void Passtarget(object sender){ /* Iterate through returns and set values MainForm_Var1 = ((TextBox)sender).Text.ToString(); } private void button1_Click(object sender, System.EventArgs e){ preferences mypref = new preferences(); mypref.passControl = new preferences.PassControl(Passtarget); mypref.ShowDialog(); return; } } public class preferences : System.Windows.Forms.Form{ /*Child Form*/ public delegate void PassControl(object sender); // Create instance (null) public PassControl passControl; private System.Windows.Forms.TextBox textBox1; } private void button3_Click(object sender, System.EventArgs e){ if (passControl != null){ //MessageBox.Show(passControl.ToString()); passControl(textBox1); } this.Hide(); } }
Answers (5)
Next Recommended Forum