By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,312 Members | 1,248 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,312 IT Pros & Developers. It's quick & easy.

How to limit data entry to numbers only in combobox

P: n/a
I have a combobox where a user can select a zoom factor or enter a
zoom factor.
I tried to limit entry into the combobox to numbers only by catching
the keydown event and setting e.handled = true if any non number
characters are entered.
But those characters still appear in the combobox. Is this a combobox
bug?
If yes any workarounds?

Tosch
Nov 21 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Hi Tosch,

Use the keypress event. The following worked fine for me, disallowing 'a':
If e.KeyChar = "a" Then

e.Handled = True

End If

HTH,

Bernie Yaeger

"Tosch" <to**********@swissonline.ch> wrote in message
news:18********************************@4ax.com...
I have a combobox where a user can select a zoom factor or enter a
zoom factor.
I tried to limit entry into the combobox to numbers only by catching
the keydown event and setting e.handled = true if any non number
characters are entered.
But those characters still appear in the combobox. Is this a combobox
bug?
If yes any workarounds?

Tosch

Nov 21 '05 #2

P: n/a
Hi Tosch,

Use the keypress event. The following worked fine for me, disallowing 'a':
If e.KeyChar = "a" Then

e.Handled = True

End If

HTH,

Bernie Yaeger

"Tosch" <to**********@swissonline.ch> wrote in message
news:18********************************@4ax.com...
I have a combobox where a user can select a zoom factor or enter a
zoom factor.
I tried to limit entry into the combobox to numbers only by catching
the keydown event and setting e.handled = true if any non number
characters are entered.
But those characters still appear in the combobox. Is this a combobox
bug?
If yes any workarounds?

Tosch

Nov 21 '05 #3

P: n/a
This is my code and it's not working. I want only numbers 0-9
accepted:

Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
Select Case e.KeyChar
Case "0" To "9"
Case Else
e.Handled = True
End Select
End Sub

BTW: I'm using VS2002.

Tosch
On Thu, 11 Nov 2004 09:58:23 -0500, "Bernie Yaeger"
<be*****@cherwellinc.com> wrote:
Hi Tosch,

Use the keypress event. The following worked fine for me, disallowing 'a':
If e.KeyChar = "a" Then

e.Handled = True

End If

HTH,

Bernie Yaeger

"Tosch" <to**********@swissonline.ch> wrote in message
news:18********************************@4ax.com.. .
I have a combobox where a user can select a zoom factor or enter a
zoom factor.
I tried to limit entry into the combobox to numbers only by catching
the keydown event and setting e.handled = true if any non number
characters are entered.
But those characters still appear in the combobox. Is this a combobox
bug?
If yes any workarounds?

Tosch


Nov 21 '05 #4

P: n/a
This is my code and it's not working. I want only numbers 0-9
accepted:

Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
Select Case e.KeyChar
Case "0" To "9"
Case Else
e.Handled = True
End Select
End Sub

BTW: I'm using VS2002.

Tosch
On Thu, 11 Nov 2004 09:58:23 -0500, "Bernie Yaeger"
<be*****@cherwellinc.com> wrote:
Hi Tosch,

Use the keypress event. The following worked fine for me, disallowing 'a':
If e.KeyChar = "a" Then

e.Handled = True

End If

HTH,

Bernie Yaeger

"Tosch" <to**********@swissonline.ch> wrote in message
news:18********************************@4ax.com.. .
I have a combobox where a user can select a zoom factor or enter a
zoom factor.
I tried to limit entry into the combobox to numbers only by catching
the keydown event and setting e.handled = true if any non number
characters are entered.
But those characters still appear in the combobox. Is this a combobox
bug?
If yes any workarounds?

Tosch


Nov 21 '05 #5

P: n/a
Tosch,

Tosch,

Some hours ago I showed this sample too EDB he told me that it was what he
wanted, however, that it can be optimized, and he is right, it where first
two samples.

http://groups.google.com/groups?selm...TNGP12.phx.gbl

I did not change them yet.

I hope however that it helps you as well.

Cor

"Tosch" <to**********@swissonline.ch>
This is my code and it's not working. I want only numbers 0-9
accepted:

Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
Select Case e.KeyChar
Case "0" To "9"
Case Else
e.Handled = True
End Select
End Sub

BTW: I'm using VS2002.

Tosch
On Thu, 11 Nov 2004 09:58:23 -0500, "Bernie Yaeger"
<be*****@cherwellinc.com> wrote:
Hi Tosch,

Use the keypress event. The following worked fine for me, disallowing
'a':
If e.KeyChar = "a" Then

e.Handled = True

End If

HTH,

Bernie Yaeger

"Tosch" <to**********@swissonline.ch> wrote in message
news:18********************************@4ax.com. ..
I have a combobox where a user can select a zoom factor or enter a
zoom factor.
I tried to limit entry into the combobox to numbers only by catching
the keydown event and setting e.handled = true if any non number
characters are entered.
But those characters still appear in the combobox. Is this a combobox
bug?
If yes any workarounds?

Tosch

Nov 21 '05 #6

P: n/a
Tosch,

Tosch,

Some hours ago I showed this sample too EDB he told me that it was what he
wanted, however, that it can be optimized, and he is right, it where first
two samples.

