繁体   English   中英

是否在其中动态添加控件的TabPage?

[英]Adding TabPages with controls inside them dynamically?

我试图添加一个TabPage,并在其中添加一个文本框。 问题是,如果我有更多的TabPage,则TextBox的内容始终是最后添加的内容,而不是所选的TabPage。

在此处输入图片说明

编辑3 //

public partial class Form1 : Form
{
    int tabcount = 0;
    TextBox TextE;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // Create a new textbox to add to each control
        TextE = new TextBox();

        // Fill the tabpage 
       TextE.Dock = DockStyle.Fill;
       TextE.Multiline = true;

         // Create new tab page and increment the text of it 
        TabPage tab = new TabPage();
        tab.Text = "Tab" + tabcount.ToString();

        // Add Textbox to the tab
        tab.Controls.Add(TextE);

        // Add tabpage to tabcontrol
        tabControl1.Controls.Add(tab);
        tabcount++;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        // Text content to messagebox...

        MessageBox.Show(TextE.Text);
    }


}

尝试使用数字索引(“ Tab01”,“ Tab02”等)命名选项卡,然后将当前选项卡转换为Scintilla TextEditor 不要对对象名称和Name属性感到困惑。 控件集合使用name属性作为索引键。 因此,即使您使用相同的名称声明了对象,只要name属性不同,也可以添加它们。

例如

    Scintilla NewTextEditor = new Scintilla();
    NewTextEditor.Name = "tab" + (this.Controls.OfType<Scintilla>.Count + 1).ToString("00");
    this.Controls.Add(NewTextEditor);

这将添加一个新控件,更改name属性,以便可以添加它。

然后假设您有一种确定当前选项卡的方法

Scintilla TextEditor = CurrentTab;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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