C # A Simple Method for changing the attributes of the parent interface using the subinterface button

Source: Internet
Author: User

Yesterday, Shimei asked me a question, saying that I want to create a small program and use buttons on a main interface to create a subinterface to show and hide myself, you can click a button on the sub-interface to change the display attribute of the main interface (Visible) so that the main interface is displayed, and then destroy yourself.

At first, I thought of using a thread and a delegate to pass values on the interface, because I used it before, but later I thought it was a bit complicated and thread-commissioned, I always think there is a simpler way, so I have tried and thought about N methods, and finally found the simplest way based on the ideas provided by others.

This method does not need to create threads, do not use delegation, and do not have to do anything in the constructor. It can be called directly, with a small amount of code and no additional overhead. I personally think it is good.

// Form1 code:

Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}


Private void btn_CreateForm2_Click (object sender, EventArgs e)
{
This. Visible = false;
Form2 myform2 = new Form2 ();
Myform2.Owner = this;
Myform2.ShowDialog ();
}
}


// Form2 code:

Public partial class Form2: Form
{
Public Form2 ()
{
InitializeComponent ();
}


Private void btn_DispForm1_Click (object sender, EventArgs e)
{
Form1 myform1 = (Form1) this. Owner;
Myform1.Visible = true;
This. Close ();
}
}


// End...

The above code has been tested and feasible.

 


The following is an example of how to pass values in the parent window and child window in the blog garden:


Case study of passing values from the parent form to the child form
============================
1. Click Form1's button1 to open Form2

 


You can call the constructor of the overloaded sub-form to directly input values to the sub-form.


Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}


Private void button#click (object sender, EventArgs e)
{
Form2 frm2 = new Form2 (this. textBox1.Text );
Frm2.Show ();
}
}


Public partial class Form2: Form
{
Public Form2 ()
{
InitializeComponent ();
}


Public Form2 (string strTextBox1Text)
{
InitializeComponent ();
This. textBox2.Text = strTextBox1Text;
}
}


2. Click Form1's button1 to open Form2

 


Call the public attribute or method of the subform Form2 to set the value of textBox1 of Form1 to textBox2 of Form2.

Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}


Private void button#click (object sender, EventArgs e)
{
Form2 frm2 = new Form2 ();
Frm2.TextBox2Text = this. textBox1.Text;
Frm2.Show ();
}
}


Public partial class Form2: Form
{
Public Form2 ()
{
InitializeComponent ();
}


Public string TextBox2Text
{
Set {this. textBox2.Text = value ;}
Get {return this. textBox2.Text ;}
}
}
 

3. Click Form1's button1 to open Form2

 


Call the public attribute or method of the parent form Form1 in Form2_Load to set the value of textBox1 of Form1 to textBox2 of Form2.


Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}


Public string TextBox1Text
{
Set {this. textBox1.Text = value ;}
Get {return this. textBox1.Text ;}
}


Private void button#click (object sender, EventArgs e)
{
Form2 frm2 = new Form2 ();
Frm2.Show (this); // or frm2.ShowDialog (this );


/// Or
// Form2 frm2 = new Form2 ();
// Frm2.Owner = this;
// Frm2.Show (); // or frm2.ShowDialog ();
}

}

 


Public partial class Form2: Form
{
Public Form2 ()
{
InitializeComponent ();
}


Private void Form2_Load (object sender, EventArgs e)
{
Form1 frm1 = (Form1) this. Owner;
This. textBox2.Text = frm1.TextBox1Text;
}
}


The child form transmits a value to the parent form.
============================
4. Click Form1's button1 to open Form2
Click "button2" of "Form2 ".
In the button2_Click event, use this. Owner to set the value of textBox2 of Form2 to textBox1 of Form1 and disable Form2.

Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

Private void button#click (object sender, EventArgs e)
{
Form2 frm2 = new Form2 ();
Frm2.Show (this); // or frm2.ShowDialog (this );

/// Or
// Form2 frm2 = new Form2 ();
// Frm2.Owner = this;
// Frm2.Show (); // or frm2.ShowDialog ();
}
}

Public partial class Form2: Form
{
Public Form2 ()
{
InitializeComponent ();
}

Private void button2_Click (object sender, EventArgs e)
{
Form1 frm1 = (Form1) this. Owner;
// If textBox1 is placed in panel1, search for panel1 and then textBox1.
(TextBox) frm1.Controls ["textBox1"]). Text = this. textBox2.Text;
This. Close ();
}
}


5. Click Form1's button1 to open Form2
Click "button2" of "Form2 ".
In the button2_Click event, use this. Owner and call the Public attributes or methods of the parent form Form1
Set the value of textBox2 of Form2 to textBox1 of Form1 and disable
 
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

Public string TextBox1Text
{
Set {this. textBox1.Text = value ;}
Get {return this. textBox1.Text ;}
}

Private void button#click (object sender, EventArgs e)
{
Form2 frm2 = new Form2 ();
Frm2.Show (this); // or frm2.ShowDialog (this );

/// Or
// Form2 frm2 = new Form2 ();
// Frm2.Owner = this;
// Frm2.Show (); // or frm2.ShowDialog ();
}
}

Public partial class Form2: Form
{
Public Form2 ()
{
InitializeComponent ();
}

Private void button2_Click (object sender, EventArgs e)
{
Form1 frm1 = (Form1) this. Owner;
Frm1.TextBox1Text = this. textBox2.Text;
This. Close ();
}

}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.