Drag and drop multiple button controls..need help modifying code please???

M

Marc

Hi,

I am using the below code which simply allows a single button control
to be dragged and dropped anywhere around a form. My problem is that
want to move about 100 buttons on the form. Is there anyway to modify
the code so that any buttons added on the form will be able to be
dragged and dropped?

Many Thanks!

Marc


Private Sub btnMove_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles btnMove.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
Dragging = True
mousex = -e.X
mousey = -e.Y
Dim clipleft As Integer = Me.PointToClient(MousePosition).X
- btnMove.Location.X
Dim cliptop As Integer = Me.PointToClient(MousePosition).Y
- btnMove.Location.Y
Dim clipwidth As Integer = Me.ClientSize.Width -
(btnMove.Width - clipleft)
Dim clipheight As Integer = Me.ClientSize.Height -
(btnMove.Height - cliptop)
Windows.Forms.Cursor.Clip = Me.RectangleToScreen(New
Rectangle(clipleft, cliptop, clipwidth, clipheight))
btnMove.Invalidate()


End If
End Sub

Private Sub btnMove_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles btnMove.MouseMove
If Dragging Then
'move control to new position
Dim MPosition As New Point()
MPosition = Me.PointToClient(MousePosition)
MPosition.Offset(mousex, mousey)
'ensure control cannot leave container
btnMove.Location = MPosition
End If
End Sub

Private Sub btnMove_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles btnMove.MouseUp
If Dragging Then
'end the dragging
Dragging = False
'Me.Capture = False
Windows.Forms.Cursor.Clip = Nothing
btnMove.Invalidate()
End If
End Sub
 
T

Tom Shelton

Marc said:
Hi,

I am using the below code which simply allows a single button control
to be dragged and dropped anywhere around a form. My problem is that
want to move about 100 buttons on the form. Is there anyway to modify
the code so that any buttons added on the form will be able to be
dragged and dropped?

Many Thanks!

Marc

Sure. Just remove the Handles clause (and probably rename the event
handler to a more generic name, but that's only for readability) and
then use AddHandler/RemoveHandler to add and remove the appropriate
events at runtime. That way, they can all share the same event code
and it can be added dynamically. Also, you won't want to refer to a
specific button in the event code, but use the passed in sender object
- since that would be a reference to the button actually pressed. Of
course, you'll have to cast it to a button to use it :) Anyway,
something like this:

Private Sub MouseDown(ByVal sender As Object, ByVal e As
MouseEventArgs)
Dim theButton As Button = DirectCast (sender, Button) ' cast

If e.Button = Windows.Forms.MouseButtons.Left Then
Dragging = True
mousex = -e.X
mousey = -e.Y
Dim clipleft As Integer = Me.PointToClient(MousePosition).X -
theButton.Location.X
Dim cliptop As Integer = Me.PointToClient(MousePosition).Y -
theButon.Location.Y
Dim clipwidth As Integer = Me.ClientSize.Width -
(theButton.Width - clipleft)
Dim clipheight As Integer = Me.ClientSize.Height -
(theButton.Height - cliptop)
Windows.Forms.Cursor.Clip = Me.RectangleToScreen( _
New Rectangle(clipleft, cliptop, clipwidth, clipheight))
theButton.Invalidate()
End If
End Sub

Now you might want to protect your cast there, but I didn't bother for
this example. Though it shouldn't fail as long as you only attach this
handler to a button :) If you do other controls, then you could make
it work on all of them by casting to Control instead. Anyway, then at
run time you can do:

AddHandler btnMove.MouseDown, AddressOf Me.MouseMove

to add the actual event handler.

HTH
 
M

Marc

Hi Tom,

Thanks very much. Im pretty new to this and am in need of all the help
I can get.

I must admit i am struggling to get it working....I have removed all
references the the actaul button and used 'the button' as you
suggested. I have also removed all the handler clauses.

How would I add the add the handler code you provided at runtime
though?
 
T

Tom Shelton

Marc said:
Hi Tom,

Thanks very much. Im pretty new to this and am in need of all the help
I can get.

I must admit i am struggling to get it working....I have removed all
references the the actaul button and used 'the button' as you
suggested. I have also removed all the handler clauses.

How would I add the add the handler code you provided at runtime
though?

That depends on how the buttons are getting added... If you are
dynamically adding them at runtime - which is what I assumed, then add
the handler at the point of creation. If this is simply you have a
bunch of static buttons on the form that you want to share the same
handler, then I would do it in the form load event... Something like:

sub form_load (blah, blah)
foreach control in me.controls
if control is button
addhandler control.mousedown, addressof me.mousendownhandler
endif
next
end sub

HTH
 
R

RobinS

You would probably put the addhandler in your form_load event.
You'd want to addhandler for each button you want to use that
event.

Robin S.
 
M

Marc

Thanks works fine
RobinS said:
You would probably put the addhandler in your form_load event.
You'd want to addhandler for each button you want to use that
event.

Robin S.
 
G

Guest

Marc, I certainly don't want to tell you how to design your system but have
you thought about using different forms to group the buttons by function.
100 buttons on a form is a lot for a user to quickly locate a button. Just a
suggestion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top