|
-
Apr 2nd, 2002, 10:56 AM
#1
Thread Starter
Hyperactive Member
Validating KeyPress
I wish to Validate entry in a textbox so that only uppercse text is entered. If Lowercase text i sentered I wish to convert it automatically to uppercase.
In vb6 - I could use the keypress event with keyascii.
For example - to convert to uppercase.
VB Code:
If KeyAscii >= 97 And KeyAscii <= 122 Then
KeyAscii = KeyAscii - 32
End If
Looking at .net - I can do something similar,
VB Code:
Private Sub txtJobCode_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtJobCode.KeyPress
If e.KeyChar.IsLower(e.KeyChar) Then
e.KeyChar.ToUpper(e.KeyChar)
End If
End Sub
but I can't figure out how to reset the key that was pressed (The above example, doesnt change what was entered in the text box).
any ideas?
-
Apr 2nd, 2002, 05:01 PM
#2
Addicted Member
well i ran into the same type of problem, but if all you want to do is change the text to upper case do this after they tab away or in the changetext event because the keychar, keycode, keyascii object are read only so you cant change them
Code:
txtTest.Text = txtTest.Text.ToUpper
 ender_pete 
C#,VS.NET Ent Arch, vb6 ee sp5,html,vbscript,jscript,
xml,dhtml,delphi,c++,vc++,java,cgi,php, python, ada(so ancient) ,adasage(also ancient) and others i can't remember.....
-
Apr 2nd, 2002, 07:10 PM
#3
Frenzied Member
easy, just set the e.Handled to true and the system will discard the KeyPress...
for example this would block the user from entering the character c:
VB Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = "c" Then e.Handled = True
End Sub
... hmm now that I re-read your post I'm not sure if this is what you actually meant
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Apr 3rd, 2002, 03:15 AM
#4
Thread Starter
Hyperactive Member
Thanks for the replies jop and ender_pete
I did it as ender_pete suggested in the end (in the textchanged event), although I found I had to save the position of the selectionstart property and reset it afterwards.
VB Code:
Dim intX As Int32 = txtJobCode.SelectionStart
txtJobCode.Text = txtJobCode.Text.ToUpper()
txtJobCode.SelectionStart = intX
Jops suggetion was useful for disallowing any character that isn't a Letter or Backspace...
VB Code:
If Not e.KeyChar = ControlChars.Back Then
If Not e.KeyChar.IsLetter(e.KeyChar) Then
Beep()
e.Handled = True
End If
End If
Thanks guys
-
Apr 3rd, 2002, 09:21 AM
#5
Lively Member
there's also a new property for textboxes called CharacterCasing
it can be set to normal, upper or lower.
if set to upper, the textbox will automatically change lower case letters to uppercase
-
Apr 3rd, 2002, 09:28 AM
#6
Thread Starter
Hyperactive Member
Thanks Nina,
I should have known there was an easy .Net way of doing it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|