Solved

Using Escape Keypress to Close a Form

Posted on 2004-10-17
5
682 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
  • 3
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
davidrichardson earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Round a string to two digits 12 32
Showdialog 8 29
vb.net checkbox 7 41
Duplicating a tab page content to another in TabControl control 11 41
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…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now