.NET Framework - Newbie - Why can't I use backspace key?

Asked By Jem on 04-Nov-07 04:33 AM

I'm REALLY new to DotNet and could use a little help/advice please.

I have the following code which i've basically hacked together from a
similar VB routine...

Private Sub txtExpReqdForPlate_KeyPress(ByVal eventSender As
System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs)
Handles txtExpReqdForPlate.KeyPress
Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
'   Make sure values are numeric and accept + and - and .
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
If KeyAscii <> 13 And KeyAscii <> 43 And KeyAscii <> 45 And_
KeyAscii <> 46 And KeyAscii <> 127 And KeyAscii <> 8 Then
KeyAscii = 0
End If
KeyAscii = 0
End If
eventArgs.KeyChar = Chr(KeyAscii)
If KeyAscii = 0 Then
eventArgs.Handled = True
End If
End Sub

This is used to only allow numeric characters (and a few others inc. the
backspace key) in a textbox.  Why doesn't it let me use the backspace key?

As I said i'm really new to this and confess I don't really understand what
i'm doing, although I am trying to learn (slowly).  Can any answers please
be as simple as possible otherwise it'll probably go straight over my head -



guff replied on 04-Nov-07 05:14 AM
The above If statement allows the backspace key, but then you make the
If statement pointless, as you set the KeyAscii value to zero anyway in
the next line.

Göran Andersson
Stephany Young replied on 04-Nov-07 05:20 AM
So, you want the user to be able to enter values comprising the characters
0, 1, 2 ,3, 4, 5, 6, 7, 8, 9, +, -, ., and, when the Enter/Return key is
pressed you want to advance the focus to the next control in the Tab order.

You also want the user to be able to press other control keys.

Try something like:

Private m_allowed As New List(Of Char){New Char() {"0"c, "1"c, "2"c, "3"c,

Private Sub txtExpReqdForPlate_KeyPress(ByVal sender As Object, ByVal e As
KeyPressEventArgs) Handles txtExpReqdForPlate.KeyPress

e.Handled = (AscW(e.KeyChar) >= 32 AndAlso AscW(e.KeyChar) < 127 AndAlso
Not m_allowed.Contains(e.KeyChar))

If e.KeyChar = Convert.ToChar(Keys.Return) Then
e.Handled = True
True, True, True, True)
End If

End Sub

The restrictions in VB6 that meant if you started mucking about with certain
keys in the KeyPress event then you had to deal with the lot are no longer
an i issue in VB.NET.

What is happening here is that e.handled will be set to True if the key that
was pressed is from ASCII 32 to 126 inclusive and is NOT in the defined
generic list, (a sort of super-duper array), m_allowed.

The only other key we need to worry about is the Enter/Retrun key and if
that is pressed we set e.Handled to True and use the Control class method
SelectNextControl to advance to the next control in the Tab order.

Note also, the use of AndAlso instead of And.

F1 is your friend here. Read up on the new features and don't get bogged
down on the way you you used to code something.
Tom Shelton replied on 04-Nov-07 05:21 AM
-------------------This is your problem --------------------

It's being done for every key that is not a 0 through 9, since it
isn't inside your exlusion test.

Here is a corrected (and sort of cleaned up version):

Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal
e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar < "0"c OrElse e.KeyChar > "9"c Then
If e.KeyChar <> "+"c AndAlso e.KeyChar <> "-"c AndAlso
e.KeyChar <> "."c AndAlso e.KeyChar <> ControlChars.Back AndAlso
e.KeyChar <> ControlChars.Cr AndAlso e.KeyChar <>
Convert.ToChar(Keys.Delete) Then
e.KeyChar = ControlChars.Tab
e.Handled = True
End If
End If
End Sub

Jem - be aware, that this will NOT stop users form copying and pasting
illegal text into your textbox.  The only reliable way to do that is
to subclass the control, and check the clipboard data on a wm_paste
message - that is if you don't want to disable pasting altogheter and
still disallow illegal values.

Tom Shelton
Jem replied on 04-Nov-07 05:36 AM
Wow, Quick response.

Thanks folks, i'm sort of embarrassed now it's been pointed out to me.  Why
couldn't i see that?

I was never really that good a programmer in VB as I only write programs for
my own use.  Making the transition to .Net is a long and torturous route,
but I don't like to be beaten, it's just that sometimes even the simplist
tasks become frustrating ;-)

Thanks again, I really appreciate you taking the time to help me overcome
these difficulties.  They may seem simple to you, but to me they are real
mountains that have to be climbed.  When I reach that brick wall that blocks
the path ahead it's nice to be able to call on expert help for guidance.


Cor Ligthert[MVP] replied on 04-Nov-07 06:28 AM

If you use the Key up then you get direct that key in your e evenent

A lot easier, a key that is pressed has forever to come up again in the
standard events from .Net.

Jem replied on 04-Nov-07 04:15 PM
Thanks Tom, I've implemented your cleaned up code below which works
beautifully apart from one thing.  I would like the focus to move to the
next textbox in line when the user presses the 'enter/return' key.  I have
come to the point of virtual self destruction trying to do this.  It was so
easy in VB but i'm really struggling with how to do this and fit it into
your code example below.  I've been trying different things for a couple of
hours now and have lost the will to live :-)

Any chance of an example as to how to do this?

Many thanks & sorry to bother you again.

Stephany Young replied on 04-Nov-07 05:50 PM
I gave you the code for this in my earlier response.

If you have lost the will to live after wrestling with a problem for only 2
hours then you don't know you're alive.

The three most important things in programming are patience, patience and
Herfried K. Wagner [MVP] replied on 04-Nov-07 04:37 PM
Enter Instead of Tab | Duncan Mackenzie .NET

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Jem replied on 05-Nov-07 12:33 PM
Oh Dear...

Sorry Stephany, in my nievety i thought it was only newbie numpties like me
that struggled for so long over a simple problem ;-)

I did try your code but it was even further outside my comfort zone than
Tom's code, and when it gave me an error I didn't know where to start,

I do have patience, honest, it's just that it seems sucha simple thing to
want to do - press 'enter' and move to the next textbox.  I have a feeling
that i'm either going to have to shelve learning .Net OR go right back to
the very basics again.

I didn't mean to offend anyone with asking for more help, when that brick
wall gets in the way over something so simple it's sometimes hard to see
what's on the other side.