Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

> NeoDataType.net <

Try my Free .Net Reporting Tool!

  • Moderators
Posted

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

Visit...Bassic Software

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...