closet geek Posted April 2, 2006 Posted April 2, 2006 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: 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: RichTextBox richTextBox1 = new RichTextBox(); tabPage2.get_Controls().Add(richTextBox1); Thanks a lot. Quote
Leaders snarfblam Posted April 2, 2006 Leaders Posted April 2, 2006 If you wanted to hack something out real quick, you could probably reference the RichTextBox by the tab's control collection and an index... 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. 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. Quote [sIGPIC]e[/sIGPIC]
closet geek Posted April 2, 2006 Author Posted April 2, 2006 Thanks - I attempted your quick and dirty solution: 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: richTextBox1.Find Which seems strange? I can select the RTB, set its Text but not Find in it? Any ideas? Cheers. Quote
Cags Posted April 2, 2006 Posted April 2, 2006 I'd guess that you need to cast the object to a richTextBox before you can access its specific functions. I could be wrong though. Quote Anybody looking for a graduate programmer (Midlands, England)?
Leaders snarfblam Posted April 2, 2006 Leaders Posted April 2, 2006 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. Quote [sIGPIC]e[/sIGPIC]
closet geek Posted April 4, 2006 Author Posted April 4, 2006 (edited) 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? Edited April 4, 2006 by closet geek Quote
Cags Posted April 4, 2006 Posted April 4, 2006 Something like this should suffice. if(tabControl1.TabPages[index].Controls[2] is RichTextBox) { RichTextBox myRtf = (RichTextBox)tabControl1.TabPages[index].Controls[2]; myRtf.DoSomething(); // where DoSomething() is the relevant method. } Quote Anybody looking for a graduate programmer (Midlands, England)?
closet geek Posted April 4, 2006 Author Posted April 4, 2006 (edited) Fantastic - thank you! Edited April 4, 2006 by closet geek Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.