locking out keyboard in textboxneed to lockout all characters except for numbers and the period
Page 1 of 1
9 Replies - 2334 Views - Last Post: 10 December 2009 - 10:17 AM
#1
locking out keyboard in textbox
Posted 08 December 2009 - 08:16 AM
I am need to allow users to input floating numbers only.
How do lock out the keyboard?
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Replies To: locking out keyboard in textbox
#2
Re: locking out keyboard in textbox
Posted 08 December 2009 - 08:31 AM
dengelkes, on 8 Dec, 2009 - 09:16 AM, said:
I am need to allow users to input floating numbers only.
How do lock out the keyboard?
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Instead of using the TextChanged event....use the KeyDown event to capture this. It will be alot more intensive as you will have to capture the ASCII codes (number value) of the keys pressed. Its not hard but probably want to use a Select....Case to validate the Integer values you want to accept. You can also capture the length of the string being inputed.
#3
Re: locking out keyboard in textbox
Posted 08 December 2009 - 09:02 AM
#4
Re: locking out keyboard in textbox
Posted 08 December 2009 - 09:06 AM
Bort, on 8 Dec, 2009 - 10:02 AM, said:
A MaskedTextBox is derived from the TexBox control, and since he is developing something for POS, the system isnt a normal computer and the maskedcontrol has some other functionalities that would get loaded that he/she may not need and create more overhead than necessary.
Since this is a POS system, i would suggest only using what you absolutely need to have, and in most cases means developing ontop of the base control.
This post has been edited by woodjom: 08 December 2009 - 09:08 AM
#5
Re: locking out keyboard in textbox
Posted 08 December 2009 - 10:17 AM
http://www.dreaminco...snippet4636.htm
More specifically, check out the SingleDecimal sub
This post has been edited by CharlieMay: 08 December 2009 - 10:20 AM
#6
Re: locking out keyboard in textbox
Posted 08 December 2009 - 11:13 AM
I have been told that "e.KeyChar = e.KeyChar" is redundant. But no one has been able to specify a less redundant way? If you know of a better way, then by all means do so.
Using this will only allow the key strokes of 0 - 9, backspace, spacebar and only 1 " . " in the text box. All others are thrown out.
Private Sub txtbox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtbox.KeyPress
If e.KeyChar >= "0" And e.KeyChar <= "9" Then 'Allows only numbers
e.KeyChar = e.KeyChar
ElseIf Asc(e.KeyChar) = 8 Then 'Allows Backspace to be used
e.KeyChar = e.KeyChar
ElseIf e.KeyChar = " "c Then 'Allows "Spacebar" to be used
e.KeyChar = e.KeyChar
ElseIf e.KeyChar = "." Then
If txtbox.Text.IndexOf(".") > -1 Then 'Allows " . " and prevents more than 1 " . "
e.Handled = True
Beep()
End If
Else
e.Handled = True 'Disallows all other characters from being used on txtbox.Text
Beep()
End If
End Sub
dengelkes, on 8 Dec, 2009 - 07:16 AM, said:
I am need to allow users to input floating numbers only.
How do lock out the keyboard?
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
#7
Re: locking out keyboard in textbox
Posted 08 December 2009 - 05:36 PM
#8
Re: locking out keyboard in textbox
Posted 08 December 2009 - 10:01 PM
http://www.codeproje.../2gs_txtbx.aspx
one of the properties is keystrokes which only a few selections and one of the selections is numbers only.
This might help other people needing to do the same thing.
I have upload the .dll for other people to use.
#9
Re: locking out keyboard in textbox
Posted 09 December 2009 - 06:46 AM
If TextBox1.Text IsNumberic ... End If
It won't quite get the formatting of the text, but it will return an error if there is anything but numbers in there.
#10
Re: locking out keyboard in textbox
Posted 10 December 2009 - 10:17 AM
If he wants to validate the input then i would suggest using a Regular Expression and a Validation Control for marking said invalid input.
All being said and done, there are multiple ways to perform the same task, the key is to note that you need to analyze the capability of the system and size requirements of the application for the need.
|
|

New Topic/Question
Reply




MultiQuote







|