locking out keyboard in textbox

need to lockout all characters except for numbers and the period

Page 1 of 1

9 Replies - 3604 Views - Last Post: 10 December 2009 - 10:17 AM Rate Topic: -----

#1 dengelkes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-December 09

locking out keyboard in textbox

Post icon  Posted 08 December 2009 - 08:16 AM

I am in the process of writing a pos for the first time for a friend for his new restaurant.

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
Is This A Good Question/Topic? 0
  • +

Replies To: locking out keyboard in textbox

#2 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 549
  • Joined: 08-May 08

Re: locking out keyboard in textbox

Posted 08 December 2009 - 08:31 AM

View Postdengelkes, on 8 Dec, 2009 - 09:16 AM, said:

I am in the process of writing a pos for the first time for a friend for his new restaurant.

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.
Was This Post Helpful? 0
  • +
  • -

#3 Bort  Icon User is offline

  • Ill-informed Mongoloid
  • member icon

Reputation: 445
  • View blog
  • Posts: 3,084
  • Joined: 18-September 06

Re: locking out keyboard in textbox

Posted 08 December 2009 - 09:02 AM

Use a MaskedTextBox instead of a TextBox. I'm sure that has ways of limiting what you can type into it.
Was This Post Helpful? 0
  • +
  • -

#4 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 549
  • Joined: 08-May 08

Re: locking out keyboard in textbox

Posted 08 December 2009 - 09:06 AM

View PostBort, on 8 Dec, 2009 - 10:02 AM, said:

Use a MaskedTextBox instead of a TextBox. I'm sure that has ways of limiting what you can type into it.


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

Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: locking out keyboard in textbox

Posted 08 December 2009 - 10:17 AM

Here is a code snippet I submitted that might help you with what you want.

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

Was This Post Helpful? 0
  • +
  • -

#6 Kilborndj  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 14-November 09

Re: locking out keyboard in textbox

Posted 08 December 2009 - 11:13 AM

This is my first attempt at helping other people. This may, or may not be something you could use...but it is a route that I took in my program. Within the text box...during the keypress event...you can try something like this?

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




View Postdengelkes, on 8 Dec, 2009 - 07:16 AM, said:

I am in the process of writing a pos for the first time for a friend for his new restaurant.

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

Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Re: locking out keyboard in textbox

Posted 08 December 2009 - 05:36 PM

Suggested reading: Numbers Only Textboxes
Was This Post Helpful? 0
  • +
  • -

#8 dengelkes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-December 09

Re: locking out keyboard in textbox

Posted 08 December 2009 - 10:01 PM

thank your for all your input but I found a custom control at

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.
Was This Post Helpful? 0
  • +
  • -

#9 Bort  Icon User is offline

  • Ill-informed Mongoloid
  • member icon

Reputation: 445
  • View blog
  • Posts: 3,084
  • Joined: 18-September 06

Re: locking out keyboard in textbox

Posted 09 December 2009 - 06:46 AM

Or...

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.
Was This Post Helpful? 0
  • +
  • -

#10 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 549
  • Joined: 08-May 08

Re: locking out keyboard in textbox

Posted 10 December 2009 - 10:17 AM

If he wants to do it at key press, then he will need to handle said event.

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1