none
Using the Escape button to stop drawing? Where should I put it in my code? RRS feed

  • Question

  • Hello. I'm trying to use the escape button to stop my application from drawing. However, its not working. I thought I could make a key down event for the form with the escape button to change the boolean Snapo to false, so that it would stop the code in my mouse move event because that's what makes my line look like its following the mouse. What should I do?

    Imports System.Drawing.Drawing2D
    
    
    
    
    Public Class Form1
    
        Dim g As System.Drawing.Graphics
        Dim r As System.Drawing.Graphics
        Dim a As System.Drawing.Graphics
    
    
        Dim col As Color
        Dim D As Integer = 2
        Dim p As Pen = New Pen(col, D)
    
    
        Dim q As Pen = New Pen(Color.Black, 0)
        Dim t As Pen = New Pen(Color.Blue, 5)
        Dim n As Pen = New Pen(Color.Black, 5)
        Dim count As Integer = 0
        Dim trac As Integer = 1
        Dim point1 As New Point()
        Dim point2 As New Point()
        Dim point3 As New Point()
    
        Dim myPointsArray As New List(Of PointF)
        Dim myPointsArrays(100)
    
        Dim ShowGrid As Boolean = False
        Dim Draw As Boolean = False
        Dim TurnOnDrawL As Boolean = False
        Dim StopConD As Boolean = False
        Dim Snapo As Boolean = False
        Dim Pushed As Boolean = True
        Dim truethat As Boolean = True
        Dim StopLoop As Boolean = False
        Dim Key As Boolean = False
    
    
        Dim isDrag As Boolean = False
        Dim theRectangle As New Rectangle(New Point(0, 0), New Size(0, 0))
        Dim startPoint As Point
    
        Dim bmp As Bitmap
        Dim SS As Bitmap
    
    
    
    
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    
    
            Me.Text = "Drawing Application Version 1.0"
    
            bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
    
            PictureBox1.Image = bmp
    
            g = PictureBox1.CreateGraphics
    
    
        End Sub
    
    
    
    
        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    
    
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim H, W As Integer
    
    
    
            r = PictureBox1.CreateGraphics
    
            D = Depth.Text
            W = Width.Text
            H = Height.Text
    
            p.Color = col
            p.Width = D
    
            g = PictureBox1.CreateGraphics
    
            gridon_Checked(sender, e)
    
            g.DrawRectangle(p, 100, 30, 60 * W, 60 * H)
    
            g.DrawRectangle(p, 100, 450, 60 * W, 60 * H)
    
            g.DrawRectangle(p, 588, 449, 60 * W, 60 * H)
    
            r.DrawLine(n, 500, 0, 500, 900)
    
            r.DrawLine(n, 0, 313, 878, 313)
    
            Lineop.Enabled = True
    
            ResetButton.Enabled = True
    
            Button1.Enabled = False
    
    
    
    
    
    
    
    
        End Sub
    
        Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles Red.CheckedChanged
    
    
            col = Color.Red
            p.Color = col
    
        End Sub
    
    
        Private Sub White_CheckedChanged(sender As Object, e As EventArgs) Handles White.CheckedChanged
    
            col = Color.White
            p.Color = col
    
        End Sub
    
        Private Sub Blue_CheckedChanged(sender As Object, e As EventArgs) Handles Blue.CheckedChanged
    
            col = Color.Blue
            p.Color = col
    
        End Sub
    
        Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
            p.DashStyle = Drawing2D.DashStyle.Solid
        End Sub
    
        Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
            p.DashStyle = Drawing2D.DashStyle.Dash
        End Sub
    
        Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    
            Draw = CheckBox1.Checked
    
    
        End Sub
    
        Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
    
    
    
            If Draw = True Then
    
                count = count + 1
    
                If count = 1 Then
    
    
                    point1 = New Point(e.X, e.Y)
    
                End If
    
    
                If count = 2 Then
    
                    point2 = New Point(e.X, e.Y)
    
                    g.DrawLine(p, point1, point2)
    
                    point1.X = point2.X
                    point1.Y = point2.Y
    
                    count = 1
    
                End If
    
    
            End If
    
    
        End Sub
    
    
        Private Sub About_Click(sender As Object, e As EventArgs) Handles About.Click
    
            MessageBox.Show("Hi, my name is Joshua. Check out unpopularthemovie.com (It has a Bible message). Thanks for using this application. All rights, privaleges, and royalties are for Joshua Tashbar(maker of this application). Copyright created in 2018",
        "About", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)
    
    
        End Sub
    
    
        Private Sub gridon_Checked(sender As Object, e As EventArgs) Handles gridon.CheckedChanged
    
            ShowGrid = gridon.Checked
    
            g = PictureBox1.CreateGraphics
    
            If ShowGrid = True Then
                q.Color = Color.Black
            Else
                q.Color = Color.LightGray
            End If
    
            For i As Integer = 1 To 44
                g.DrawLine(q, 50 * i, 0, 50 * i, 735)
            Next
    
            For j As Integer = 1 To 37
                g.DrawLine(q, 0, 50 * j, 878, 50 * j)
            Next
    
    
    
    
        End Sub
    
    
    
        Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
    
            Dim q As New Pen(Color.LightGray, D)
    
            'This z variable holds the remainder of the x coordinate of the mouse
            Dim z As Integer
    
            'This b variable holds the remainder of the y coordiante of the mouse
            Dim b As Integer
    
    
            If Snapo = True Then
    
                If count = 1 Then
    
                    point3 = New Point(e.X, e.Y)
    
                    'Here is where I do the math for the lines to go on the grid
    
                    'This are the variables to hold the remainders of the division
                    z = e.X / 50
                    b = e.Y / 50
    
                    'The reason I divide and multiply by 50 is because thats the height and width of my squares in my grid
                    'These are the x and y coordinate values for the point 3
                    point3.X = z * 50
                    point3.Y = b * 50
    
    
                    'This makes a line of whatever color the user chooses (red for example)
                    g.DrawLine(p, point1, point3)
    
                    gridon_Checked(sender, e)
    
                    Button1_Click(sender, e)
    
                    'This line makes a line of lightgray so that the user cannot see it because thats what is the color the picture box
                    g.DrawLine(q, point1, point3)
    
    
    
                End If
    
            End If
    
    
        End Sub
    
        Private Sub ResetButton_Click(sender As Object, e As EventArgs) Handles ResetButton.Click
    
            PictureBox1.Refresh()
    
    
        End Sub
    
        'Button to save picture
        Private Sub SavePicture_Click(sender As Object, e As EventArgs) Handles SavePicture.Click
    
            Dim SaveDLG As New SaveFileDialog
    
            Take_Clip()
    
            SaveDLG.Filter = "JPEG Files (*.jpeg)|*.jpeg|All Files|*.*"
    
            SaveDLG.Title = "Save File As"
            SaveDLG.InitialDirectory = "C:\"
    
            DialogResult = SaveDLG.ShowDialog
    
            If DialogResult = Windows.Forms.DialogResult.OK Then
    
                SS.Save(SaveDLG.FileName)
    
    
    
                SS.Save("Drawing.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)
    
            ElseIf DialogResult = Windows.Forms.DialogResult.Cancel Then
            End If
    
    
    
        End Sub
    
    
        Private Sub BStop_Click(sender As Object, e As EventArgs)
    
            StopConD = True
    
    
        End Sub
    
        Private Sub Snapon_CheckedChanged(sender As Object, e As EventArgs) Handles Snapon.CheckedChanged
    
    
            Snapo = Snapon.Checked
    
        End Sub
    
        Private Sub Lineop_Click(sender As Object, e As EventArgs) Handles Lineop.Click
    
            While trac = 1
    
    
    
                If trac = 1 Then
                    btnun.Enabled = True
    
                    btnre.Enabled = True
    
                    lblmessage.Text = "Exit New Line Drawing"
    
    
                End If
    
                trac = trac + 1
    
            End While
    
            If trac = 3 Then
                btnun.Enabled = False
    
                btnre.Enabled = False
    
                lblmessage.Text = "..."
    
                trac = 0
    
            End If
    
            trac = trac + 1
    
    
    
        End Sub
    
        Private Function CaptureControlArea(ctrl As Control) As Bitmap
            Dim size As Size = ctrl.ClientSize
            Dim tmpBmp As New Bitmap(size.Width, size.Height)
            Dim g As Graphics
            g = Graphics.FromImage(tmpBmp)
            g.CopyFromScreen(ctrl.PointToScreen(New Drawing.Point(0, 0)), New Drawing.Point(0, 0), New Size(size.Width, size.Height))
            Return tmpBmp
        End Function
    
        Private Sub Take_Clip()
            SS = CaptureControlArea(PictureBox1)
        End Sub
    
        Private Sub lblmessage_KeyDown(sender As Object, e As KeyEventArgs) Handles lblmessage.KeyDown
    
        End Sub
    
        Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
            If e.KeyCode = Keys.Escape Then
                Snapo = False
            End If
        End Sub
    
        ' Public Shared Function IsKeyDown(key As Key) As Boolean
    
        'End Function
    
    End Class
    
    
    
    
    

    Thursday, July 26, 2018 3:56 PM

Answers

  • Josh,

    Did you set the form KeyPreview = True ?

    The form must have the focus when the key is pressed.

    Its possible other controls are steeling focus after they are clicked ie a button etc

    Otherwise I dont see any reason why your code is not working.

    So now you have to debug your creation.  :)

    Put a break point in the key press event and see if it fires if you have not. Step through the code if you can and check the values are what you expect. Check the mousemove event etc.

     

    PS It could be the key press is working but your drawing is just not working the way you think?

    I don't know. Your project has become too complicated for us to debug.

    Friday, July 27, 2018 4:05 PM

