locked
Event Handler to apply to several combo boxes RRS feed

  • Question

  • Been using this code to handle drop down and other events:

    Private Sub cboVst_Mon_DropDown(sender As Object, e As EventArgs) Handles cboVst_Mon.DropDown,
            cboCalc_Type3.DropDown, cboTrans_Type3.DropDown, cboInt_Term3.DropDown
            
            ButtonState2()
        End Sub
     I got to believe that there is a simpler way to hand this.  Any suggestions?


    ISV using VB.net and SQL Server

    Monday, April 22, 2019 9:57 PM

Answers

  • Since your ComboBox controls are on panels the code will not work "as is" you need to add the following code module to your project.

    Public Module ControlExtensions
        ''' <summary>
        ''' Provides access to all controls on a form including
        ''' controls within containers e.g. panel and group-box, Panel etc.
        ''' </summary>
        ''' <typeparam name="T"></typeparam>
        ''' <param name="control"></param>
        ''' <returns></returns>
        <Runtime.CompilerServices.Extension>
        Public Iterator Function Descendants(Of T As Class)(control As Control) As IEnumerable(Of T)
            For Each child As Control In control.Controls
    
                Dim currentChild = TryCast(child, T)
                If currentChild IsNot Nothing Then
                    Yield currentChild
                End If
    
                If child.HasChildren Then
                    For Each descendant As T In child.Descendants(Of T)()
                        Yield descendant
                    Next
                End If
            Next
        End Function
    End Module
     

    Use it as follows

    Descendants(Of ComboBox).ToList().ForEach(
        Sub(currentComboBox)
    
            AddHandler currentComboBox.DropDown,
                 Sub(someComboBox As Object, a As EventArgs)
                     MessageBox.Show(CType(someComboBox, ComboBox).Name)
                 End Sub
        End Sub)
    
    I recommend placing the last code block in the Form Shown event, not the Form load event.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Jeff07 Tuesday, April 23, 2019 2:42 PM
    Monday, April 22, 2019 11:25 PM

All replies

  • Hello,

    This will perform the same as what you have given that the ComboBox controls are all on the form, not in a container such as a Panel and if this is the case I have a solution for this too.

    Controls.OfType(Of ComboBox).ToList.ForEach(
        Sub(currentComboBox)
    
            AddHandler currentComboBox.DropDown,
                  Sub(someComboBox As Object, a As EventArgs)
                      MessageBox.Show(CType(someComboBox, ComboBox).Name)
                  End Sub
        End Sub)


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Monday, April 22, 2019 10:21 PM
  • Thank you, thank you.

    All combo boxes are on the same form but contained in panels.  Does this make a difference?

    My code ButtonState2 would replace the line "MessageBox.show...." in your code?


    ISV using VB.net and SQL Server

    Monday, April 22, 2019 10:52 PM
  • Where would I place your code in project?


    ISV using VB.net and SQL Server

    Monday, April 22, 2019 11:06 PM
  • Since your ComboBox controls are on panels the code will not work "as is" you need to add the following code module to your project.

    Public Module ControlExtensions
        ''' <summary>
        ''' Provides access to all controls on a form including
        ''' controls within containers e.g. panel and group-box, Panel etc.
        ''' </summary>
        ''' <typeparam name="T"></typeparam>
        ''' <param name="control"></param>
        ''' <returns></returns>
        <Runtime.CompilerServices.Extension>
        Public Iterator Function Descendants(Of T As Class)(control As Control) As IEnumerable(Of T)
            For Each child As Control In control.Controls
    
                Dim currentChild = TryCast(child, T)
                If currentChild IsNot Nothing Then
                    Yield currentChild
                End If
    
                If child.HasChildren Then
                    For Each descendant As T In child.Descendants(Of T)()
                        Yield descendant
                    Next
                End If
            Next
        End Function
    End Module
     

    Use it as follows

    Descendants(Of ComboBox).ToList().ForEach(
        Sub(currentComboBox)
    
            AddHandler currentComboBox.DropDown,
                 Sub(someComboBox As Object, a As EventArgs)
                     MessageBox.Show(CType(someComboBox, ComboBox).Name)
                 End Sub
        End Sub)
    
    I recommend placing the last code block in the Form Shown event, not the Form load event.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Jeff07 Tuesday, April 23, 2019 2:42 PM
    Monday, April 22, 2019 11:25 PM