VB.NET: Dynamically get a user control's parent tab's index

Answered VB.NET: Dynamically get a user control's parent tab's index

  • Saturday, April 14, 2012 9:54 PM
     
     

    I have a user control that is created within a tab control every time a user creates a new tab, so each tab on a tab control contains a new instance of the user control.  From within the user control I would like to get the parent tab's index, so that I can dynamically alter properties of the tab, such as the tab's title.

    Any ideas?

    Thanks
    Aaron

All Replies

  • Saturday, April 14, 2012 10:22 PM
     
      Has Code

    You have not provided any code, so it is difficult to guess what problem you might be having in accessing the parent of the control.  You need to use the reference that you created in the control that allowed you to access the control's tabpage so that you know which control was selected and the processing associated with that control, but you haven't indicated how you are doing that.  This example uses the control's parent property to identify the tab page. Your situation may be different, and of course you will be using different controls.

        Private Sub btnAddPage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddPage.Click
            Dim T As New TabPage
            T.Name = "TabPage" & TabControl1.TabPages.Count.ToString
            Dim B As New Button
            B.Parent = T
            T.Controls.Add(B)
            AddHandler B.Click, AddressOf Button_Click
            TabControl1.TabPages.Add(T)
        End Sub
    
        Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim B As Button = CType(sender, Button)
            B.Parent.Text = Now.ToString
        End Sub

  • Saturday, April 14, 2012 10:37 PM
     
      Has Code

    Thanks for that.

    I adapted that for what I am doing, shown below:

    CType(sender, Form).Parent.Text = "Text"

    Am I doing that wrong because I am getting the error:

    Unable to cast object of type 'System.Windows.Forms.WebBrowser' to type 'System.Windows.Forms.Form'.

    The above code is contained within a user control, and I am trying to change the text of the parent tab that contains the user control.

    Any ideas?

    Thanks
    Aaron

  • Saturday, April 14, 2012 10:39 PM
     
      Has Code

    Any ideas?  Don't do it that way.  At least do not let a child control alter it's parent.  The best practice for communicating between controls is the following.

    1.  Child to Parent - Child fires an event to which the Parent subscribes.

    2.  Parent to Child - Parent calls Subs/Functions in the Child.

    3.  Child to Child - Combines the above two approaches in a separate class.  Instantiate the class, and give each Child a reference to the instance.  Each Child subscribes to events in the shared instance to recieve information.  Each Child calls Subs/Functions in the shared instance to send information.

    Your scenario would involve chaining together events to be handled by the custom TabControl's parent, which would subscribe to it's custom event.  You would want to declare your own custom EventArgs class, instead of using System.EventArgs like I did.

    Public Class UserControl1
    
        Public Event DataTransmitted As EventHandler(Of EventArgs)
    
        Private Sub OnDataTransmitted(ByVal e As EventArgs)
            RaiseEvent DataTransmitted(Me, e)
        End Sub
    
    End Class

    Here's what a custom TabControl class could like, which simply retransmits the event to its' own subscribers.

    Public Class MyTabControl : Inherits TabControl
    
        WithEvents userControl As UserControl1
    
        Private Sub userControl_DataTransmitted(ByVal sender As Object, ByVal e As EventArgs) Handles userControl.DataTransmitted
            ' receive data as a Property of EventArgs parameter
            Me.OnDataTransmitted(e)
        End Sub
    
        Public Event DataTransmitted As EventHandler(Of EventArgs)
    
        Private Sub OnDataTransmitted(ByVal e As EventArgs)
            RaiseEvent DataTransmitted(Me, e)
        End Sub
    
    End Class

    Rudy  =8^D

    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

  • Saturday, April 14, 2012 10:45 PM
     
      Has Code

    Check that.  We cross posted.  When the custom TabControl handles the event from the UserControl, simply change the text property in the handler instead of retransmitting the event to a parent control.

    You can use the code I posted.  Assign the text to the sender object parameter, instead of 'Me'.  Cast the sender to text in the event handler.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    EDIT:  Discard that code.

    Public Class MyTabPage : Inherits TabPage
    
        WithEvents userControl As UserControl1
    
        Private Sub userControl_DataTransmitted(ByVal sender As Object, ByVal e As EventArgs) Handles UserControl.DataTransmitted
            Me.Text = CType(sender, String)
        End Sub
    
    End Class
    
    
    Public Class UserControl1
    
        Public Event DataTransmitted As EventHandler(Of EventArgs)
    
        Private Sub OnDataTransmitted(ByVal e As EventArgs)
            RaiseEvent DataTransmitted("Title", e)
        End Sub
    
    End Class

    • Edited by Rudedog2MVP Saturday, April 14, 2012 10:46 PM
    • Edited by Rudedog2MVP Saturday, April 14, 2012 11:00 PM
    •  
  • Saturday, April 14, 2012 11:12 PM
     
     

    If you are getting that error then you are doing something quite different than you have described, or the code that I provided.

    Show the code that you are using to create the control in the tab page and update the parent property, like I did in the example.   Either you are not setting the parent property correctly, or you are accessing the wrong control to update the text.

  • Saturday, April 14, 2012 11:21 PM
     
      Has Code

    Thank you for your responses, when the a new tab is created I use te following:

    Dim NewTab As New TabPage

    TabPage being the user control, and this code to listen for the navigation event being raised by the web browser control contained within the user control (TabPage):

    AddHandler NewTab.WebBrowser.Navigating, AddressOf WebBrowser_Navigating

    I then add this code to display a message box ever time a web browser is navigating:

        Private Sub WebBrowser_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs)
            MsgBox("")
        End Sub
    

    This works fine, instead of displaying a message box, I want to be able to change the title of the tab that contians the user control, that contains that web browser control that raised the navigating event.

    I still am unsure as to how I would do that.

    Thanks
    Aaron

  • Saturday, April 14, 2012 11:40 PM
     
      Has Code

    You lost me.  A "user control" is a custom control, similar to the class that I posted.  My custom TabPage has the control already built into it.  Try using this TabPage and this WebBrowser.

    Public Class MyWebBrowser : Inherits WebBrowser
    
        Public Event DataTransmitted As EventHandler(Of EventArgs)
    
        Private Sub OnDataTransmitted(ByVal e As EventArgs)
            RaiseEvent DataTransmitted("Title", e) 'sends the text
        End Sub
    
        Protected Overrides Sub OnNavigated(e As System.Windows.Forms.WebBrowserNavigatedEventArgs)
            Me.OnDataTransmitted(New EventArgs())
            MyBase.OnNavigated(e)
        End Sub
    
    End Class
    
    '''''''''''''''''''''''''''''''
    
    Public Class MyTabPage : Inherits TabPage
    
        WithEvents webBrowser As New MyWebBrowser()
    
        Private Sub webBrowser_DataTransmitted(ByVal sender As Object, ByVal e As EventArgs) Handles webBrowser.DataTransmitted
            Me.Text = CType(sender, String)
        End Sub
    
    End Class

    [EDIT]  Your posted code is lacking in context. I guess all of it appears somewhere in your Form class.


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/


    • Edited by Rudedog2MVP Saturday, April 14, 2012 11:56 PM no context
    •  
  • Saturday, April 14, 2012 11:55 PM
     
     

    That code creates the new tab page. The addhandler doesn't have anything to do with the issue - that is related to your webbrowser navigating.

    When you create a new tab page, you are also adding your user control to the tab page, but you haven't included any of that code.  That is the first part of the code I posted, except that I used a button instead of your user control.  When I created that button I did two things:
     - I set the parent proeprty of the new control.   That is critical - it's what makes the subsequent code work.
    - I attached an event handler to the new control.  This was only so that I could test the process.  I have no idea whether or not your user control needs events, or what event you will be using to update the text of the tab page becasue you have not provided any of that detail.

    You have to do the first of those steps when you create your user control and add it to the new tab page. Then, when you want to update the text of the tab page, you will use the parent property of the control to access the tab page instance. You cannot do that from a webbrowser navigating event - you have to do it from some event that is associated with the control that you added to the tabpage.

  • Saturday, April 14, 2012 11:59 PM
     
      Has Code

    Thanks for that.

    I adapted that for what I am doing, shown below:

    CType(sender, Form).Parent.Text = "Text"

    Am I doing that wrong because I am getting the error:

    Unable to cast object of type 'System.Windows.Forms.WebBrowser' to type 'System.Windows.Forms.Form'.

    The above code is contained within a user control, and I am trying to change the text of the parent tab that contains the user control.

    Any ideas?

    Thanks
    Aaron

    CType(sender, WebBrowser).Parent.Text = "Text"

    Note the change from "Form" to "WebBrowser".  I am going to guess that line of code appears in your Navigating event handler.


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/


    • Edited by Rudedog2MVP Sunday, April 15, 2012 12:00 AM
    •  
  • Sunday, April 15, 2012 12:26 AM
     
     

    Thanks for that.  I don't understand how to apply the code above to my code.

    Aaron

  • Sunday, April 15, 2012 12:55 AM
     
     

    Thanks for that.  I don't understand how to apply the code above to my code.

    Aaron

    Probably a sign that we do not understand what you are doing.  Your code snippets lack context. 

    Did my last snippet fix your casting exception?

    Are you using custom user controls, or tabpages like you posted?

    Maybe it is time to post code of what you are doing.  We have indicated that your code snippets lack context.  Posting a single line of code can be meaningless most of the time.  We have no idea where your posted appears in your code.  Stating which class would be a big help.

    Did I guess correctly when I assumed your Navigating event handler is in your Form class?

    You indicated that you add controls dynamically, but didn't post code of how you do that.  You should always have a test project to test new code before you add it to your real application.  That way you do not run the risk of breaking your main application.  Test new code until you understand it, then add it to your application.

    You state that you do not know how to use the code that I just posted?  All I did was change one word in your line of code.  ???


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/


    • Edited by Rudedog2MVP Sunday, April 15, 2012 12:56 AM
    •  
  • Sunday, April 15, 2012 1:13 AM
     
      Has Code

    Hi, the control does get a parent control set:

    Dim NewTab As New TabPage
    TabControl.SelectedTab.Controls.Add(NewTab)

    I'll try phrasing this another way, if at all possible when the navigating even is raised by the web browser control that sits in the user/custom control I would like to find out what the index of the tab page is (the tab page being where the user/custom control sits).  That way when I have the index I can set the text of that tab (which I also can't seem to figure out how to do).

    Aaron

  • Sunday, April 15, 2012 1:34 AM
     
      Has Code

    Hi, the control does get a parent control set:

    Dim NewTab As New TabPage
    TabControl.SelectedTab.Controls.Add(NewTab)

    I'll try phrasing this another way, if at all possible when the navigating even is raised by the web browser control that sits in the user/custom control I would like to find out what the index of the tab page is (the tab page being where the user/custom control sits).  That way when I have the index I can set the text of that tab (which I also can't seem to figure out how to do).

    Aaron

    No.   You have multiple tabpages and multiple webbrowsers, and you don't know how to figure out which browser navigated on which page.  We don't know, either.  We don't know how everything is connected together, or even what is connected to what.

    Where is the WebBrowser control in your posted code?  Where is the "user/custom" control in your posted code?

    I've already asked several questions and you completely ignored them.  This is your last chance with me today.  Try again tomorrow.

    What 'user/custom' control are you talking about?  It sounds like your "user/custom" control is a WebBrowser.  Is it?

    Why do you need the index of the TabPage?  The Parent of the "user/custom/webbrowser" control is the TabPage where the control appears.

    When you add the "user/custom/webbrowser" control to your TabPage, isn't that the TabPage that you are trying to modify?

    I think you are trying to frame the answer to your question, but the solutions that have been suggested do not fit your idea of how to do it.  You are looking for a round answer, but  we keep presenting square ones, which do not fit into your solution.  That's what happens when you post single lines of code without context.  We are forced to give you broad, generalized solutions and leave it up to you to connect the dots.  If you want specific answers, then ask specific questions detailing the EXACT issue.  If you do not connect the dots for us, it is impossible for us to connect the dots in a solution.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/


    • Edited by Rudedog2MVP Sunday, April 15, 2012 1:36 AM
    •  
  • Sunday, April 15, 2012 1:44 AM
     
     

    You haven't posted any code indicating how you are setting the parent.   It's not clear whether you are using a user control or a custom control - they are very different.

    The process I described and the example I gave involved setting the parent property of the control that was being added to the tab page.  That property can then be used to set the text property of that parent tab page.

    If the webbrowser control sits inside the custom control / user control that sits on the tab page, and if you want to update the tab page text from an event raised for the webbrowser control, then you have another level of parent / child that you need to work through. How you do that depends on how you have constructed the user control or the custom control.  For a custom control it would be trivial - for a user control it might be more difficult.

    If it's a custom control, simply repeat the code to set the webbrowser parent when it's created. This is good practice anyway for contained controls.

    If it's a user control you could check if the webbrowser has a reference to its parent and use that:

            Dim W As WebBrowser = CType(sender, WebBrowser)
            W
    .Parent.Parent.Text = Now.ToString
    If the user control doesn't automatically set the parent property of its contained controls, the problem becoems more complex because you start to be involved in references to controls that are not part of the current class.

  • Sunday, April 15, 2012 1:51 AM
     
     

    Thanks, Acamar.  I think you said it much better than I did. 

    I've already I posted code for one solution to handling controls nested in a usercontrol and passing information between the classes.

    I've already posted code for a custom control and how to handle the passing of information between the classes.

    Now we wait.


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

  • Sunday, April 15, 2012 3:20 PM
     
      Has Code

    Hi, the code that was just posed worked great, except I had to alter it a little to:

    Dim W As Windows.Forms.WebBrowser = CType(sender, Windows.Forms.WebBrowser)
    W.Parent.Parent.Text = "Loading..."
    

    I think it's something to do with a change in versions of the framework.

    And, it's a user control that I created.

    I would like to also do something similar with the DocumentCompleted event of the WebBrowser control I tried the following, although I would like to display the title of the document in the tab, and neither sender or e contain the information:

    Private Sub WebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
        Dim W As Windows.Forms.WebBrowser = CType(sender, Windows.Forms.WebBrowser)
        W.Parent.Parent.Text = (sender./e.)
    End Sub
    

    Any ideas?

    Thanks
    Aaron

  • Sunday, April 15, 2012 3:23 PM
     
     Answered Has Code

    Sorry, I just got it:

    W.Parent.Parent.Text = W.DocumentTitle

    Thanks for all you help.

    Aaron