[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.
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
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:
Public Structure DraggedInfo
Public ImDragging as boolean
Public DraggedItem as ...uh...see below
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.
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
Re: Help please! Easy question but I'm struggling.
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
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
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.