Results 1 to 6 of 6

Thread: [RESOLVED] Changing X Location of Panel

  1. #1

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Resolved [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

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  3. #3

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Re: Changing X Location of Panel

    Quote Originally Posted by Shaggy Hiker View Post
    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

  4. #4
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    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

  5. #5

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Re: Changing X Location of Panel

    Quote Originally Posted by OptionBase1 View Post
    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

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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
  •  



Click Here to Expand Forum to Full Width