So I am trying to create a text editor for php, html, css an JS. I have a tab function that creates a new tab, and inserts a new richtextbox ready to type code. My problem is this, how do I select only the text from the currently selected tab so that the user can save their file? Here is the code for the button which creates a new tab and inserts a new RTB into the tab:

private void newTool_Click(object sender, EventArgs e)
    // Create a new tab.
    string newTabTitle = "New file " + (tabControl.TabCount + 1).ToString();
    TabPage newTab = new TabPage(newTabTitle);

    // New tab's settings.
    newTab.Name = newTabTitle; 
    newRTBName = newTabTitle;

    // Adds a new rich text box control.
    RichTextBox newRTB = new RichTextBox();

    // Settings for new rich text box.
    newRTB.Dock = DockStyle.Fill;
    newRTB.Name = newTabTitle;
    newRTB.Text = "NAME: " + newRTBName;
    tmpSaveData[tabControl.TabCount] = newRTB.Text;
    newRTB.Font = new Font("Courier New", 10);

3 Years
Discussion Span
Last Post by CJMW

Would something like this:

TabControl1.SelectedTab.Controls.Find("MyRichTextBox", [True])

Be the answer you are looking for?

Just replace the "MyRichTextBox" with the tab title, et voila.


Thanks for your reply! How would I then get the text from the tab? For example how would I be able to display the text in: MessageBox.Show(); ?


To expand on the previous post...

RichTextBox textBox = TabControl1.SelectedTab.Controls.Find("MyRichTextBox", [True]) as RichTextBox;

Sure you can figure out the rest...

Edited by Ketsuekiame: Better design


Ahhhhhh.... So I understand how this would work... however I am getting a build error: "Error 1 Cannot convert type 'System.Windows.Forms.Control[]' to 'System.Windows.Forms.RichTextBox' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

My code is as follows:

RichTextBox textBox = tabControl.SelectedTab.Controls.Find("New file 2", true) as RichTextBox;


Attach .FirstOrDefault() to the .Find() method.

If the framework doesn't allow this, you will need to pull out the controls into an array, check that the array contains a single element and then cast that element into the textbox object.


Okay so its been a few days and I still cant figure this one out! When I run:

RichTextBox textBox = tabControl.SelectedTab.Controls.Find(("New file " + tabNum), true).FirstOrDefault() as RichTextBox;

I get a null reference exception... "Object reference not set to an instance of an object". I don't understand because I know the RichTextBox exists and what its ecaxt name is because it's the same name as the new tab and I used the same string variable to set both.

Any suggestions?


You can try this

        private void button2_Click(object sender, EventArgs e)
            foreach (RichTextBox richtext in tabControl1.TabPages[tabControl1.SelectedIndex].Controls)

Or just change your code to

            int tabNum = tabControl1.SelectedIndex + 1;
            RichTextBox textBox = tabControl1.SelectedTab.Controls.Find(("New file " + tabNum), true).FirstOrDefault() as RichTextBox;

Such a small thing that's causing all the frustration

Edited by Fenrir()


I know the RichTextBox exists and what its ecaxt name is because it's the same name as the new tab and I used the same string variable to set both.

Well that's a key bit of information that makes it all easier...

string tabName = tabControl1.SelectedTab.Name;
RichTextBox textBox = tabControl1.SelectedTab.Controls.Find((tabName), true).FirstOrDefault();
if(textBox == null)
    MessageBox.Show("Could not find textbox!");


If you're sure that the name of the tab and the name of the textbox are the same, it makes more sense to do it this way around to avoid any typographical errors and such.


Thank you all for your answers! Fenrir I used your foreach loop and it works perfectly! Thank you so much!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.