Example of implementing a one-way parameter pass test code after loading a display form in the C # tabcontral tab (figure)

Source: Internet
Author: User
A few days ago on the Internet to find the information can be loaded into the entire form of the option Advant (see previous article), so that the implementation is very useful, multi-window display is also a lot of convenience.

After adding the window, I wondered if I could pass the parameters on the main form to the form that was loaded on the tab, such as the parameters for passing the query.

1. Create a new 2 form: main form Form1, subform Form2

Set the TextBox1 on the Form1 to public so that the textBox1 can be passed directly to the past and, of course, only string

First, use the previous code to add Form2 to the Form1 tab, where the event is set when you click the Generatefm button.

private void Generatefm_click (object sender, EventArgs e)        {            tbmobile = new TabPage ("China");            Tbmobile.name = "Tbmobile";            TABCONTROL1.CONTROLS.ADD (tbmobile);            Form Form2 = new Form2 (textBox1);             Pass TextBox1 past              //Form2 form = new Form2 ();              Form2. TopLevel = false;            Form2. BackColor = Color.White;            Form2. Anchor = Anchorstyles.top | Anchorstyles.left | Anchorstyles.right;            Form2. FormBorderStyle = Formborderstyle.none;            Form2. Show ();            TBMOBILE.CONTROLS.ADD (Form2);            Tabcontrol1.selectedindex = 2;        }


Use the constructor in Form2 to accept parameters:

    public partial class Form2:form    {public        Form2 ()        {            InitializeComponent ();        }        Public Form2 (TextBox ts)        {            InitializeComponent ();            TextBox2.Text = ts. Text;        }

}


This enables the loading of Form2 to pass the contents of the Form1 input directly past, which is the initial state. If you want to modify the input after loading and change the contents of Form2, continue:

      private void Button1_Click (object sender, EventArgs e)        {            form2.textBox2.Text = TextBox1.Text;        }        private void Tabcontrol1_selectedindexchanged (object sender, EventArgs e)        {            Form2.textBox2.Text = TextBox1.Text;                  }

The FROM2 variable used here needs to be declared in the first place in Form1, and the code in the corresponding Join tab is simply changed, Form1 the final code:

 Public partial class Form1:form {private TabPage tbmobile = null;        Private TabPage tbunion = null;        Form2 Form2 = null;        Declare here first to facilitate loading after referencing public Form1 () {InitializeComponent ();            } private void Button1_Click (object sender, EventArgs e) {form2.textBox2.Text = TextBox1.Text; Here you can refer to, Form2 's textBox2 must first be set to public} private void Tabcontrol1_selectedindexchanged (object Sende            R, EventArgs e) {form2.textBox2.Text = TextBox1.Text; Also get the latest input parameters when switching tabs} private void Generatefm_click (object sender, EventArgs e) {Tbmobile            = new TabPage ("China");            Tbmobile.name = "Tbmobile";            TABCONTROL1.CONTROLS.ADD (Tbmobile);            Form2 = new Form2 (textBox1);              Form2 form = new Form2 (); Form2.            TopLevel = false; Form2.            BackColor = Color.White; Form2. Anchor = ANCHORSTYLES.TOp | Anchorstyles.left |            Anchorstyles.right; Form2.            FormBorderStyle = Formborderstyle.none; Form2.            Show ();            TBMOBILE.CONTROLS.ADD (Form2);        Tabcontrol1.selectedindex = 2; }    }

This example simply implements a one-way parameter pass, and if you need to pass the parameter to the main form in the loaded form, in turn? Continue to study, you have any good ways to explore together.

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.