Generalized Drag/Drop Event Behavior

Knyquist

New member
Joined
May 15, 2013
Messages
4
Programming Experience
1-3
Hello!

I believe this is a very basic question on VB.net and OOP But I think it will help me to trigger a deeper understanding of what's going on.
I'm essentially new to OOP and VB.net and most probably my question is not so well formulated, sorry for that.

So, I have implemented an easy Drag/Drop event handling between two Listboxes in Form1.vb.
As for now it operates only between the two listboxes that are specified in the "Handles" list.
but I would like to make it a more general code such that it can operate between any couple of listboxes that I'm interested to
so that I don't need to duplicate the code when I add another couple of listboxes in Form1.vb [design].

I'm aware that I should use some sort of Class to do this, but than, how should it be structured?
I'm referring especially to the events: let's say the code I've written uses subs that handle myListbox1.DragOver myListbox2.DragOver myListbox1.DragDrop... etc..
How do I structure the class so that to be sensible to events that occur between a custom couple of Listboxes?
Do I also need for example to add methods in order to specify the names of the listboxes between which I want the behavior to happen?

What I'm asking could be just also a hint that will let me understand which direction I should take to write my code.
I'm remaining very general with my questions but I can go deeper or add code as needed.

Thanks and Best Regards.
 
To add event handlers you either need a WithEvents variable and use the Handles clause on handler method, or use the AddHandler statement. Either can be used to dynamically handle events for objects at runtime, because you can dynamically assign any object to the WithEvents variable, and you can specify any object for AddHandler statement. The WithEvents variable naturally has the limitation that it can only refer to a single object at any given time.

If you use AddHandler statement you must take care to use RemoveHandler statement before releasing the object, if not it will not be garbage collected. The WithEvents variable can be dereferenced and all its related event handlers will also be released.
 
Thanks for the tip, I have used WithEvents and it seems to work.

By the way, I have used this kind of structure:

VB.NET:
Public Class Class1    
    Private WithEvents InternalBtn As Button

    Public Sub setBtnRef(ByRef Ref As Button)
        InternalBtn = Ref
    End Sub


    Private Sub Btn_Click(sender As Object, e As EventArgs) Handles InternalBtn.Click
        MessageBox.Show("Test")
    End Sub
End Class

VB.NET:
Public Class Form1

    Dim Obj As New Class1


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Obj.setBtnRef(Button1)
    End Sub


End Class

Is it correct to use a method to pass the reference of the button?
Is there a better way?
How could I check whether the code passes at least a valid button reference?

Thanks!
 
Properties is generally used to get/set class fields (private variables) that represent object state. Methods usually for processing.

ByRef doesn't mean what you think it does, change that to ByVal for all future. Actually in newer VB versions passing mechanism is omitted by default, and that means ByVal.

That you have declared the parameter as type Button means that only a reference to a Button object can be passed, or a null reference (Nothing). If you had used a more general type like Control and wanted to allow only specific types of controls then you would need to validate what control object was actually passed, and that is something that would be done in property setter.
 
Ok so, is it correct to say that in VB when I pass an object ByVal I'm passing a "copy" of its reference?
Indeed I originally passed the object ByRef because I was worried about creating another copy of the same object.
And that would also explain why there is no reason to pass ByRef something that is already a reference, right?

I agree also for the rest. I don't think I will need to go more in detail for now.
As soon as I have it working, I'll post the last "structure" as a matter of completion.

Thanks for now!
 
I understand it's very basic stuff but maybe it still can help someone.

So in the end it's like that I guess..

VB.NET:
Public Class Class1    

    Private WithEvents InternalBtn As Button


    Public Property BtnRef() As Button
        Get
            Return InternalBtn
        End Get
        Set(value As Button)
            InternalBtn = value
        End Set
    End Property


    Private Sub Btn_Click(sender As Object, e As EventArgs) Handles InternalBtn.Click
        MessageBox.Show("Test")
    End Sub

End Class

VB.NET:
Public Class Form1

    Dim Obj As New Class1


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Obj.BtnRef = Button1
    End Sub

End Class
 

Latest posts

Back
Top