-
Feb 6th, 2013, 11:23 AM
#1
Thread Starter
Member
Access Child control
Good morning forum,
I was wondering if what I'm doing is correct or if there is a better way. I have a Tab Control and I add tab page at run-time. Each page contains a richtextbox. When the user clicks the save all documents button it will save the text inside the richtextbox to its file. This following code loops through each tab and does what I mentioned above. When I create the page I add the file location to the tag property of the RIchTextBox. What I am worried about is the
Code:
myTabPages.Controls(0)
part. Is there a different way to get that control even if its index 0, 1, 2? also what if the control was inside a panel which sits inside the TabPage. Would I then have to loop through each page, find the panel, then find the RichTextBox?
Code:
For Each myTabPages As TabPage In TabControl1.TabPages
Dim iDocument = CType(myTabPages.Controls(0), RichTextBox)
If iDocument.Modified = True Then
Dim objwriter As New IO.StreamWriter(iDocument.Tag, False)
objwriter.WriteLine(iDocument.Text())
objwriter.Close()
iDocument.Modified = False
End If
Next
-
Feb 6th, 2013, 12:05 PM
#2
Re: Access Child control
How are the tabpages created? When you add the RTB, give it (the RTB) a unique name and then you can later get to it by the name instead of an index but this only works if you know what container the RTB is in.
The other alternative that works regardless of where you place the RTB in is to keep a direct reference to it when you add it. You can create a new class that inherit the tabpage class and add a property for setting/getting the RTB...
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Feb 6th, 2013, 12:18 PM
#3
Re: Access Child control
Something like this
Code:
For Each myTabPages As TabPage In TabControl1.TabPages
'get controls on the page
Dim ctrl As Control = myTabPages.GetNextControl(myTabPages, True)
Do Until ctrl Is Nothing
Debug.WriteLine(ctrl.Name)
If TypeOf ctrl Is RichTextBox Then 'is it a RichTextBox
'yes
Dim iDocument As RichTextBox = DirectCast(ctrl, RichTextBox)
If iDocument.Modified Then
'save code here
End If
End If
ctrl = myTabPages.GetNextControl(ctrl, True)
Loop
Next
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|