none
error on lines 13 and 15 of this code. In a nutshell this program moves a picture box by mouseclick

    Question

  • Public Class Form1
        Private MouseDown1 As Boolean
        Private PosDiff As Point
        Private Sub Form1_Load(ByVal sender As Object,
                       ByVal e As System.EventArgs) Handles Me.Load
            Me.TopMost = True
        End Sub
    
        Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As _
               System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
            If Not MouseDown1 Then Exit Sub
            If Not System.Windows.Forms.SystemInformation.MouseButtonsSwapped() Then
                If Not e.Button = Windows.Forms.MouseButtons.Left Then Exit Sub
            Else
                If Not e.Button = Windows.Forms.MouseButtons.Right Then Exit Sub
            End If
    
            Me.Location = New Point(Cursor.Position().X - PosDiff.X,
                                    Cursor.Position().Y - PosDiff.Y)
        End Sub
    
        Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As _
              System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
            MouseDown1 = False
        End Sub
    
        Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As _
             System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
            MouseDown1 = True
            PosDiff = New Point(Cursor.Position().X - Me.Location.X,
                                Cursor.Position().Y - Me.Location.Y)
        End Sub
    End Class
    the section where it shows right on lines 13 and 15 of my code 
    Thursday, March 9, 2017 10:03 PM

Answers

  • As Acamar says, ME refers to the form, not the picturebox. Here is a working example.

    Public Class Form6
        Private PopupToolbarLocation As Point
        Private PopupToolbarMouseDown As Point
        Private PopupToolbarMouseState As Integer
        Private HeaderHeight As Integer = 20
    
        Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
            If e.Button = MouseButtons.Left And e.Y <= HeaderHeight Then
                'begin moving form
                PopupToolbarMouseState = 1
                PopupToolbarMouseDown = e.Location
                PopupToolbarLocation = PictureBox1.Location
            End If
        End Sub
    
        Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
    
            If PopupToolbarMouseState > 0 Then
                Dim dx As Integer = e.X - PopupToolbarMouseDown.X
                Dim dy As Integer = e.Y - PopupToolbarMouseDown.Y
    
                PictureBox1.Location = New Point(PopupToolbarLocation.X + dx, PopupToolbarLocation.Y + dy)
                PopupToolbarLocation.X += dx
                PopupToolbarLocation.Y += dy
    
            End If
        End Sub
    
        Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
            PopupToolbarMouseState = 0
            CheckToolbarOnScreen(PictureBox1)
        End Sub
    
        Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
            e.Graphics.Clear(Color.AntiqueWhite)
            e.Graphics.FillRectangle(Brushes.Chocolate, 0, 0, PictureBox1.ClientSize.Width, HeaderHeight)
            e.Graphics.DrawRectangle(Pens.Black, 0, 0, PictureBox1.ClientSize.Width - 2, PictureBox1.ClientSize.Height - 2)
        End Sub
    
        Public Sub CheckToolbarOnScreen(thisPic As PictureBox)
            'check screen location within main form bounds
            If thisPic.Right > ClientRectangle.Right Then
                thisPic.Left = ClientRectangle.Right - thisPic.Width
            End If
    
            If thisPic.Bottom > ClientRectangle.Bottom Then
                thisPic.Top = ClientRectangle.Bottom - thisPic.Height
            End If
    
            If thisPic.Top < ClientRectangle.Top Then thisPic.Top = ClientRectangle.Top
            If thisPic.Left < ClientRectangle.Left Then thisPic.Left = ClientRectangle.Left
    
        End Sub
    End Class

    • Marked as answer by Programmer_10 Wednesday, March 15, 2017 8:27 PM
    Friday, March 10, 2017 1:18 PM

All replies

  • the section where it shows right on lines 13 and 15 of my code 

    Which lines are 13 and 15?

    What is the error?

    - Wayne

    Thursday, March 9, 2017 10:16 PM
  • the section where it shows right on lines 13 and 15 of my code 

    The error message probably tells you what the problem is.

    You have made arbitrary changes to the code, and I don't believe it can work at all.   You are changing the position of a form, not a picturebox, which doesn't seem to make sense.

    Thursday, March 9, 2017 10:28 PM
  • As Acamar says, ME refers to the form, not the picturebox. Here is a working example.

    Public Class Form6
        Private PopupToolbarLocation As Point
        Private PopupToolbarMouseDown As Point
        Private PopupToolbarMouseState As Integer
        Private HeaderHeight As Integer = 20
    
        Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
            If e.Button = MouseButtons.Left And e.Y <= HeaderHeight Then
                'begin moving form
                PopupToolbarMouseState = 1
                PopupToolbarMouseDown = e.Location
                PopupToolbarLocation = PictureBox1.Location
            End If
        End Sub
    
        Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
    
            If PopupToolbarMouseState > 0 Then
                Dim dx As Integer = e.X - PopupToolbarMouseDown.X
                Dim dy As Integer = e.Y - PopupToolbarMouseDown.Y
    
                PictureBox1.Location = New Point(PopupToolbarLocation.X + dx, PopupToolbarLocation.Y + dy)
                PopupToolbarLocation.X += dx
                PopupToolbarLocation.Y += dy
    
            End If
        End Sub
    
        Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
            PopupToolbarMouseState = 0
            CheckToolbarOnScreen(PictureBox1)
        End Sub
    
        Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
            e.Graphics.Clear(Color.AntiqueWhite)
            e.Graphics.FillRectangle(Brushes.Chocolate, 0, 0, PictureBox1.ClientSize.Width, HeaderHeight)
            e.Graphics.DrawRectangle(Pens.Black, 0, 0, PictureBox1.ClientSize.Width - 2, PictureBox1.ClientSize.Height - 2)
        End Sub
    
        Public Sub CheckToolbarOnScreen(thisPic As PictureBox)
            'check screen location within main form bounds
            If thisPic.Right > ClientRectangle.Right Then
                thisPic.Left = ClientRectangle.Right - thisPic.Width
            End If
    
            If thisPic.Bottom > ClientRectangle.Bottom Then
                thisPic.Top = ClientRectangle.Bottom - thisPic.Height
            End If
    
            If thisPic.Top < ClientRectangle.Top Then thisPic.Top = ClientRectangle.Top
            If thisPic.Left < ClientRectangle.Left Then thisPic.Left = ClientRectangle.Left
    
        End Sub
    End Class

    • Marked as answer by Programmer_10 Wednesday, March 15, 2017 8:27 PM
    Friday, March 10, 2017 1:18 PM
  • Ohhh no wonder, thanks for pointing that out, I got this code from someone else you see, btw Wayne AKing the error just shows unhandled exception error.
    Friday, March 10, 2017 3:31 PM
  • Ohhh no wonder, thanks for pointing that out, I got this code from someone else you see, btw Wayne AKing the error just shows unhandled exception error.

    Which lines are 13 and 15?  You might be using an older version of VS.

    The error message always carries extra description, which is the part you need to quote.  'Unhandled exception' simply means the error was passed to the runtime for handling.

    Friday, March 10, 2017 8:54 PM