Results 1 to 3 of 3

Thread: Access Child control

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    40

    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

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,289

    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 -

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width