http://groups.google.com/groups?selm...TNGP12.phx.gbl

I did not change them yet.

I hope however that it helps you as well.

Cor

"Tosch" <to**********@swissonline.ch>
This is my code and it's not working. I want only numbers 0-9
accepted:

Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
Select Case e.KeyChar
Case "0" To "9"
Case Else
e.Handled = True
End Select
End Sub

BTW: I'm using VS2002.

Tosch
On Thu, 11 Nov 2004 09:58:23 -0500, "Bernie Yaeger"
<be*****@cherwellinc.com> wrote:
Hi Tosch,

Use the keypress event. The following worked fine for me, disallowing
'a':
If e.KeyChar = "a" Then

e.Handled = True

End If

HTH,

Bernie Yaeger

"Tosch" <to**********@swissonline.ch> wrote in message
news:18********************************@4ax.com. ..
I have a combobox where a user can select a zoom factor or enter a
zoom factor.
I tried to limit entry into the combobox to numbers only by catching
the keydown event and setting e.handled = true if any non number
characters are entered.
But those characters still appear in the combobox. Is this a combobox
bug?
If yes any workarounds?

Tosch

Nov 21 '05 #7

P: n/a
"Tosch" <to**********@swissonline.ch> schrieb:
This is my code and it's not working. I want only numbers 0-9
accepted:

Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
Select Case e.KeyChar
Case "0" To "9"
Case Else
e.Handled = True
End Select
End Sub

BTW: I'm using VS2002.


IIRC that's caused by the buggy implementation of the combobox.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #8

P: n/a
"Tosch" <to**********@swissonline.ch> schrieb:
This is my code and it's not working. I want only numbers 0-9
accepted:

Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
Select Case e.KeyChar
Case "0" To "9"
Case Else
e.Handled = True
End Select
End Sub

BTW: I'm using VS2002.


IIRC that's caused by the buggy implementation of the combobox.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #9

P: n/a
Hi Tosch,

I tried your code inside vs 2003, which is all that I work with, and it
worked fine. So the answer has to be that there's a bug in 2002. Also, you
have to add a few characters - backspace, delete, arrow keys, etc. So you
may want to modify your code, into something like this:

Dim KeyAscii As Integer = Asc(e.KeyChar)
Select Case KeyAscii

Case 8, 27, 48 To 57, 9

Case Else

KeyAscii = 0

End Select

If KeyAscii = 0 Then

e.Handled = True

Else

e.Handled = False

End If
HTH,

Bernie Yaeger

"Tosch" <to**********@swissonline.ch> wrote in message
news:8r********************************@4ax.com...
This is my code and it's not working. I want only numbers 0-9
accepted:

Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
Select Case e.KeyChar
Case "0" To "9"
Case Else
e.Handled = True
End Select
End Sub

BTW: I'm using VS2002.

Tosch
On Thu, 11 Nov 2004 09:58:23 -0500, "Bernie Yaeger"
<be*****@cherwellinc.com> wrote:
Hi Tosch,

Use the keypress event. The following worked fine for me, disallowing
'a':
If e.KeyChar = "a" Then

e.Handled = True

End If

HTH,

Bernie Yaeger

"Tosch" <to**********@swissonline.ch> wrote in message
news:18********************************@4ax.com. ..
I have a combobox where a user can select a zoom factor or enter a
zoom factor.
I tried to limit entry into the combobox to numbers only by catching
the keydown event and setting e.handled = true if any non number
characters are entered.
But those characters still appear in the combobox. Is this a combobox
bug?
If yes any workarounds?

Tosch

Nov 21 '05 #10

P: n/a
Hi Tosch,

I tried your code inside vs 2003, which is all that I work with, and it
worked fine. So the answer has to be that there's a bug in 2002. Also, you
have to add a few characters - backspace, delete, arrow keys, etc. So you
may want to modify your code, into something like this:

Dim KeyAscii As Integer = Asc(e.KeyChar)
Select Case KeyAscii

Case 8, 27, 48 To 57, 9

Case Else

KeyAscii = 0

End Select

If KeyAscii = 0 Then

e.Handled = True

Else

e.Handled = False

End If
HTH,

Bernie Yaeger

"Tosch" <to**********@swissonline.ch> wrote in message
news:8r********************************@4ax.com...
This is my code and it's not working. I want only numbers 0-9
accepted:

Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
Select Case e.KeyChar
Case "0" To "9"
Case Else
e.Handled = True
End Select
End Sub

BTW: I'm using VS2002.

Tosch
On Thu, 11 Nov 2004 09:58:23 -0500, "Bernie Yaeger"
<be*****@cherwellinc.com> wrote:
Hi Tosch,

Use the keypress event. The following worked fine for me, disallowing
'a':
If e.KeyChar = "a" Then

e.Handled = True

End If

HTH,

Bernie Yaeger

"Tosch" <to**********@swissonline.ch> wrote in message
news:18********************************@4ax.com. ..
I have a combobox where a user can select a zoom factor or enter a
zoom factor.
I tried to limit entry into the combobox to numbers only by catching
the keydown event and setting e.handled = true if any non number
characters are entered.
But those characters still appear in the combobox. Is this a combobox
bug?
If yes any workarounds?

Tosch

Nov 21 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.