VB.Net 2005: abstracting click events?

k3n51mm

Active member
Joined
Mar 7, 2007
Messages
40
Programming Experience
3-5
I have two click event handler Subs in a vb.net 2005 desktop application that are exactly the same across many forms, except that they are fired by the Click events of different controls on each form, and they set the text value of a textbox that is differently named in each form.

I didn't want to use a Module.vb file, since that is not object-oriented (right?). Otherwise though, is there any way I can bring these Subs into an external place like a header file that I can include in all the forms I need them in? Right now I am cutting and pasting what feels like a lot of code, and it seems to me I should be able to abstract them into something reusable. but nothing comes to mind when I try to figure out how.
VB.NET:
'EXAMPLE
'The UpdateScore subroutine below sets a textbox value as the result of checkboxes being checked/unchecked.
'It is exactly the same in 30 different forms in this app, except for
' 1) the multiple click events that fire it: cbExpCfgDmg.Click, etc.
'and 2) the textbox whose value it sets: txtCWMCfg.Text
'-------------------------------------------------------------------
Friend Sub UpdateScore(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbExpCfgDmg.Click, _
cbMixUXO.Click, cbExpCfgUndmg.Click, cbBulk.Click, cbK941.Click, cbCAIS.Click, cbEvCWM.Click
  If sender.checked = True Then
    If Convert.ToInt32(sender.text) > Convert.ToInt32(txtCWMCfg.Text) Then
      txtCWMCfg.Text = sender.text
    End If
  ElseIf sender.checked = False Then
    Dim cb As CheckBox
    Dim arrCbValues As New ArrayList
    For Each ctrl As Control In gb1.Controls
      If TypeOf ctrl Is CheckBox Then
        cb = CType(ctrl, CheckBox)
        If cb.Checked = True Then
          arrCbValues.Add(Convert.ToInt32(cb.Text))
        End If
      End If
    Next ctrl
    'find checked boxes
    If arrCbValues.Count > 0 Then
      'if max value in checked boxes is greater than textbox value, replace it
      If FindMax(arrCbValues) >= 0 Then
        txtCWMCfg.Text = FindMax(arrCbValues)
      End If
    Else
      txtCWMCfg.Text = "0"
    End If
  End If
End Sub
'-----------------------------------------------------
'END EXAMPLE
 
Last edited:
Using a Module looks like the best option, something like this:
VB.NET:
Public Module commons

    Public tb As TextBox
    Public controls As Control.ControlCollection

    Public Sub controls_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        'just displaying what to work with here:
        Dim current As Control = sender
        MsgBox(current.Text)
        MsgBox(tb.Text)
        For Each c As Control In controls

        Next
    End Sub
End Module
Each form that uses it must set the Textbox and Controls references and add handlers for the control events. (hopefully these 30 forms are used in succession and not simultaneously, which would complicate the references somewhat.)
VB.NET:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        commons.tb = Me.TextBox1
        commons.controls = Me.Controls
        AddHandler Button1.Click, AddressOf commons.controls_Click
        AddHandler CheckBox1.Click, AddressOf commons.controls_Click
        AddHandler CheckBox2.Click, AddressOf commons.controls_Click
        AddHandler CheckBox3.Click, AddressOf commons.controls_Click
        AddHandler CheckBox4.Click, AddressOf commons.controls_Click

    End Sub
 
If it's the same in 30 different forms, does it mean the 30 forms have a related purpose?
If so, you can create one form as a base class for all these, implement the common functionality, then in every inherited form you can probably set the click handlers in the designer rather than writing the addhandler code yourself..
 
Back
Top