Changing data on a different form
-
Stipulation - I'm doing this with notepad and the SDK.. no Visual Studio. How do you get a spawend dialog to interact with the main form? With VB, it was as easy as referring to the main form and it's properties. It doesn't seem to be as easy with c#.. example code..
using System;
using System.Windows.Forms;
using System.Drawing;class MainForm : Form {
Button btnGo = new Button(); public MainForm() { Text = "Main Form"; btnGo.Location = new Point(100,100); btnGo.Name = "btnGo"; btnGo.Text = "Go"; btnGo.Click += new EventHandler(btnGo\_Click); Controls.Add(btnGo); Opacity = 80; } public static void Main(string\[\] args) { Application.Run(new MainForm()); } private void btnGo\_Click( object sender, EventArgs e) { Form2 x = new Form2(); x.ShowDialog(); }
}
class Form2 : Form {
Button btn1 = new Button(); public Form2() { Text = "This is the second form"; btn1.Text = "Do it"; btn1.Location = new Point(100,100); btn1.Click += new EventHandler(btn1\_Click); btn1.Name = "btn1"; } private void btn1\_Click(object sender, EventArgs e) { //Here is where I want to modify the main form. // How would I go about changing, let's say, the Title? }
}
-
Stipulation - I'm doing this with notepad and the SDK.. no Visual Studio. How do you get a spawend dialog to interact with the main form? With VB, it was as easy as referring to the main form and it's properties. It doesn't seem to be as easy with c#.. example code..
using System;
using System.Windows.Forms;
using System.Drawing;class MainForm : Form {
Button btnGo = new Button(); public MainForm() { Text = "Main Form"; btnGo.Location = new Point(100,100); btnGo.Name = "btnGo"; btnGo.Text = "Go"; btnGo.Click += new EventHandler(btnGo\_Click); Controls.Add(btnGo); Opacity = 80; } public static void Main(string\[\] args) { Application.Run(new MainForm()); } private void btnGo\_Click( object sender, EventArgs e) { Form2 x = new Form2(); x.ShowDialog(); }
}
class Form2 : Form {
Button btn1 = new Button(); public Form2() { Text = "This is the second form"; btn1.Text = "Do it"; btn1.Location = new Point(100,100); btn1.Click += new EventHandler(btn1\_Click); btn1.Name = "btn1"; } private void btn1\_Click(object sender, EventArgs e) { //Here is where I want to modify the main form. // How would I go about changing, let's say, the Title? }
}
Try this. I added 2 lines of code, one in the "Owner" form (btnGo_Click), and one in the "Owned" form (btn1_Click). Good Luck! class MainForm : Form { Button btnGo = new Button(); public MainForm() { Text = "Main Form"; btnGo.Location = new Point(100,100); btnGo.Name = "btnGo"; btnGo.Text = "Go"; btnGo.Click += new EventHandler(btnGo_Click); Controls.Add(btnGo); Opacity = 80; } public static void Main(string[] args) { Application.Run(new MainForm()); } private void btnGo_Click( object sender, EventArgs e) { Form2 x = new Form2(); x.Owner = this; x.ShowDialog(); } } class Form2 : Form { Button btn1 = new Button(); public Form2() { Text = "This is the second form"; btn1.Text = "Do it"; btn1.Location = new Point(100,100); btn1.Click += new EventHandler(btn1_Click); btn1.Name = "btn1"; Controls.Add(btn1); } private void btn1_Click(object sender, EventArgs e) { //Here is where I want to modify the main form. // How would I go about changing, let's say, the Title? this.Owner.Text = "HOWDY"; } } Andy Gaskell, MCSD
-
Try this. I added 2 lines of code, one in the "Owner" form (btnGo_Click), and one in the "Owned" form (btn1_Click). Good Luck! class MainForm : Form { Button btnGo = new Button(); public MainForm() { Text = "Main Form"; btnGo.Location = new Point(100,100); btnGo.Name = "btnGo"; btnGo.Text = "Go"; btnGo.Click += new EventHandler(btnGo_Click); Controls.Add(btnGo); Opacity = 80; } public static void Main(string[] args) { Application.Run(new MainForm()); } private void btnGo_Click( object sender, EventArgs e) { Form2 x = new Form2(); x.Owner = this; x.ShowDialog(); } } class Form2 : Form { Button btn1 = new Button(); public Form2() { Text = "This is the second form"; btn1.Text = "Do it"; btn1.Location = new Point(100,100); btn1.Click += new EventHandler(btn1_Click); btn1.Name = "btn1"; Controls.Add(btn1); } private void btn1_Click(object sender, EventArgs e) { //Here is where I want to modify the main form. // How would I go about changing, let's say, the Title? this.Owner.Text = "HOWDY"; } } Andy Gaskell, MCSD