|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Drag and drop multiple button controls..need help modifying code please???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 Marc wrote:
> Hi, Sure. Just remove the Handles clause (and probably rename the event> > 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 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 -- Tom Shelton 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? Marc wrote:
> Hi Tom, That depends on how the buttons are getting added... If you are> > 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? 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 -- Tom Shelton 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. --------------------------- Show quoteHide quote "Marc" <marc_cro***@hotmail.com> wrote in message news:1164304374.957083.81610@j44g2000cwa.googlegroups.com... > 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? > Thanks works fine
RobinS wrote: Show quoteHide quote > 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. > --------------------------- > > "Marc" <marc_cro***@hotmail.com> wrote in message > news:1164304374.957083.81610@j44g2000cwa.googlegroups.com... > > 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? > > 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. -- Show quoteHide quoteDennis in Houston "Marc" wrote: > Thanks works fine > RobinS wrote: > > 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. > > --------------------------- > > > > "Marc" <marc_cro***@hotmail.com> wrote in message > > news:1164304374.957083.81610@j44g2000cwa.googlegroups.com... > > > 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? > > > > >
Q: Regular Expressions?
tell more about .net framework date time picker and validate event ( is this a bug ? ) Managing without .NET for runtime applications Automatic Form Fill in SQL connection problem Inherited handler problem. Restart a Sub and keep loop going Adapter Update...Syntax error embem Excel in winford using vb.net |
|||||||||||||||||||||||