neodatatype Posted November 16, 2003 Posted November 16, 2003 Hi all! I tryed to add an event to all Controls in my form, but I cannot do it :( This is what I did: Public Sub SetMyeventToAll(ByVal AddressOfMyEvent As [Delegate]) Dim c As Control For Each c In Me.Controls Try AddHandler c.MyEvent, AddressOfMyEvent Catch ex As Exception ' this control does not have a MyEvent event End Try Next End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click SetMyeventToAll(AddressOf MyEventSub) End Sub Private Sub MyEventSub(ByVal sender As Object, ByVal e As KeyPressEventArgs) MessageBox.Show(e.KeyChar) End Sub You can replace the MyEvent with any event to do a test, such as KeyPress. I got an error ad SetMyeventToAll(AddressOf MyEventSub) that I cannot understand. Is there a way to add a event to all my controls by a reusable sub? Thank you Quote > NeoDataType.net < Try my Free .Net Reporting Tool!
Moderators Robby Posted November 16, 2003 Moderators Posted November 16, 2003 Yes and No, you can add an event handler per control type, each control has it's own signature and events. Check for the TypeOf control inside your loop... If TypeOf x Is TextBox Then 'add handler here end if Quote Visit...Bassic Software
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.