?
Solved

Using Escape Keypress to Close a Form

Posted on 2004-10-17
5
Medium Priority
?
693 Views
Last Modified: 2008-01-09
Hi all,

I have just started learning VB.NET and have nearly completed a simple VAT Calculator but have run into one slight problem.  I need to be able to allow the user to press the escape key at any time and terminate the program.  I have a close button so I think I should be able to call that procedure when the key is pressed, but I don't know the code and where to put it for the actual keypress.  I am giving 500 points for this cause I need an answer quite quickly.

Here is the code for my form:

Dim vatrate As Decimal
    Dim excvat As Decimal
    Dim incvat As Decimal
    Dim vat As Decimal
    Dim newrate As Decimal

    Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
        txtvatrate.Text = "17.50"
        txtexcvat.Focus()
    End Sub

    Private Sub txtexcvat_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtexcvat.KeyPress
        If e.KeyChar = ControlChars.Cr Then ' if enter key pressed
            e.Handled = True ' remove beep
            btncalcvat_Click(Nothing, Nothing) ' run btncalcvat_click code
        End If
    End Sub

    Private Sub txtincvat_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtincvat.KeyPress
        If e.KeyChar = ControlChars.Cr Then ' if enter key pressed
            e.Handled = True ' remove beep
            btncalcb4vat_Click(Nothing, Nothing) ' run btncalcb4vat_click code
        End If
    End Sub

    Private Sub btncalcvat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncalcvat.Click
        If txtexcvat.Text = "" Then
            MsgBox("No Input", MsgBoxStyle.Information, "VAT Calculator Error")
            txtexcvat.Focus()
        Else
            If Not IsNumeric(txtexcvat.Text) Then
                MsgBox("Invalid Input", MsgBoxStyle.Information, "VAT Calculator Error")
                txtexcvat.Text = ""
                txtexcvat.Focus()
            Else
                excvat = Val(txtexcvat.Text)
                vatrate = Val(txtvatrate.Text)
                vat = excvat * vatrate / 100
                incvat = excvat + vat
                txtvat.Text = Format(vat, "0.00")
                txtincvat.Text = Format(incvat, "0.00")
                btnclear.Focus()
            End If
        End If
    End Sub

    Private Sub btnclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclear.Click
        txtexcvat.Text = ""
        txtvat.Text = ""
        txtincvat.Text = ""
        txtexcvat.Focus()
    End Sub

    Private Sub btncalcb4vat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncalcb4vat.Click
        If txtincvat.Text = "" Then
            MsgBox("No Input", MsgBoxStyle.Information, "VAT Calculator Error")
            txtincvat.Focus()
        Else
            If Not IsNumeric(txtincvat.Text) Then
                MsgBox("Invalid Input", MsgBoxStyle.Information, "VAT Calculator Error")
                txtincvat.Text = ""
                txtincvat.Focus()
            Else
                incvat = Val(txtincvat.Text)
                vatrate = Val(txtvatrate.Text)
                vat = incvat / (vatrate + 100) * vatrate
                excvat = incvat - vat
                txtvat.Text = Format(vat, "0.00")
                txtexcvat.Text = Format(excvat, "0.00")
                btnclear.Focus()
            End If
        End If
    End Sub

    Private Sub btnsetrate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsetrate.Click
        If txtvatrate.Text = "" Then
            MsgBox("No Input", MsgBoxStyle.Information, "VAT Calculator Error")
            txtvatrate.Focus()
        Else
            If Not IsNumeric(txtvatrate.Text) Then
                MsgBox("Invalid Input", MsgBoxStyle.Information, "VAT Calculator Error")
                txtvatrate.Focus()
            Else
                vatrate = txtvatrate.Text
                txtvatrate.Text = Format(vatrate, "0.00")
                txtexcvat.Text = ""
                txtvat.Text = ""
                txtincvat.Text = ""
                txtexcvat.Focus()
            End If
        End If
    End Sub

    Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclose.Click
        Me.Hide()
    End Sub

End Class



Thanks in advance,

John
0
Comment
Question by:john-formby
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
davidrichardson earned 2000 total points
ID: 12334301
   Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean

        If msg.WParam.ToInt32() = CInt(Keys.Escape) Then

            Form.Close()

            Return True

        End If

        Return MyBase.ProcessCmdKey(msg, keyData)

    End Function 'ProcessCmdKey
0
 
LVL 14

Author Comment

by:john-formby
ID: 12334320
Thats brilliant, thanks.

John
0
 
LVL 7

Expert Comment

by:davidrichardson
ID: 12334324
try again

   Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean

        If msg.WParam.ToInt32() = CInt(Keys.Escape) Then

            me.Close()

            Return True

        End If

        Return MyBase.ProcessCmdKey(msg, keyData)

    End Function 'ProcessCmdKey
0
 
LVL 14

Author Comment

by:john-formby
ID: 12334332
I already changed that ;-)

Thanks,

John
0
 
LVL 7

Expert Comment

by:davidrichardson
ID: 12334338
No Problem Glad i could help
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question