reusing sub routines

S

sevenfoot

how can I make one subroutine to handle events from different objects?
For instance I have six panels that do the same thing when something
is dragged into them, I'd like to have one subroutine to do this
rather than have six.

Private Sub Panel0_DragEnter(ByVal sender
As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles
Panel0.DragEnter
If (e.Data.GetDataPresent(DataFormats.Text))
Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub

Private Sub Panel1_DragEnter(ByVal sender As Object, ByVal e
As System.Windows.Forms.DragEventArgs) Handles Panel1.DragEnter
If (e.Data.GetDataPresent(DataFormats.Text))
Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub

here's two of the subroutines, the only thing that changes is the name
of the panel.


thanks,
Jason
 
J

John Smith

.... Handles Panel0.DragEnter, Panel1.DragEnter, Panel2.DragEnter, ...

or use AddHandler ...

how can I make one subroutine to handle events from different objects?
For instance I have six panels that do the same thing when something
is dragged into them, I'd like to have one subroutine to do this
rather than have six.

Private Sub Panel0_DragEnter(ByVal sender
As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles
Panel0.DragEnter
If (e.Data.GetDataPresent(DataFormats.Text))
Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub

Private Sub Panel1_DragEnter(ByVal sender As Object, ByVal e
As System.Windows.Forms.DragEventArgs) Handles Panel1.DragEnter
If (e.Data.GetDataPresent(DataFormats.Text))
Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub

here's two of the subroutines, the only thing that changes is the name
of the panel.


thanks,
Jason
 
J

Jay B. Harlow [MVP - Outlook]

Jason,
As John stated, you can list multiple objects in the Handles clause or use
AddHandler.

The other thing that is useful to know is the "sender" parameter to the
event handler is the object raising the event.

For example you can use the sender to change the background color of the
panel when you are dragging over that panel, with something like:
Private Sub Panel0_DragEnter(ByVal sender
As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles
Panel0.DragEnter, Panel1.DragEnter
If TypeOf sender Is Panel Then
Dim thePanel As Panel = DirectCast(sender, Panel)
thePanel.BackGround = Color.Red
End If

...
Private Sub Panel0_DragLeave(ByVal sender
As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles
Panel0.DragLeave, Panel1.DragLeave
If TypeOf sender Is Panel Then
Dim thePanel As Panel = DirectCast(sender, Panel)
thePanel.BackGround = System.Colors.Control
End If

...

Hope this helps
Jay

sevenfoot said:
how can I make one subroutine to handle events from different objects?
For instance I have six panels that do the same thing when something
is dragged into them, I'd like to have one subroutine to do this
rather than have six.

Private Sub Panel0_DragEnter(ByVal sender
As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles
Panel0.DragEnter
If (e.Data.GetDataPresent(DataFormats.Text))
Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub

Private Sub Panel1_DragEnter(ByVal sender As Object, ByVal e
As System.Windows.Forms.DragEventArgs) Handles Panel1.DragEnter
If (e.Data.GetDataPresent(DataFormats.Text))
Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub

here's two of the subroutines, the only thing that changes is the name
of the panel.


thanks,
Jason
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top