Results 1 to 17 of 17

Thread: [RESOLVED] Help please! Easy question but I'm struggling.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Seattle.
    Posts
    176

    Resolved [RESOLVED] Help please! Easy question but I'm struggling.

    I will be using the drag code by Paul. Problem is, it only handles one ballRed1 (a game piece) and I have 8 total: ballRed1, ballRed2, ballRed3, ballRed4, ballBlue1, ballBlue2, ballBlue3, ballBlue4), yet I need an easy way for just by using the dragging thing, it applies to all of those in one single code. Please help.

  2. #2
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: Help please! Easy question but I'm struggling.

    A good start would be posting the drag code by paul.

    Cheers
    Icyculyr

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Seattle.
    Posts
    176

    Re: Help please! Easy question but I'm struggling.

    Code:
        Dim Dragging As Boolean
        Dim PointClicked As Point
        Private Sub BallRed1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ballRed1.MouseDown
            If e.Button = Windows.Forms.MouseButtons.Left Then
                Dragging = True
                PointClicked = New Point(e.X, e.Y)
            Else
                Dragging = False
            End If
        End Sub
        Private Sub BallRed1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ballRed1.MouseMove
            If Dragging Then
                Dim PointMoveTo As Point
                PointMoveTo = ballRed1.PointToScreen(New Point(e.X, e.Y))
                PointMoveTo.Offset(-(PointClicked.X + Me.Left) - CInt((Me.Width - Me.ClientRectangle.Width) / 2), -(PointClicked.Y + Me.Top) - (Me.Height - Me.ClientRectangle.Height) + CInt((Me.Width - Me.ClientRectangle.Width) / 2))
                ballRed1.Location = PointMoveTo
            End If
        End Sub
        Private Sub BallRed1_MouseUp(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) Handles ballRed1.MouseUp
            Dragging = False
        End Sub

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Help please! Easy question but I'm struggling.

    A single sub can handle multiple events by adding to the Handles clause at the end of the sub:

    Handles ballRed1.MouseMove, Handles ballRed2.MouseMove, etc.

    This will not be sufficient, though it looks like it might be for the MouseDown and MouseUp events. For the MouseMove event, the problem will be that the current code handles ballRed1, and ONLY ballRed1. You might consider something in that sub that would identify which item is being moved and work with that, but I don't think that's such a good solution.

    Consider altering the Dragging variable from a boolean to a structure like this:

    vb Code:
    1. Public Structure DraggedInfo
    2.  Public ImDragging as boolean
    3.  Public DraggedItem as ...uh...see below
    4. End Structure

    Now, you would declare Dragging as New DraggedInfo, rather than Boolean.
    In the MouseDown Event, instead of setting Dragging, you would set Dragging.ImDragging, and you would set DraggedItem to whatever item the mouse was clicked on. What DraggedItem is I'm not too clear on. If it is a picturebox or a label, then make it that. If the items you listed in post #1 were multiple types of object, then DraggedItem could just be of type Object, though you'd have to cast it back to the right item at a later time.

    Figuring out which item was clicked on if the MouseDown event handled multiple items might prove to be tricky (so that you would know which item to put into DraggedItem), but it probably isn't too hard. It can be done by looking at the mouse x,y relative to the size and position of the items, if you can't get the info from one of the arguments directly (you should be able to).

    Now, in MouseMove, you would just replace the ballRed1 with Dragged.DraggedItem, and the If Dragging with If Dragged.ImDragging

    The MouseUp event would be even easier, as it is just clearing Dragged.ImDragging.
    My usual boring signature: Nothing

  5. #5
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: Help please! Easy question but I'm struggling.

    If you don't use the tag of the ball for anything, you can do the following...

    The word "nObject" is to be replaced with the type of RedBall1
    Code:
    Public Class cBallData
        Public Dragging As Boolean = False
        Public PointClicked As Point = Nothing
    End Class
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim bdData As New cBallData()
        ballRed1.Tag = bdData
        ballRed2.Tag = bdData
        ballRed3.Tag = bdData
    End Sub
        Private Sub BallRed1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ballRed1.MouseDown, ballRed2.MouseDown, ballRed3.MouseDown
            Dim bBall As nObject = CType(sender, nObject)
            If e.Button = Windows.Forms.MouseButtons.Left Then
                bBall.Dragging = True
                bBall.PointClicked = New Point(e.X, e.Y)
            Else
                bBall.Dragging = False
            End If
        End Sub
        Private Sub BallRed1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ballRed1.MouseMove, ballRed2.MouseMove, ballRed3.MouseMove
            Dim bBall As nObject = CType(sender, nObject)
            If bBall.Dragging Then
                Dim PointMoveTo As Point = Nothing
                PointMoveTo = bBall .PointToScreen(New Point(e.X, e.Y))
                PointMoveTo.Offset(-(PointClicked.X + Me.Left) - CInt((Me.Width - Me.ClientRectangle.Width) / 2), -(PointClicked.Y + Me.Top) - (Me.Height - Me.ClientRectangle.Height) + CInt((Me.Width - Me.ClientRectangle.Width) / 2))
                bBall .Location = PointMoveTo
            End If
        End Sub
        Private Sub BallRed1_MouseUp(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) Handles ballRed1.MouseUp, ballRed2.MouseUp, ballRed3.MouseUp
            Dim bBall As nObject = CType(sender, nObject)
            bBall.Dragging = False
        End Sub
    That should work

    Cheers
    Icyculyr

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Seattle.
    Posts
    176

    Re: Help please! Easy question but I'm struggling.

    Icy, nObject isn't defined. I'm using VB 2005 Express.

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Help please! Easy question but I'm struggling.

    Quote Originally Posted by Seahawks1
    Icy, nObject isn't defined. I'm using VB 2005 Express.
    Have you tried reading the words Icy marked in bold?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: Help please! Easy question but I'm struggling.

    Yeah, that would be a good start..

    Cheers
    Icyculyr

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Help please! Easy question but I'm struggling.

    Quote Originally Posted by Icyculyr
    If you don't use the tag of the ball for anything, you can do the following...

    The word "nObject" is to be replaced with the type of RedBall1
    Code:
    Public Class cBallData
        Public Dragging As Boolean = False
        Public PointClicked As Point = Nothing
    End Class
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim bdData As New cBallData()
        ballRed1.Tag = bdData
        ballRed2.Tag = bdData
        ballRed3.Tag = bdData
    End Sub
        Private Sub BallRed1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ballRed1.MouseDown, ballRed2.MouseDown, ballRed3.MouseDown
            Dim bBall As nObject = CType(sender, nObject)
            If e.Button = Windows.Forms.MouseButtons.Left Then
                bBall.Dragging = True
                bBall.PointClicked = New Point(e.X, e.Y)
            Else
                bBall.Dragging = False
            End If
        End Sub
        Private Sub BallRed1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ballRed1.MouseMove, ballRed2.MouseMove, ballRed3.MouseMove
            Dim bBall As nObject = CType(sender, nObject)
            If bBall.Dragging Then
                Dim PointMoveTo As Point = Nothing
                PointMoveTo = bBall .PointToScreen(New Point(e.X, e.Y))
                PointMoveTo.Offset(-(PointClicked.X + Me.Left) - CInt((Me.Width - Me.ClientRectangle.Width) / 2), -(PointClicked.Y + Me.Top) - (Me.Height - Me.ClientRectangle.Height) + CInt((Me.Width - Me.ClientRectangle.Width) / 2))
                bBall .Location = PointMoveTo
            End If
        End Sub
        Private Sub BallRed1_MouseUp(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) Handles ballRed1.MouseUp, ballRed2.MouseUp, ballRed3.MouseUp
            Dim bBall As nObject = CType(sender, nObject)
            bBall.Dragging = False
        End Sub
    That should work

    Cheers
    Icyculyr
    i don't see how thats supposed to work. you've assigned a value to the .tag property but then you don't use it

  10. #10
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: Help please! Easy question but I'm struggling.

    Code:
    Public Class cBallData
        Public Dragging As Boolean = False
        Public PointClicked As Point = Nothing
    End Class
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim bdData As New cBallData()
        ballRed1.Tag = bdData
        ballRed2.Tag = bdData
        ballRed3.Tag = bdData
    End Sub
        Private Sub BallRed1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ballRed1.MouseDown, ballRed2.MouseDown, ballRed3.MouseDown
            Dim bBall As nObject = CType(sender, nObject)
            Dim bdData As cBallData = CType(bBall.Tag, cBallData)
            If e.Button = Windows.Forms.MouseButtons.Left Then
                bdData.Dragging = True
                bdData.PointClicked = New Point(e.X, e.Y)
            Else
                bBall.Dragging = False
            End If
            bBall.Tag = bdData
        End Sub
        Private Sub BallRed1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ballRed1.MouseMove, ballRed2.MouseMove, ballRed3.MouseMove
            Dim bBall As nObject = CType(sender, nObject)
            Dim bdData As cBallData = CType(bBall.Tag, cBallData)
            If bdData.Dragging Then
                Dim PointMoveTo As Point = Nothing
                PointMoveTo = bBall .PointToScreen(New Point(e.X, e.Y))
                PointMoveTo.Offset(-(PointClicked.X + Me.Left) - CInt((Me.Width - Me.ClientRectangle.Width) / 2), -(PointClicked.Y + Me.Top) - (Me.Height - Me.ClientRectangle.Height) + CInt((Me.Width - Me.ClientRectangle.Width) / 2))
                bBall.Location = PointMoveTo
            End If
        End Sub
        Private Sub BallRed1_MouseUp(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) Handles ballRed1.MouseUp, ballRed2.MouseUp, ballRed3.MouseUp
            Dim bBall As nObject = CType(sender, nObject)
            Dim bdData As cBallData = CType(bBall.Tag, cBallData)
            bdData.Dragging = False
            bBall.Tag = bdData
        End Sub
    Fixed, I'd gotten confused, and was using bBall thinking it was the class

    Cheers
    Icyculyr

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Seattle.
    Posts
    176

    Re: Help please! Easy question but I'm struggling.

    What is cBallData? I'm putting this in Form1 and I receive errors Should Cballdad me form1?

  12. #12
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: Help please! Easy question but I'm struggling.

    Post your code then.

    Cheers
    Icyculyr

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Seattle.
    Posts
    176

    Re: Help please! Easy question but I'm struggling.

    It's too confusing to write out. I've attached everything. please look at it!
    Attached Files Attached Files

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Help please! Easy question but I'm struggling.

    try this. i've put the dragging code in the puckred + puckblue classes.
    seems to work
    Attached Files Attached Files

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Seattle.
    Posts
    176

    Re: Help please! Easy question but I'm struggling.

    Quote Originally Posted by .paul.
    try this. i've put the dragging code in the puckred + puckblue classes.
    seems to work
    You are my VB hero!

    THANK YOU

  16. #16
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [RESOLVED] Help please! Easy question but I'm struggling.

    One more thing, instead of tags, you may use the following trick to find out which control was clicked:

    vb.net Code:
    1. Private Sub Label1.MouseDown(ByVal sender As System.Object, _
    2.                                           ByVal e As System.Windows.Forms.MouseEventArgs) _
    3.                                           Handles Label1.MouseDown, _
    4.                                                      Label2.MouseDown, _
    5.                                                      Label3.MouseDown 'etc...
    6.  
    7. Dim lbl As Label = CType(sender, Label) 'This line will get you the exact label that was clicked
    8.  
    9. End Sub
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  17. #17
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: [RESOLVED] Help please! Easy question but I'm struggling.

    what i would recommend, with your puckred + puckblue classes, as they are more or less identical, is to use 1 class for both red + blue pucks. the image you're using which i'm assuming is different for red + blue, could be passed as a parameter in the constructor.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width