Control
-
i want, form another form, and at a specified condition, to create an object from a certain control and add it to the first form, like :
namespace Win
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1 = new Button();
private System.Windows.Forms.Button button2 = new Button();
public System.Windows.Forms.Panel panel1;public Form1() { InitializeComponent(); } static void Main() { Application.Run(new Form1()); } private void button1\_Click(object sender, System.EventArgs e) { this.Close(); } private void button2\_Click(object sender, System.EventArgs e) { Form2 f2 = new Form2(); f2.Show(); } }// Form1 public class Form2 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1 = new Button() ; public Form2() { InitializeComponent(); } private void button1\_Click(object sender, System.EventArgs e) { // at a specified condition : Button b = new Button(); b.Text = "Hello" ; Form1 f1 = new Form1(); f1.panel1.Controls.Add(b); } }// Form2
}// namespace Win
this code does not work. What is the wrong here ? thanks
-
i want, form another form, and at a specified condition, to create an object from a certain control and add it to the first form, like :
namespace Win
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1 = new Button();
private System.Windows.Forms.Button button2 = new Button();
public System.Windows.Forms.Panel panel1;public Form1() { InitializeComponent(); } static void Main() { Application.Run(new Form1()); } private void button1\_Click(object sender, System.EventArgs e) { this.Close(); } private void button2\_Click(object sender, System.EventArgs e) { Form2 f2 = new Form2(); f2.Show(); } }// Form1 public class Form2 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1 = new Button() ; public Form2() { InitializeComponent(); } private void button1\_Click(object sender, System.EventArgs e) { // at a specified condition : Button b = new Button(); b.Text = "Hello" ; Form1 f1 = new Form1(); f1.panel1.Controls.Add(b); } }// Form2
}// namespace Win
this code does not work. What is the wrong here ? thanks
In
Form2.button1_Click
you're creating a new instance ofForm1
, adding aButton
to itspanel1
control and... well, that's it.f1
is going the way of the dodo bird when the method exits. What I think you're trying to do is add theButton
to the instance ofForm1
that calledForm2.Show
in the first place. You'll need a reference to that object inForm2
. You can pass it inForm2
's constructor or as a property. Here's an example:public class Form2 : Form
{
Form1 _form1;
public Form2(Form1 form1)
{
_form1 = form1;
}private void button1_Click(object sender, EventArgs e)
{
// create button as before
_form1.panel1.Controls.Add(b);
}// rest of class
}public class Form1 : Form
{
private void button2_Click(object sender, System.EventArgs e)
{
Form2 f2 = new Form2(this);
f2.Show();
}// rest of class
}Charlie if(!curlies){ return; }
-
i want, form another form, and at a specified condition, to create an object from a certain control and add it to the first form, like :
namespace Win
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1 = new Button();
private System.Windows.Forms.Button button2 = new Button();
public System.Windows.Forms.Panel panel1;public Form1() { InitializeComponent(); } static void Main() { Application.Run(new Form1()); } private void button1\_Click(object sender, System.EventArgs e) { this.Close(); } private void button2\_Click(object sender, System.EventArgs e) { Form2 f2 = new Form2(); f2.Show(); } }// Form1 public class Form2 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1 = new Button() ; public Form2() { InitializeComponent(); } private void button1\_Click(object sender, System.EventArgs e) { // at a specified condition : Button b = new Button(); b.Text = "Hello" ; Form1 f1 = new Form1(); f1.panel1.Controls.Add(b); } }// Form2
}// namespace Win
this code does not work. What is the wrong here ? thanks