Problem with tab control and rich text box

mars

Member
Joined
Jun 19, 2006
Messages
13
Programming Experience
1-3
I m going to create a text editor in vb.net with tab controls and rich text boxes
it should create programmatically a new “tabPage” with a “RichTextBox” in it for every opened file. I wrote the open() function which is working fine
. It worked like I wanted, for every opened file it created a new tabPage with RichTextBox showing the contents of the file. But, when I tried to code the Save function it was working only for the first tabPage and RichTextBox created by me, but not those created programmatically. Therefore my problem is that I can’t create functions that will work for the tabPages and RichTextBoxes created programmatically, like the Visual Studio .Net ( for every opened source it creates a tabPage ex.: Form1.cs[Design] plus Form1.cs source so you can work with both of them at the same time ).

thanx
 
You have to identify which TabPage is currently selected (TabControl.SelectedTab). Further get the RichTextBox control in that TabPage from the TabPage.Controls collection. I see you mention .cs files, so note that this is a VB.Net site.
 
its just an example im working in vb.net
i used also the selectedtab property but still its saving just the first tab's rich text box contents
 
Please post the code to get the RichTextBox and save its contents.
 
hi again
i made a form and put just a blank tab control there
i m creating richtextbox and tabpage on form load event
VB.NET:
PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
Dim myTabPage As TabPage = New TabPage("first")
TabControl1.TabPages.Add(myTabPage)
Module1.index = TabControl1.TabPages.IndexOf(myTabPage)
Dim myRichTextBox As RichTextBox = New RichTextBox
TabControl1.TabPages(index).Controls.Add(myRichTextBox)
EndSub
i have 2 buttons load and save
code in load method is here


VB.NET:
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.Filter = "Text Files|*.txt|All Files|*.*"
OpenFileDialog1.ShowDialog()
Dim title = OpenFileDialog1.FileName
Dim myTabPage As TabPage = New TabPage(title)
TabControl1.TabPages.Add(myTabPage)
Module1.index = TabControl1.TabPages.IndexOf(myTabPage)
Dim myRichTextBox As RichTextBox = New RichTextBox
myRichTextBox.LoadFile(title, RichTextBoxStreamType.PlainText)
TabControl1.TabPages(index).Controls.Add(myRichTextBox)
TabControl1.SelectedTab = myTabPage
EndSub

now i have opened files each on a new tab and richtextbox showing the contents of opened file

i have problem in save how cud i refer the tab page and richtext box from save method becuase both are created in diff procedure like open
and the scope of those object isnt here in the save method

If u have any question ask me
regards
 
Last edited by a moderator:
JohnH said:
You have to identify which TabPage is currently selected (TabControl.SelectedTab). Further get the RichTextBox control in that TabPage from the TabPage.Controls collection.
If I was a bad teacher I would tell you to read the above quote 100 times, but I'm even worse so I will also give you some code. Skipping checking the controls collection here since you only add one control to it anyway.
VB.NET:
Function GetCurrentRichTextBox() As RichTextBox
  Return TabControl1.SelectedTab.Controls(0)
End Function
You could then call it:
VB.NET:
Dim rt As RichTextBox = GetCurrentRichTextBox()
rt.SaveFile( ............ )
If you had to search for RichTextBox among many controls you could do this:
VB.NET:
Function GetCurrentRichTextBox() As RichTextBox
 For Each c As Control In TabControl1.SelectedTab.Controls
  If TypeOf c Is RichTextBox Then Return c
 Next
 Return Nothing
End Function
 
Back
Top