Hi all,
I have seen a few potential solutions on this one, but none of them seem to make sense to me. I was hoping that as well as suggesting code, someone might be able to explain in laymans terms what was going on?
I have buttons on my main form (btn_1_1, btn_2_1, btn_2_2, etc.) which represent different parts of a machine. When clicked, I would like to capture the name of the button clicked and pass the variable to a function stored in a module.
The passing of the variable is easy. I can take care of that. However, I have well over 100 buttons and i'm not keen on creating a sub for each button to handle the onClick event. I need some sort of Global Button Click Event Handler that will listen for button clicks and provide the name of a clicked button.
Suggestions so far include adding all button controls to a collection (though I lost it from there), and also a VB.net solution (as below) which doesn't play nicely in Access 2010.
Code:
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each Control As Control In Me.Controls
If Control.GetType = GetType(TextBox) Then
If Control.Name.ToString.StartsWith("Lbl") Then
Dim TextBox As TextBox = DirectCast(Control, TextBox)
AddHandler TextBox.TextChanged, AddressOf TextBoxTextChanged_Handler
End If
End If
If Control.GetType = GetType(ComboBox) Then
Dim ComboBox As ComboBox = DirectCast(Control, ComboBox)
AddHandler ComboBox.SelectedIndexChanged, AddressOf ComboboxSelectedIndexChanged_Handler
End If
Next
End Sub
Any help would be appreciated.