簡體   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