After loading the display form in the C# TabContral tab, implement one-way parameter transfer test code example (picture)

黄舟
Release: 2017-03-10 14:18:38
Original
2281 people have browsed it

The information I found online a few days ago can load the entire form into a tab (see the previous article). This is very easy to implement, and multi-window display is also much more convenient.

After adding all the windows, I wonder if I can pass parameters on the main form to the form loaded on the tab, such as passing query parameters.

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

Set textBox1 on Form1 to public, so that textBox1 can be passed directly. Of course, you can also just pass the string

First use the code in the previous article to add form2 Go to the tab of form1, and set this event to be executed when the generatefm button is clicked

 private void generatefm_Click(object sender, EventArgs e)
        {
            tbMobile = new TabPage("中国");

            tbMobile.Name = "tbmobile";
            tabControl1.Controls.Add(tbMobile);

            Form form2 = new Form2(textBox1);             //传递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;
        }
Copy after login


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;
        }
Copy after login

}


In this way, the content entered in form1 can be passed directly when loading form2. This is the initial state. If you want to modify the input content after loading and change the content in form2 at the same time, 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;          
        }
Copy after login

The from2 variable used here needs to be declared at the beginning of form1, and the corresponding tab is added The code in simply needs to be changed. The final code of Form1:

 public partial class Form1 : Form
    {
        private TabPage tbMobile = null;  
        private TabPage tbUnion = null;

        Form2 form2 = null;        //先在这里声明,便于加载后引用

        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            form2.textBox2.Text = textBox1.Text;            //这里就可以引用了,form2的textBox2必须先设置成public
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            form2.textBox2.Text = textBox1.Text;            //切换选项卡时也获取最新的输入参数
        }

        private void generatefm_Click(object sender, EventArgs e)
        {
            tbMobile = new TabPage("中国");

            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;
        }
    }
Copy after login

This example only implements one-way parameter transfer. If in turn, the parameters need to be passed to the main form in the loaded form. Woolen cloth? Keep researching and let’s discuss if you have any good methods.

The above is the detailed content of After loading the display form in the C# TabContral tab, implement one-way parameter transfer test code example (picture). For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!