Hi, i am having a bit of trouble condensing some basic drag and drop code and i am hoping that someone can help me. to reduce code i have created a subroutine to handle the proces, however i still have a large number of event handlers calling this process. I am aware that i can create my own event handler and make it handle a number of different objects. However the process requires a paramater pass of the initiating object, which i assumed was the sender object of the event handler, however i can not access any of the properties of the sender object. Can anyone please tell me how to do this.

Recommended Answers

All 4 Replies

You can handle the click event of many controls (TextBox, Buttons,... etc) with just one method what you need is to identify the sender (Who's?) Button or TextBox or what and then decide what to do!!

just to clarify the code i want to condense is

Private Sub picA1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles picA1.DragDrop
        MovePiece(e, picSource, picA1)
    End Sub

    Private Sub picA2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles picA2.DragDrop
        MovePiece(e, picSource, picA2)
    End Sub

    Private Sub picA3_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles picA3.DragDrop
        MovePiece(e, picSource, picA3)
    End Sub

and i think i can condense it to somthing like this

Private Sub PicTarget_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles picA1.DragDrop, picA2.DragDrop, picA3.DragDrop
        MovePiece(e, picSource, sender)
    End Sub

however i this does not work. Can anyone please help

if sender is Picture cast it
C# code I don't know VB.NET :$

PictureBox pic = (PictureBox)sender;
//hint: Exception would release if sender isn't PictureBox control

Full code

Private Sub PicTarget_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles picA1.DragDrop, picA2.DragDrop, picA3.DragDrop
PictureBox pic = (PictureBox)sender;
        MovePiece(e, picSource, sender)
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.