在每个TabPage上显示TextBox

时间:2017-11-02 14:54:24

标签: c# textbox tabcontrol tabpage

我将TextBox附加到TabPage的第一个TabControl。我想在每个TextBox上显示相同的TabPage对象。我尝试将控件添加到tabControl集合中,但不幸的是它无效。

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

2 个答案:

答案 0 :(得分:1)

按钮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);
    }

答案 1 :(得分:0)

我错过了两种方法。它现在正在工作!

tabControl1.Selecting += new TabControlCancelEventHandler(tabControl1_Selecting);


    void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
    {

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