简体   繁体   中英

Adding a twin tabPage to tabControl through a user command

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 which is containing a TextBox object; this object, when the event "Text changed" is invoked, will perform the change of the parent tabPage's name.

在此处输入图片说明

The textbox where I typed "text changed by me" has a method which is managing changing the name of the tabPage:

private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox1.Text != "")
                this.tabControl2.SelectedTab.Text = this.textBox1.Text;
            else
                this.tabControl2.SelectedTab.Text = "(no name)";
        }

Into the current page menu is contained a control to add a new page, which runs this method when the user click on it:

private void addNewPageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int numPagine;
            string strPagine;
            numPagine = this.tabControl2.TabCount;
            strPagine = numPagine.ToString();
            this.tabControl2.TabPages.Add("new page" + strPagine);
        }

...and here is the output, which is expected since I'm just asking to add a new empty tabPage:

在此处输入图片说明

So, my question is: how can I make possible that when the user is clicking on "Add new page", rather than creating an empty new tabPage the program is rather creating a page like the first one (ie containing a textbox into the same position which has a method to change the text of the parent tabPage that I have just created?

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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM