By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,357 Members | 1,021 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,357 IT Pros & Developers. It's quick & easy.

Ones more - DragDrop in VB.NET 2002

P: n/a
KS
In want to visualy drag a Button to a Label and when I depress the
mousebutton on top of the label I want to show some dato from the Button in
a MsgBox - that's my primary goal.

I have made a simple sample code - a form with a Button1, a Label1 and a
Timer1.

The Button1 has Tag="SomeData", the Label1 has AllowDrop=True and the Timer1
has an Interval=20.

Paste this code to your solution - from here:

Dim GripOffset As Point
Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
If e.Button = MouseButtons.Left Then
Timer1.Enabled = True
GripOffset.X = e.X
GripOffset.Y = e.Y
Button1.DoDragDrop(Button1.Tag, DragDropEffects.Copy)
End If
End Sub
Private Sub label1_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles Label1.DragDrop
If e.Data.GetData(DataFormats.Text).ToString = "SomeData" Then
Button1.Location = PointToClient(MousePosition)
Button1.BringToFront()
MessageBox.Show("Yes, it worked !")
Button1.Location = New Point(15, 50)
End If
End Sub
Private Sub Label1_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles Label1.DragEnter
If e.Data.GetDataPresent(DataFormats.Text) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Dim NPos As New Point()
NPos = Me.PointToClient(MousePosition)
NPos.Offset(-GripOffset.X, -GripOffset.Y)
Button1.Location = NPos
End Sub
' MouseUp never executes - why ?
' Is it disabled or by some other meens controled by the DoDragDrop in
MouseDown-event ?
Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
Timer1.Enabled = False
End Sub

' to here !!!!!!!!

Compiler and drag the Button1 to the Label1 - the Button is visualy
following the mouse but it wont drop at all - you have to right-click the
mouse and the usual 'DragDrop-system' with my MsgBox is NOT working.

Now - in the Button1_MouseDown-event comment out the Timer1.Enabled=True and
start again - now the 'DragDrop-system' with my MsgBox is IS working but the
Button1 is NOT visualy following the mouse but jumps to the Label1 when I
depress the mouseButton.

I think the latter solution works the best way so that you can comment out
all the Timer-related-code.

BUT in my application where I plan to use this 'DragDrop-functionality' I
can NOT reference the Button1 directly when I move it in the
Label1_DragDrop-event - that is - I can NOT use the "Button1.Location =
PointToClient(MousePosition)" - therefore:

How can I Drag and Drop an object as a whole unit (here a Button) so that I
in the Label1_DragDrop-event can work on that 'dropped object' and get
access to all it's properties etc. ?

In the Button1_MouseDown-event I actualy without any syntax error CAN write:

Button1.DoDragDrop(Button1, DragDropEffects.Copy) ' where 'the whole
object' Button1 is referenced !

But what shall I do in the DragEnter and DragDrop events to tell thats is
the whole object I want to work with ?

How can I do this ?

KS, Denmark
Jul 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
KS
I found out myself !

KS, Denmark

"KS" <ke************@os.dk> skrev i en meddelelse
news:%2******************@TK2MSFTNGP11.phx.gbl...
In want to visualy drag a Button to a Label and when I depress the
mousebutton on top of the label I want to show some dato from the Button in a MsgBox - that's my primary goal.

I have made a simple sample code - a form with a Button1, a Label1 and a
Timer1.

The Button1 has Tag="SomeData", the Label1 has AllowDrop=True and the Timer1 has an Interval=20.

Paste this code to your solution - from here:

Dim GripOffset As Point
Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
If e.Button = MouseButtons.Left Then
Timer1.Enabled = True
GripOffset.X = e.X
GripOffset.Y = e.Y
Button1.DoDragDrop(Button1.Tag, DragDropEffects.Copy)
End If
End Sub
Private Sub label1_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles Label1.DragDrop
If e.Data.GetData(DataFormats.Text).ToString = "SomeData" Then
Button1.Location = PointToClient(MousePosition)
Button1.BringToFront()
MessageBox.Show("Yes, it worked !")
Button1.Location = New Point(15, 50)
End If
End Sub
Private Sub Label1_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles Label1.DragEnter
If e.Data.GetDataPresent(DataFormats.Text) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Dim NPos As New Point()
NPos = Me.PointToClient(MousePosition)
NPos.Offset(-GripOffset.X, -GripOffset.Y)
Button1.Location = NPos
End Sub
' MouseUp never executes - why ?
' Is it disabled or by some other meens controled by the DoDragDrop in
MouseDown-event ?
Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
Timer1.Enabled = False
End Sub

' to here !!!!!!!!

Compiler and drag the Button1 to the Label1 - the Button is visualy
following the mouse but it wont drop at all - you have to right-click the
mouse and the usual 'DragDrop-system' with my MsgBox is NOT working.

Now - in the Button1_MouseDown-event comment out the Timer1.Enabled=True and start again - now the 'DragDrop-system' with my MsgBox is IS working but the Button1 is NOT visualy following the mouse but jumps to the Label1 when I
depress the mouseButton.

I think the latter solution works the best way so that you can comment out
all the Timer-related-code.

BUT in my application where I plan to use this 'DragDrop-functionality' I
can NOT reference the Button1 directly when I move it in the
Label1_DragDrop-event - that is - I can NOT use the "Button1.Location =
PointToClient(MousePosition)" - therefore:

How can I Drag and Drop an object as a whole unit (here a Button) so that I in the Label1_DragDrop-event can work on that 'dropped object' and get
access to all it's properties etc. ?

In the Button1_MouseDown-event I actualy without any syntax error CAN write:
Button1.DoDragDrop(Button1, DragDropEffects.Copy) ' where 'the whole
object' Button1 is referenced !

But what shall I do in the DragEnter and DragDrop events to tell thats is
the whole object I want to work with ?

How can I do this ?

KS, Denmark

Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.