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.

Dani AI

Generated

Good news: the approach is fine — the issue is that sender is typed as Object, so you must cast it to the control type you need before accessing properties or passing it to a method that expects a PictureBox. was right to suggest casting. With Option Strict On you must make that cast explicitly; with Option Strict Off VB may try late binding and either fail at runtime or behave unpredictably.

A safe, compact VB pattern is to cast sender with TryCast and bail out if the cast fails:

Private Sub Shared_DragDrop(sender As Object, e As DragEventArgs) _
    Handles picA1.DragDrop, picA2.DragDrop, picA3.DragDrop

    Dim target As PictureBox = TryCast(sender, PictureBox)
    If target Is Nothing Then Exit Sub

    MovePiece(e, picSource, target)
End Sub

If you want to avoid a global picSource, start the drag with the source control as the data and read it back inside the drop handler. Example pattern:

' when starting the drag:
sourcePictureBox.DoDragDrop(sourcePictureBox, DragDropEffects.Move)

' in the drop handler:
Dim src As PictureBox = TryCast(e.Data.GetData(GetType(PictureBox)), PictureBox)
If src IsNot Nothing Then
    MovePiece(e, src, target)
End If

Extra tips: use TryCast to avoid exceptions (use DirectCast only if you know the type is correct), or change MovePiece to accept Control if you genuinely need to support multiple control types. If a cast still fails, inspect Debug.WriteLine(sender.GetType().FullName) to see what actual type raised the event. This resolves the type-mismatch that caused MovePiece(e, picSource, sender) to not work as expected for .

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.