Solved

Using Escape Keypress to Close a Form

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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