Adding a twin tabPage to tabControl through a user command

后端 未结 1 1219
青春惊慌失措
青春惊慌失措 2021-01-27 04:52

I\'m a newbie in c# and probably going to ask a very easy question, but I\'ve not been able to find anything on the web to help.

I have a tabControl with a TabPage whic

相关标签:
1条回答
  • 2021-01-27 05:18

    Here is an example.

        //..
        // create the new page
        TabPage tpNew = new TabPage("new page..");
        // add it to the tab
        this.tabControl2.TabPages.Add(tpNew);
        // create one labe with text and location like label1
        Label lbl = new Label();
        lbl.Text = label1.Text;
        lbl.Location = label1.Location;
        // create a new textbox..
        TextBox tbx = new TextBox();
        tbx.Location = textBox1.Location;
        tpNew.Controls.Add(lbl);
        tpNew.Controls.Add(tbx); 
        // add code to the new textbox via lambda code:      
        tbx.TextChanged += ( (sender2, evArgs) =>
        {
            if (tbx.Text != "")
                this.tabControl2.SelectedTab.Text = tbx.Text;
            else
                this.tabControl2.SelectedTab.Text = "(no name)";
        } );
    

    For more complicated layout you may want to consider creating a user control.. You also may want to create the first page with this code; the, of course with real values for text and positions!

    For creating a UserControl you go to the project tag and right click Add-UserControl-UserControl and name it, maybe myTagPageUC. Then you can do layout on it like on a form. A rather good example is right here on MSDN

    The problem is that is has no connection to the form, meaning you'll have to code all sorts of references to make it work..

    I'm not really sure if you may not be better off writing a complete clonePage method instead. It could work like the code above, but would loop over the Controls of the template page and check on the various types to add the right controls..

    It really depends on what is more complicated: the Layout or the ties between the pages and the form and its other controls..

    0 讨论(0)
提交回复
热议问题