Create new RichTextBox in tab page

ironfistchamp

Active member
Joined
Dec 9, 2005
Messages
29
Programming Experience
Beginner
Hey there

I would like to know how I can create a new rich text box in a tab page. I have got the code to create the new tab but its just a blank one and I would like to have a docked rich text box in there. Any help on this would be great. Thanks very much.

Ironfistchamp
 
To programatically add controls to a tab page you would do something like this...

TB.controls.add(rb)

Where tb is the variable assigned to your tab page and where rb is assigned to a richtextbox i.e

dim rb as new richtextbox
rb.size = new size(30,30) // or whatever size you want it to be.

so the full code would look like this..

VB.NET:
Dim Rb as new RichTextBox
Rb.size = new size(height,width)
Tb .controls.add(rb)

Is this what you were after?
 
Not exactly. The code I have to create a new tab page is this

"Dim i As Integer = 2
Dim tab As New TabPage
tab.Name = ("tabPage" & i)
tab.Text = ("Untitled" & i)
tabbox.Controls.Add(tab)
i = i + 1 "

Basically creates the new tab and increments i so the next tab is one number higher. The thing is its a blank page. There is nothing in it. I need some code so that when the page is created there is a rich text box inside. With the code supplied above I get an error because you can only add tab pages. Maybe I got the wrong end of the stick. If so a clearer explanation would be great. If not another method perhaps?

Thanks

Ironfistchamp
 
ironfistchamp, I think vis781 is right, you just have to change his example variable name "tb" with your own "tab".
 
Yea you have got the wrong end of the stick. To add a tabpage to a tabcontrol you should be using..

VB.NET:
Tabbox.tabpages.add(tab)
Then to add controls to the tabpage you first have to declare a variable and assign it to, in your case a richtextbox. i.e

VB.NET:
Dim Rb as new richtextbox
Then set the properties of the RichTextBox. i.e size etc...

Then add it to yuor tabpage using...

VB.NET:
tabbox.tabpages(the index of the page you want).controls.add(rb)

With me?
 
gotcha... sorry I'm not the brightest you know. I have only been doing this for a couple of weeks. I think I should get a book or something as college tutor doesn't seem to help much :rolleyes:

Next question is how can I add a right click menu to the tabs? In fact is that even possible? I would just like it so I can close the tab by rightclicking and selecting it from a menu.

Thanks

Ironfistchamp
 
No problem you can add a context menu from the toolbox and drop it onto your form. Or you can do it programatically in code. In this case i would use the toolbox option. Then select the context menu in the component tray and look up at the top of the form. You will see your 'Right Click' menu. Click it and type in the text that you want your menu item to display.
Then Double click it to raise an event handler for this menu item and add the code in here to close your tab page.
Then select your tabbox and assign it's contextmenu property to the contextmenu you have just created.

Btw you cant actually close a tab page but you can set it's visible property to false.
 
Excelent it works. One tiny little niggle is that when I create the new rich text box inside the tab page and set the DockStyle to fill it ends up being bigger than the original richt text box.

The program starts with one tab page with rtb inside. Then when you create a new tab it also has an rtb inside. The original has a gap round the edge. The new one seems to overlap. I have set the tabs margin and padding to 3 all the way around and the rbt's margin to 3 all the way around just like the first one.

Is there something I'm missing?

Thanks

Ironfistchamp
 
Sorry to cut and run on you like this but i'm off down the pub. But i'll check it out tomorrow and get back to you. But hopefully someone else could help you before then.
 
Just to say thanks for the help. Erm sorry people but I've got another prob. Hope I'm not bugging you.

Basically because each rtb has a different name when I want to save the text I have a problem because I need to find out the name of the current text box. How can I do this.

An example name of an rtb is "rtb *number of tab page*"
An example name of a tab page is "1"

I've set a variable like this

writewhat = "rtb" & tabbox.SelectedTab.Name & ".text"

That just writes out rtb1.text into the file. Does anyone know how I can get the name of the rtb in the selected tab and get the text from it into a file?

Thanks

Ironfistchamp


PS: I have the actually saving bit worked out fine :p
 
not at my pc at the mo but you should be able to iterate throuhg the control collection of a specified tab page and find the one you want, maybe i'm wrong here because i'm doing this from memory but you could try...

VB.NET:
For each ctrl as control in tabbox.tabpages(index of tabpage).controls
if ctrl is typeof richtextbox then
textbox1.text = ctrl.name
 
All i'm tring to do in my last post is to use a loop to 'run through' the controls on the selected tabpage..

VB.NET:
For each crtl as control in .......

where crtl is the object that i have declared as a control, it doesn't matter what sort of control crtl is, it's just a way of looping through the tabpages control collection. Then the next bit is fairly straight forward...

VB.NET:
if ctrl is typeof richtextbox Then

so if you have two buttons and one richtextbox on your tabpage, the first time through the loop ctrl may be a button, then the second time it may be the other button, but the third time ctrl will be a richtextbox so the above if statement will execute it's Then clause..

the bottom bit about the textbox is just an example of how to use the ctrl variable. We know now that ctrl is our rishtextbox so we can extract the name

VB.NET:
ctrl.name.
 
That makes much more sense thanks. If I run ino any poblems I'll come back. THanks for all your help.


Ironfistchamp

EDIT: Apparently "richtextbox is a type and cannot be used in an expression." Eh? Oh and it wants an "is" before "then"... if I inset that then i get "Keyword does not name a type"
 
Last edited:
how about "if typeof ctrl is richtextbox" ?
 
Back
Top