-
Jan 5th, 2019, 06:41 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Changing X Location of Panel
In my form i'm trying to move panels around so I manged to make them move with the mouse to be located anywhere on the form. Now to mange these movements I made some limits to the location X to be in one equal X location between a range of values.
I need to correct my code because it dosen't come back to the x location when it's in the range.
code:
Code:
Public Class Form1
Dim x, y As Integer
Dim newpoint As New Point
Private Sub Panel1_MouseDown(sender As Object, e As MouseEventArgs) Handles Panel1.MouseDown
x = Control.MousePosition.X - Panel1.Location.X
y = Control.MousePosition.Y - Panel1.Location.Y
Panel1.BackColor = Color.LightGray
End Sub
Private Sub Panel1_MouseMove(sender As Object, e As MouseEventArgs) Handles Panel1.MouseMove
If e.Button = MouseButtons.Left Then
newpoint = Control.MousePosition
newpoint.X -= x
newpoint.Y -= y
Panel1.Location = newpoint
Panel1.Refresh()
End If
End Sub
Private Sub Panel1_MouseUp(sender As Object, e As MouseEventArgs) Handles Panel1.MouseUp
Panel1.BackColor = Color.SteelBlue
If newpoint.X >= 250 <= Then
newpoint.X = 330
Else
Panel1.Location = newpoint
Panel1.Refresh()
End If
End Sub
End Class
Here is exactly where I need the correction:
Code:
If newpoint.X >= 250 <= Then
newpoint.X = 330
Else
Panel1.Location = newpoint
Panel1.Refresh()
End If
Thank you very much
-
Jan 5th, 2019, 06:49 PM
#2
Re: Changing X Location of Panel
You set the newpoint.X to 330, but you don't set the panel location to newPoint, unless the X is not in the range. Don't you want to set the panel location to newPoint after you have set the X value to 330?
Seems like you could just delete the Else, but it might be better to write it like this:
Code:
If newpoint.X >= 250 <= Then
newpoint.X = 330
End If
Panel1.Location = newpoint
The refresh doesn't matter in that case, because the method is over right after you set the location. Setting the location will invalidate the panel, so it will paint when the UI gets around to it....and it will get around to it right after the End Sub, so right away. Refresh is needed in the MouseMove, because you may be getting so many of those in a row that the panel doesn't have a chance to redraw before the next MouseMove arrives. They really come fast and furious.
My usual boring signature: Nothing
-
Jan 5th, 2019, 07:16 PM
#3
Thread Starter
Hyperactive Member
Re: Changing X Location of Panel
Originally Posted by Shaggy Hiker
You set the newpoint.X to 330, but you don't set the panel location to newPoint, unless the X is not in the range. Don't you want to set the panel location to newPoint after you have set the X value to 330?
Seems like you could just delete the Else, but it might be better to write it like this:
Code:
If newpoint.X >= 250 <= Then
newpoint.X = 330
End If
Panel1.Location = newpoint
The refresh doesn't matter in that case, because the method is over right after you set the location. Setting the location will invalidate the panel, so it will paint when the UI gets around to it....and it will get around to it right after the End Sub, so right away. Refresh is needed in the MouseMove, because you may be getting so many of those in a row that the panel doesn't have a chance to redraw before the next MouseMove arrives. They really come fast and furious.
Okay, it worked perfectly this way but I needed to remove ( <= ) Because it says ( Expression Expected )
and it works this way now:
Code:
If newpoint.X >= 250 Then
newpoint.X = 350
End If
Panel1.Location = newpoint
but I need it to be in a range between 250 and 660 for example:
Code:
If newpoint.X >= 250 <= 660 Then
but it dosen't work this way
-
Jan 5th, 2019, 07:20 PM
#4
Re: Changing X Location of Panel
If newpoint.X >= 250 <= 660 Then
That notation is basically mathematical shorthand.
It needs to be like this:
Code:
If newpoint.X >= 250 AndAlso newpoint.X <= 660 Then
-
Jan 5th, 2019, 07:25 PM
#5
Thread Starter
Hyperactive Member
Re: Changing X Location of Panel
Originally Posted by OptionBase1
That notation is basically mathematical shorthand.
It needs to be like this:
Code:
If newpoint.X >= 250 AndAlso newpoint.X <= 660 Then
That Worked perfectly
Thank you very much
-
Jan 5th, 2019, 09:09 PM
#6
Re: [RESOLVED] Changing X Location of Panel
HA! I didn't even look at the If statement enough to see that.
My usual boring signature: Nothing
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|