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:

// NEW FILE TOOL
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);
    tabControl.TabPages.Add(newTab);

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

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

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

}

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...

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;

MessageBox.Show(textBox.Text);

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;
//textBox.Focus();
MessageBox.Show(textBox.Text);

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)
            {
                MessageBox.Show(richtext.Text);
            }
        }

Or just change your code to

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

Such a small thing that's causing all the frustration

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!");
    return;
}

MessageBox.Show(textBox.Text);

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.