?
Solved

Vb.net text box "e.KeyChar = ControlChars.Cr"

Posted on 2009-02-20
4
Medium Priority
?
8,713 Views
Last Modified: 2012-05-06
Hello,

I am trying to make a text box that has the following properties...

1. Only numeric information is acceptable
2. The backspace key gives focus to a different text box
3. The enter key give focus to a different text box

It seems to be working except for the enter key, nothing happens when I hit the enter key while in the text box.
Private Sub WeightIn_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles WeightIn.KeyPress
        If Convert.ToInt32(e.KeyChar) = 8 Then
            e.Handled = False
            If e.KeyChar = ControlChars.Back Then
                WeightIn.Text = ""
                WBEndIn.Focus()
            ElseIf e.KeyChar = ControlChars.Cr Then
                BCIn.Focus()
            End If
        ElseIf Not Char.IsDigit(e.KeyChar) Then
            e.Handled = True
        Else
            e.Handled = False
        End If
    End Sub

Open in new window

0
Comment
Question by:Navicerts
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 2000 total points
ID: 23691254
Try this....
    Private Sub WeightIn_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles WeightIn.KeyPress
 
        If Not Char.IsNumber(e.KeyChar) Then
            Select Case Convert.ToInt32(e.KeyChar)
                Case 8 'backspace
                    WeightIn.Text = ""
                    WBEndIn.Focus()
                Case 13 'enter
                    BCIn.Focus()
                Case Else
                    e.Handled = True
            End Select
        End If
 
    End Sub

Open in new window

0
 
LVL 7

Author Comment

by:Navicerts
ID: 23691273
Perfect :)
0
 
LVL 1

Expert Comment

by:KCFORSYTHE
ID: 23691296
Hi,

Try this.  You just had your check for the Cr in the wrong spot.  Also, I changed the WeightIn.Text="" to WeighIn.Text="" assuming this was a typo.  If WeightIn is a different textbox than the three that you mention, then it will need to be changed back to your original.

Good luck.


    Private Sub WeighIn_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles WeighIn.KeyPress
        If Convert.ToInt32(e.KeyChar) = 8 Then
            e.Handled = False
            If e.KeyChar = ControlChars.Back Then
                WeighIn.Text = ""
                WBEndIn.Focus()
            End If
        ElseIf Convert.ToInt32(e.KeyChar) = 13 Then
            BCIn.Focus()
        ElseIf Not Char.IsDigit(e.KeyChar) Then
            e.Handled = True
        Else
            e.Handled = False
        End If
 
    End Sub

Open in new window

0
 
LVL 7

Author Comment

by:Navicerts
ID: 23691305
Ahh, well that helps me understand why my code was not working.  Thank you sir :)  A bit late for the points so a thanks will have to do!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

840 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