Groups | Blog | Home
all groups > vb.net > march 2004 >

vb.net : reusing sub routines


dotnetboards NO[at]SPAM sevenfoot-dot-com.no-spam.invalid
3/19/2004 11:09:34 PM
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


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
John Smith
3/20/2004 5:15:46 AM
.... Handles Panel0.DragEnter, Panel1.DragEnter, Panel2.DragEnter, ...

or use AddHandler ...


[quoted text, click to view]
Jay B. Harlow [MVP - Outlook]
3/20/2004 9:11:56 AM
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:

[quoted text, click to view]
If TypeOf sender Is Panel Then
Dim thePanel As Panel = DirectCast(sender, Panel)
thePanel.BackGround = Color.Red
End If

...

[quoted text, click to view]
If TypeOf sender Is Panel Then
Dim thePanel As Panel = DirectCast(sender, Panel)
thePanel.BackGround = System.Colors.Control
End If

...

[quoted text, click to view]

Hope this helps
Jay

"sevenfoot" <dotnetboards@sevenfoot-dot-com.no-spam.invalid> wrote in
message news:405bd20e$4_2@Usenet.com...
[quoted text, click to view]

AddThis Social Bookmark Button