如何仅在组合框中将数据输入限制为数字 [英] How to limit data entry to numbers only in combobox

查看:68
本文介绍了如何仅在组合框中将数据输入限制为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组合框,用户可以选择缩放系数或输入

缩放系数。

我试图通过捕捉来限制进入组合框的数字

keydown事件和设置e.handled = true如果输入任何非数字

字符。

但这些字符仍出现在组合框中。这是一个组合框

的错误吗?

如果是的话有任何解决方法吗?


Tosch

解决方案

您好Tosch,


使用按键事件。以下工作对我来说很好,不允许''a'':

如果e.KeyChar =a然后


e.Handled = True


结束如果


HTH,


Bernie Yaeger


" Tosch" <到********** @ swissonline.ch>在消息中写道

news:18 ******************************** @ 4ax.com ...

我有一个组合框,用户可以选择缩放系数或输入缩放系数。
我试图通过捕捉来限制进入组合框的数字
keydown事件和设置e.handled = true如果输入任何非数字
字符。
但这些字符仍然出现在组合框中。这是一个组合框
错误吗?
如果是的话有任何解决方法吗?

Tosch



Hi Tosch,


使用按键事件。以下工作对我来说很好,不允许''a'':

如果e.KeyChar =a然后


e.Handled = True


结束如果


HTH,


Bernie Yaeger


" Tosch" <到********** @ swissonline.ch>在消息中写道

news:18 ******************************** @ 4ax.com ...

我有一个组合框,用户可以选择缩放系数或输入缩放系数。
我试图通过捕捉来限制进入组合框的数字
keydown事件和设置e.handled = true如果输入任何非数字
字符。
但这些字符仍然出现在组合框中。这是一个组合框
错误吗?
如果是的话有任何解决方法吗?

Tosch



这是我的代码,它不起作用。我只想要接受0-9

的数字:


Private Sub ComboBox1_KeyPress(ByVal发送者作为对象,ByVal e As

系统.Windows.Forms.KeyPressEventArgs)处理ComboBox1.KeyPress

Select Case e.KeyChar

Case" 0"至9

Case Else

e.Handled = True

结束选择

End Sub


BTW:我正在使用VS2002。


Tosch

2004年11月11日星期四09:58: 23 -0500,Bernie Yaeger

< be ***** @ cherwellinc.com>写道:

您好Tosch,

使用按键事件。以下工作对我来说很好,不允许''a'':
如果e.KeyChar =a然后

e.Handled = True

结束如果

HTH,

Bernie Yaeger

" Tosch的" <到********** @ swissonline.ch>在消息中写道
新闻:18 ******************************** @ 4ax.com .. < blockquote class =post_quotes>我有一个组合框,用户可以在其中选择缩放系数或输入缩放系数。
我试图通过捕捉来限制进入组合框的数字组合
keydown事件和设置e.handled = true如果输入任何非数字
字符。
但这些字符仍然出现在组合框中。这是一个组合框
错误吗?
如果是的话有任何解决方法吗?

Tosch




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

解决方案

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



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



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




这篇关于如何仅在组合框中将数据输入限制为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