3

I have a groupbox containing a lot of Checkboxes - only Checkboxes.

Is there a simple/fast way to handle the same event coming from different controls?

I know I can write a single sub and let it handle all the Events, but it's really time-consuming to write.

Using Visual Studio 2012

Ciacciu
  • 137
  • 8
  • I would love to see this! anyway, how about creating a method called *DoSomething()* and for every *SelectedIndexChanged* event, just call *DoSomething();*??? – jbutler483 Sep 10 '14 at 12:42
  • I guess it would work, but it's just as time consuming as the other option :) – Ciacciu Sep 10 '14 at 12:48
  • don't forget, you can have the designer view on half the screen and half the screen code. Double clicking on one, pasting the *DoSomething()* method on the other half. It's as fast as it's gonna get i'm afraid :( – jbutler483 Sep 10 '14 at 12:51

2 Answers2

1

If your time concern is about writing and managing a large Handles clause, you can simply loop through the GroupBox's Controls collection upon construction of your UserControl/Form and wire up each event on each CheckBox, like so:

Imports System.Linq

Public Sub New()
    InitializeComponent()

    For Each chkBox As CheckBox In yourGroupBoxVariable.Controls.OfType(Of CheckBox)()
        AddHandler chkBox.CheckStateChanged, AddressOf YourCheckStateChangedHandlerMethod
    Next
End Sub

Private Sub YourCheckStateChangedHandlerMethod(ByVal sender As Object, ByVal e As EventArgs)
    ' Your handler code for the checkboxes
End Sub

This leverages LINQ's OfType Enumerable extension to filter down all child controls of the GroupBox to those of type CheckBox.

avanek
  • 1,649
  • 12
  • 20
  • I don't know why someone would down vote this answer, but it seems very useful when handling events. Upvoted the answer. – Mr.J Feb 11 '17 at 07:23
1

Another possibility is to create a custom GroupBox, i.e. deriving from GroupBox and exposing the event yourself:

Public Class CheckboxGroup
  Inherits GroupBox

  Public Event CheckboxChanged(source As CheckBox, e As EventArgs)

  Protected Overrides Sub OnControlAdded(e As ControlEventArgs)
    ' this method is called everytime a checkbox is added
    If TypeOf e.Control Is CheckBox Then
      Dim chk As CheckBox = DirectCast(e.Control, CheckBox)
      AddHandler chk.CheckedChanged, AddressOf AllCheckedChange
    End If
  End Sub

  Private Sub AllCheckedChange(source As Object, e As EventArgs)
    If TypeOf source Is CheckBox Then
      Dim chk As CheckBox = DirectCast(source, CheckBox)
      RaiseEvent CheckboxChanged(chk, e)
    End If
  End Sub

End Class

You can then attach to the event in the Form like:

  Private Sub CheckboxChanged(source As CheckBox, e As EventArgs) Handles gb.CheckboxChanged
    MsgBox(source.Text & " to " & source.Checked)
  End Sub

Advantage: you can never miss to add an event handler to a CheckBox, even if it is created dynamically.

WeSt
  • 2,628
  • 5
  • 22
  • 37
  • Very interesting, thank you! [Avanek](http://stackoverflow.com/users/357625/avanek)'s answer isbetter suited for the situation I'm in, but this IS indeed very useful! – Ciacciu Sep 10 '14 at 13:05