Dynamic TabPages

3xodus

Freshman
Joined
Dec 26, 2004
Messages
47
Location
Derbyshire, UK
Hi,

I'm writing a user interface for an online gaming system (found at TeamXLink.co.uk - it allows you to play XBox, PlayStation 2, Gamecube, PSP and Nontendo DS games online). The XLink engine, which does all the work tunneling packets over the Internet) is seperate from the UI, so basically you can write a UI in any language you like, so long as you communicate between the UI and engine correctly.

Anyway, onto my problem - I have a treeview populated with a list of online users (tvChatUsers) and a tabControl (tabPrivateMessages). I want to add a new tab to the tabControl when a user is double clicked on, the tab should contain a button, richTextBox and a textBox. I have the following code to add a tab:
Code:
   TabPage PMtab = new TabPage(e.Node.Text.ToString());
   tabChat.TabPages.Add(PMtab);
(e.Node.Text contains the users name)
That works fine, and adds a tabPage to the tabControl with the users name as it's .Text property.

The part I'm having a problem with is adding a button and textBox to the tabPage container.

I tried:
Code:
   TabPage PMtab = new TabPage(e.Node.Text.ToString());
   Button PMbutton = new Button();
   PMbutton.Text = "Send";
   PMtab.Container.Add(PMbutton);
   tabChat.TabPages.Add(PMtab);
But got the error "Use the new keyword to add an object instance." on the second to last line. I don't see how to use the new keyword here - I thought my first two lines were creating the object instances?

Also is it possible to name the button and textBox? - I was hoping to include the value at e.Node.Text in the name so that I know which button is being clicked - which user to send the message to.

Thankyou for any suggestions ro links :)
 
I would be tempted to create the tab page in the designer, then at runtime as the form loads, remove the tabpage from the tabcontrol.

Then, when a user is double-clicked, insert a new instance of the tabpage into the tabcontrol.

B.
 
penfold69 - that's one Idea I was considering, though I didn't get round to trying it. I wanted to do it through code if I could.

Joe Mamma that is perfect, thanks. Can't believe I din't think of .Parent :(


Thankyou both :)
 
your welcome. . .
might also consider creating a customcontrol . . .
change it to inherit from TabPage. . .

In the constructor, initialize all your child components. . .

then all you have to do is Add to the tab control an instance of your customtab already laid out.
 
Back
Top