Link to home
Start Free TrialLog in
Avatar of john-formby
john-formbyFlag for Ghana

asked on

Using Escape Keypress to Close a Form

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
ASKER CERTIFIED SOLUTION
Avatar of davidrichardson
davidrichardson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of john-formby

ASKER

Thats brilliant, thanks.

John
Avatar of davidrichardson
davidrichardson

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
I already changed that ;-)

Thanks,

John
No Problem Glad i could help