Multiple RichTextBoxs in tabs

closet geek

Newcomer
Joined
Apr 2, 2006
Messages
21
Hi,

I have a program that needs multiple tabs. The tabs are created when a user clicks a button and these created tabs are clones of the initial tab (which has a richtexbox on it).

I have a couple of questions.

1) How do I allow for unlimited tab creation - so instead of me defining a tab name I can say to J#: "Name the tab tabPage[integer] and after every creation do [integer]++". Currently I'm just using:

Code:
TabPage tabPage2 = new TabPage("tab");
tabControl1.get_TabPages().Add(tabPage2);

Which does allow multiple tabs to be open but do these have the same name?

2) How do I reference the richtextboxes that are created when a new tab is created? How do I ensure each richtext box that is created has a unique identifier (as with the tabs)?

I'm currently just using this, again it creates a richtextbox no matter how many tabs I create but I'm not sure how to reference the richtextbox:

Code:
RichTextBox richTextBox1 = new RichTextBox();
tabPage2.get_Controls().Add(richTextBox1);

Thanks a lot.
 
If you wanted to hack something out real quick, you could probably reference the RichTextBox by the tab's control collection and an index...
C#:
tabControl1.TabPages[index].Controls[2].Text //if you know that the RTF box is control #2

If you want to take a neater, cleaner approach, you need to store the newly created objects in some sort of array or collection. You may need to wire/unwire events on the fly. I don't know J#, so you will probably have to research the syntax in MSDN, but here is the c# equvalent.
C#:
List<RichTextBox> RTFs = new List<RichTextBox>(); //ArrayList could also be used
// Should be initialized with any RTFs that were placed at design time
// i.e. in contructor RTFs.Add(rtfDocument); where rtfDocument is
// the name of the RichTextBox added at design time.

// An example of duplicating a tab with an RTF
void DuplicateRtf(){
    // Create the new rich text box with copied text
    RichTextBox CurrentBox = RTFs[tabControl1.SelectedIndex];
    RichTextBox NewBox = new RichTextBox();
    NewBox.Rtf = CurrentBox.Rtf;
    
    // If you need to wire events:
    NewBox.LinkClicked += this.OnRtfLinkClicked();
    
    // Create the new tab page and add the new Rtf box.
    // This can be accessed later via index in the TabControl.TabPages collection.
    TabPage NewPage = new TabPage;
    tabControl1.TabPages.Add(NewPage);
    NewPage.Controls.Add(NewBox);
}

// An example of how to access a specific text box,
// in this case, the currently visible one.
void DisplayRtfFromCurrentlyVisibleBoxJustAsAnExample(){
    string RTF = Rtfs[tabControl1.SelectedIndex].Rtf;
    MessageBox.Show(RTF);

}
I haven't tested this either, but it should demonstrate the basic process.
 
Thanks - I attempted your quick and dirty solution:

Code:
tabControl1.get_SelectedTab().get_Controls().get_Item("richTextBox1").Select();

This code selects the RTB created on the new tab however I can't then peform actions such as:

Code:
richTextBox1.Find

Which seems strange? I can select the RTB, set its Text but not Find in it? Any ideas?

Cheers.
 
That is exactly what the problem is. Because you are getting it from the Controls collection, all that the compiler knows is that it is a control, i.e. it has a text property, a position, a parent, etc. but not that it has an Rtf property, a Find method, etc. because for all the compiler knows, Controls[1] could be a button or a group box. So, yes, you need to cast.
 
Thanks - but I get errors when I try to cast the object, I am using this:

(RichTextBox) tabControl1.get_SelectedTab().etc etc

Is this not the correct way cast?
 
Last edited:
Something like this should suffice.
C#:
if(tabControl1.TabPages[index].Controls[2] is RichTextBox)
{
    RichTextBox myRtf = (RichTextBox)tabControl1.TabPages[index].Controls[2];
    myRtf.DoSomething(); // where DoSomething() is the relevant method.
}
 
Back
Top