Results 1 to 7 of 7

Thread: Interacting with custom made controls

  1. #1

    Thread Starter
    Frenzied Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    1,168

    Interacting with custom made controls

    I have a custom control that has a tab control that when a new tab is created it also has a toolstrip with a few buttons and a text box. I want to interact with those as they exist e.g selected index - and as needed from the form (Very important) - one of the libraries I am using doesn't work well with button interactions outside of the parent form. And I cannot have the control placed on the tabs as part of the custom hardcode either as it does not work correctly (and known issue they basically said it was unsupported lmao)

    I have also slimmed the control to be WITHOUT the tab control and just try and add this on the fly, but that too is failing.


    My brain is weak from not really doing VB.NET for a while so looking for inputs on solutions.

    I have tried a simple address e.g.

    Code:
       Private Sub ButtonFavorites_Click(sender As Object, e As EventArgs) Handles BrowserURLBar1.ButtonFavorites.Click
            ' Handle the Favorites button click event
            MessageBox.Show("Favorites button clicked!")
        End Sub
    Can someone jar my brain and explain how to interact with buttons on a custom control? It is probably easier than I am making it.
    Disclaimer: When code is given for example - it is merely a example.




    Unless said otherwise indicated - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,806

    Re: Interacting with custom made controls

    You don't interact with child controls. If you have created what sounds like a user control, all interact from the outside should be done with that. Any members of child controls that you need to work with from the outside should be exposed by what I call pass-through members. For instance, if you had a child TextBox then you might add a property to the parent control that exposed the Text property:
    vb.net Code:
    1. Public Property TextBox1Text As String
    2.     Get
    3.         Return TextBox1.Text
    4.     End Get
    5.     Set
    6.         TextBox1.Text = value
    7.     End Set
    8. End Property
    In the case of a Button, it is the Click event that you want to pass through:
    vb.net Code:
    1. Public Event Button1Click As EventHandler
    2.  
    3. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    4.     RaiseEvent Button1Click(Me, EventArgs.Empty)
    5. End Sub

  3. #3

    Thread Starter
    Frenzied Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    1,168

    Re: Interacting with custom made controls

    Thanks that seems to send me in the right direction. I have started to also add a new tab button, by painting the button on the control, it seems that it works in the designer but not at run time. May start a new thread for that issue as it seems to be a whole new slew of what ifs on a solution.
    Disclaimer: When code is given for example - it is merely a example.




    Unless said otherwise indicated - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,288

    Re: Interacting with custom made controls

    Here’s an example that might help…

    https://www.vbforums.com/showthread....ded-TabControl

  5. #5
    New Member SKPTitleLLC's Avatar
    Join Date
    Feb 2022
    Location
    United States
    Posts
    1

    Re: Interacting with custom made controls

    Interacting with custom-made controls in Visual Basic (VB) involves creating, manipulating, and responding to events of controls that you design or customize for your VB application. Here's a general overview of how to work with custom controls in VB:

    Create a Custom Control:

    You can create custom controls by either inheriting from existing controls (user controls) or building entirely new controls from scratch.
    Design the Custom Control:

    Use the Visual Studio Designer to design the appearance and behavior of your custom control.
    Customize properties, events, and methods as needed to achieve the desired functionality.
    Add Custom Control to a Form:

    In the VB.NET designer, you can drag and drop your custom control onto a Windows Form or a user control.
    Interact with the Custom Control:

    Set properties: You can set properties of your custom control programmatically to configure its appearance and behavior.
    Handle events: Register event handlers for the custom control's events. This allows you to respond to user actions or changes in the control's state.
    Call methods: Invoke methods of the custom control to trigger specific actions or behaviors.

  6. #6
    Addicted Member
    Join Date
    Jan 2022
    Posts
    204

    Re: Interacting with custom made controls

    You can also create Public methods or functions and pass arguments in. Here are some examples, obviously the controls in this example are just created for example and dont belong to any container so this wont work unless the controls are visible on a form/usercontrol. Just to give you an idea.

    Code:
    Public Class Class1
        Private _Class2 As New Class2
        Private Button1 As New Button
    
        Public Sub New()
            AddHandler Button1.Click, Sub()
                                          Dim SomeObj As New Object 'can be anything
                                          _Class2.SetButton1Stuff("Your Button Text", SomeObj)
    
    
                                          _Class2.ClickButton1("Your msg txt")
                                      End Sub
        End Sub
    End Class
    
    Public Class Class2
        Private Button1 As New Button
        Private Msg As String = String.Empty
    
        Public Sub New()
            AddHandler Button1.Click, Sub()
                                          MsgBox(Msg)
                                      End Sub
        End Sub
    
        Public Sub ClickButton1(ByVal MsgTxt As String)
            Msg = MsgTxt
            Button1.PerformClick()
        End Sub
    
        Public Sub SetButton1Stuff(Optional ButtonText As String = "Default Text", Optional _Tag As Object = Nothing)
            Button1.Text = ButtonText
            Button1.Tag = _Tag
        End Sub
    End Class

  7. #7
    Addicted Member
    Join Date
    Jan 2022
    Posts
    204

    Re: Interacting with custom made controls

    Quote Originally Posted by SKPTitleLLC View Post
    Interacting with custom-made controls in Visual Basic (VB) involves creating, manipulating, and responding to events of controls that you design or customize for your VB application. Here's a general overview of how to work with custom controls in VB:

    Create a Custom Control:

    You can create custom controls by either inheriting from existing controls (user controls) or building entirely new controls from scratch.
    Design the Custom Control:

    Use the Visual Studio Designer to design the appearance and behavior of your custom control.
    Customize properties, events, and methods as needed to achieve the desired functionality.
    Add Custom Control to a Form:

    In the VB.NET designer, you can drag and drop your custom control onto a Windows Form or a user control.
    Interact with the Custom Control:

    Set properties: You can set properties of your custom control programmatically to configure its appearance and behavior.
    Handle events: Register event handlers for the custom control's events. This allows you to respond to user actions or changes in the control's state.
    Call methods: Invoke methods of the custom control to trigger specific actions or behaviors.
    Would be nice to downvote this gpt response

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