简体   繁体   中英

Adding TabPages with controls inside them dynamically?

I am trying to add a TabPage and inside it a Textbox. The problem is if I have more TabPages the content of the TextBox is always the last added and not the selected TabPage.

在此处输入图片说明

EDIT 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);
    }


}

Try naming the tabs with a number index("Tab01","Tab02", etc.) then cast the current tab as Scintilla TextEditor . Don't get confused by the object name and the Name property. The controls collection uses the name property as an index key. so even though you have objects you've declared with the same name, as long as the name property is different you can add them.

For instance

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

this will add a new control change the name property so it can be added.

Then Assuming you have a way of determining the current tab

Scintilla TextEditor = CurrentTab;

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