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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marc

    #1

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

    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_MouseDo wn(ByVal sender As Object, ByVal e As
    System.Windows. Forms.MouseEven tArgs) Handles btnMove.MouseDo wn
    If e.Button = Windows.Forms.M ouseButtons.Lef t Then
    Dragging = True
    mousex = -e.X
    mousey = -e.Y
    Dim clipleft As Integer = Me.PointToClien t(MousePosition ).X
    - btnMove.Locatio n.X
    Dim cliptop As Integer = Me.PointToClien t(MousePosition ).Y
    - btnMove.Locatio n.Y
    Dim clipwidth As Integer = Me.ClientSize.W idth -
    (btnMove.Width - clipleft)
    Dim clipheight As Integer = Me.ClientSize.H eight -
    (btnMove.Height - cliptop)
    Windows.Forms.C ursor.Clip = Me.RectangleToS creen(New
    Rectangle(clipl eft, cliptop, clipwidth, clipheight))
    btnMove.Invalid ate()


    End If
    End Sub

    Private Sub btnMove_MouseMo ve(ByVal sender As Object, ByVal e As
    System.Windows. Forms.MouseEven tArgs) Handles btnMove.MouseMo ve
    If Dragging Then
    'move control to new position
    Dim MPosition As New Point()
    MPosition = Me.PointToClien t(MousePosition )
    MPosition.Offse t(mousex, mousey)
    'ensure control cannot leave container
    btnMove.Locatio n = MPosition
    End If
    End Sub

    Private Sub btnMove_MouseUp (ByVal sender As Object, ByVal e As
    System.Windows. Forms.MouseEven tArgs) Handles btnMove.MouseUp
    If Dragging Then
    'end the dragging
    Dragging = False
    'Me.Capture = False
    Windows.Forms.C ursor.Clip = Nothing
    btnMove.Invalid ate()
    End If
    End Sub

  • Tom Shelton

    #2
    Re: Drag and drop multiple button controls..need help modifying code please???


    Marc wrote:
    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.M ouseButtons.Lef t Then
    Dragging = True
    mousex = -e.X
    mousey = -e.Y
    Dim clipleft As Integer = Me.PointToClien t(MousePosition ).X -
    theButton.Locat ion.X
    Dim cliptop As Integer = Me.PointToClien t(MousePosition ).Y -
    theButon.Locati on.Y
    Dim clipwidth As Integer = Me.ClientSize.W idth -
    (theButton.Widt h - clipleft)
    Dim clipheight As Integer = Me.ClientSize.H eight -
    (theButton.Heig ht - cliptop)
    Windows.Forms.C ursor.Clip = Me.RectangleToS creen( _
    New Rectangle(clipl eft, cliptop, clipwidth, clipheight))
    theButton.Inval idate()
    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.MouseDo wn, AddressOf Me.MouseMove

    to add the actual event handler.

    HTH

    --
    Tom Shelton

    Comment

    • Marc

      #3
      Re: Drag and drop multiple button controls..need help modifying code please???

      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?

      Comment

      • Tom Shelton

        #4
        Re: Drag and drop multiple button controls..need help modifying code please???


        Marc wrote:
        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.mousedo wn, addressof me.mousendownha ndler
        endif
        next
        end sub

        HTH

        --
        Tom Shelton

        Comment

        • RobinS

          #5
          Re: Drag and drop multiple button controls..need help modifying code please???

          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_crosby@ho tmail.comwrote in message
          news:1164304374 .957083.81610@j 44g2000cwa.goog legroups.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?
          >

          Comment

          • Marc

            #6
            Re: Drag and drop multiple button controls..need help modifying code please???

            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_crosby@ho tmail.comwrote in message
            news:1164304374 .957083.81610@j 44g2000cwa.goog legroups.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?

            Comment

            • Dennis

              #7
              Re: Drag and drop multiple button controls..need help modifying co

              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.
              --
              Dennis 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_crosby@ho tmail.comwrote in message
              news:1164304374 .957083.81610@j 44g2000cwa.goog legroups.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?
              >
              >
              >

              Comment

              Working...