0

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.

enter image description here

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


}
0

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

5 Comments

I tried changing the code as suggested; however, I get an error.
You're trying to assign a string as a control. Use the string as the index of the Controls collection and assign that control to the texteditor
Controls.IndexOf(TextEditor).ToString() always shows -1 now matter how many controls are added. I can't figure out why?
Use the string as the index of the Controls collection and assign that control to the texteditor something like this TextEditor = doc.Controls["Title1"] see how the name string is used as the index..
Okey I tried to set the Index like so: private void tabControl1_Selected(object sender, TabControlEventArgs e) { // Active Tab event tab.Controls.SetChildIndex(TextE, e.TabPageIndex); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.