简体   繁体   中英

Show TextBox on every TabPage

I attached a TextBox to the first TabPage of a TabControl . I would like to display the same TextBox object on every TabPage . I tried to add the control to the tabControl Collection but unfortunately it's not working.

private void Form1_Load(object sender, EventArgs e)
{
    tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Add(textBox);
}

Button b;

    public Form1()
    {
        InitializeComponent();
        b = new Button() { Text = "Prueba" };
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        AddButtonToTabControl();
    }

    private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
    {
        AddButtonToTabControl();
    }

    public void AddButtonToTabControl()
    {
        tabControl1.SelectedTab.Controls.Add(b);
    }

I missed two methods. It's working now!

tabControl1.Selecting += new TabControlCancelEventHandler(tabControl1_Selecting);


    void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
    {

        tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Add(textBox);
    }

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