Solved

Using Escape Keypress to Close a Form

Posted on 2004-10-17
5
679 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

22 Experts available now in Live!

Get 1:1 Help Now