All replies

  • Hi

    I think override keyboard events is a good way to change the boolean Snapo to false.

        Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
            If keyData = Keys.Escape Then
                Snapo = False
            End If
            Return True
        End Function

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 27, 2018 1:52 AM
  • Josh,

    Did you set the form KeyPreview = True ?

    The form must have the focus when the key is pressed.

    Its possible other controls are steeling focus after they are clicked ie a button etc

    Otherwise I dont see any reason why your code is not working.

    So now you have to debug your creation.  :)

    Put a break point in the key press event and see if it fires if you have not. Step through the code if you can and check the values are what you expect. Check the mousemove event etc.

     

    PS It could be the key press is working but your drawing is just not working the way you think?

    I don't know. Your project has become too complicated for us to debug.

    Friday, July 27, 2018 4:05 PM
  • Thank you Mr. Tom. Setting KeyPreview to true made it work.
    Friday, July 27, 2018 6:28 PM
  • Thank you for the help Mr. Alex, but when I put your code into mine. It caused the drawing to not work anymore.
    Friday, July 27, 2018 6:32 PM
  • Hello,

    I am trying to use the Cancel Button property on my form to stop the application from drawing my line, however when I click escape it does not seem to click the button to stop my drawing. It makes the error noise but there is no error. I have KeyPreview set to True. I also have the Cancel Button set to the button that has the code to stop the drawing. The button that does this is invisible (i.e. visible is set to False).

    Also I was wondering if there is a way for me to post a video on here so you guys can see the application in action?

    Thank you for your time

    Friday, July 27, 2018 8:31 PM
  • Hi

    Why start a new thread with the SAME question?

    Suggestion: put these Options at the top of your code

    Option Strict On
    Option Explicit On
    

    as the code has many errors.

    Some examples:

      
    ' D,W and H are declared Integer yet you
    ' try to assign Strings to them.
      D = Depth.Text
      W = Width.Text
      H = Height.Text
    
    ' z and b are declared as Integer but
    ' you try to assign Doubles to them
       z = e.X / 50
       b = e.Y / 50
    
    

    Your code has many controls, so I can't be bothered to recreate the UI. I see the Save code still remains 'strange'.


    Regards Les, Livingston, Scotland

    Friday, July 27, 2018 9:56 PM
  • Josh,

    "I am trying to use the Cancel Button property on my form... I also have the Cancel Button set to the button that has the code to stop the drawing.  "

    Sounds like the code to stop the drawing has a problem? Put a break point in that routine, when execution stops at the break, step through the code one line at a time using the debugging tools looking at the values of the variables with intelisense and make sure it does what you think.

    Makes sure your project is set to break execution on errors in the exception settings see:

      http://sandiasoftware.com/vb.net%20depot/Visual%20Studio%20Tips.htm


    I would not use the form cancel button as you show as that is for closing the form mostly. Dont use form cancel for a drawing tool. Find something else. In the example here I used the right mouse button to cancel drawing using the mouseStage variable. You should be able to easily do the same with both rmb and esc key etc.

    https://social.msdn.microsoft.com/Forums/en-US/0902f03f-28b3-474e-ab87-4183810cfa7a/having-trouble-with-saving-my-drawing-in-jpeg-format-to-my-computer-savefiledialog?forum=vbgeneral

    Now you are getting into usabilty of your gui design. And readability of the code logic structure etc.

    You can show an animated gif on the forum up to 1000k. You can show a link to a youtube video.

    Friday, July 27, 2018 10:48 PM