Textbox和Combox限制只能输入数字的问题?

lilysunny13 2004-05-11 05:40:39
针对Textbox和Combox我分别写了一样的KeyPress函数,为什么Textbox执行得很好,而Combox的没有效果呢?
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim MidStr As String
MidStr = "0123456789"
If MidStr.IndexOf(e.KeyChar) = -1 Then
e.Handled = True
End If
End Sub

Private Sub ComboxBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim MidStr As String
MidStr = "0123456789"
If MidStr.IndexOf(e.KeyChar) = -1 Then
e.Handled = True
End If
End Sub
...全文
227 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
eTopFancy 2004-05-14
  • 打赏
  • 举报
回复
我用c#测试了一下,先设定ComboBox1的KeyPress事件,并设定TextBox1共用ComboBox1的事件没问题,代码:
private void comboBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar == (Char)13)
return;
else
{
string MidStr = "0123456789";
if(MidStr.IndexOf(e.KeyChar) == -1)
{
e.Handled = true;
MessageBox.Show("Please input Number!");
}
}
}
lilysunny13 2004-05-14
  • 打赏
  • 举报
回复
哪位帮我测试一下?
Goshawkx 2004-05-13
  • 打赏
  • 举报
回复
关注一下
lilysunny13 2004-05-13
  • 打赏
  • 举报
回复
to:flyingbirddhp()
不好意思我是在Vb.net下,不能用“==”判断啊!

大家请注意我的问题,我的代码在TextBox1_KeyPress时是有效的执行得很好,可是在ComboxBox1_KeyPress中就一点作用都没有!这是怎么回事!
lilysunny13 2004-05-13
  • 打赏
  • 举报
回复
to:yanwc() 我是想实现ComboBox1的KeyPress,TextBox1_KeyPress只是用来测试我写的代码对不对罢了!
yanwc 2004-05-13
  • 打赏
  • 举报
回复
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress,ComboBox1.KeyPress
Dim MidStr As String
MidStr = "0123456789"
If MidStr.IndexOf(e.KeyChar) = -1 Then
e.Handled = True
End If
End Sub
zlz1981 2004-05-12
  • 打赏
  • 举报
回复
用正则表达式试试
lilysunny13 2004-05-12
  • 打赏
  • 举报
回复
哪个遇到过?或者时间比较空可以帮你看看吗?
flyingbirddhp 2004-05-12
  • 打赏
  • 举报
回复
private void txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
string allowed = "0123456789";
if(e.KeyChar == (char)13)
{
return;
}
if(allowed.IndexOf(e.KeyChar) == -1)
{
MessageBox.Show("只能输入数字");
this.txt.Text = "";
e.Handled=true;
}
}

private void cmb_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
string allowed = "0123456789";
if(e.KeyChar == (char)13)
{
return;
}
if(allowed.IndexOf(e.KeyChar) == -1)
{
MessageBox.Show("只能输入数字");
this.cmb.Text = "";
e.Handled=true;
}
}
flyingbirddhp 2004-05-12
  • 打赏
  • 举报
回复
呵呵,很明显拉,
If MidStr.IndexOf(e.KeyChar) == -1
应该是两个等号!
我测试过了,没有问题,代码我贴出来
Lmovie 2004-05-12
  • 打赏
  • 举报
回复
UP
Lmovie 2004-05-12
  • 打赏
  • 举报
回复
UP
lilysunny13 2004-05-12
  • 打赏
  • 举报
回复
而且我发现我改成e.Handled = False什么影响都没有,还是一样什么都能输入,好奇怪!
lilysunny13 2004-05-12
  • 打赏
  • 举报
回复
设了,我输入字符的时候e.Handled = True语句都执行了,可是字符仍然能输入进去,真是奇怪!
taofirst 2004-05-12
  • 打赏
  • 举报
回复
没有问题啊,你设个断点看看执行了没有
lilysunny13 2004-05-12
  • 打赏
  • 举报
回复
不好意思,是我拷贝的时候出错了其实代码里handles的地方没有问题
Private Sub ComboxBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboxBox1.KeyPress
Dim MidStr As String
MidStr = "0123456789"
If MidStr.IndexOf(e.KeyChar) = -1 Then
e.Handled = True
End If
End Sub

请问是什么问题呢?
zheninchangjiang 2004-05-12
  • 打赏
  • 举报
回复
我做的一个类,看看对你有没有用
Imports System
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Public Class numtbx
Inherits System.Windows.Forms.TextBox

#Region " 组件设计器生成的代码 "

Public Sub New()
MyBase.New()

' 该调用是组件设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'Control 重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'控件设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是组件设计器所必需的
' 可以使用组件设计器修改此过程。不要使用
' 代码编辑器修改它。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container
End Sub

#End Region
Dim intdecimal As Int16 = 0
Public Property DecimalLength() As Int16 '设置小数位
Get
DecimalLength = intdecimal
End Get
Set(ByVal Value As Int16)
intdecimal = Value
End Set
End Property

Protected Overrides Sub OnPaint(ByVal pe As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(pe)

'在此添加自定义绘画代码
End Sub
Protected Overrides Sub onkeypress(ByVal e As KeyPressEventArgs)
If IsNumeric(e.KeyChar) Then
If MyBase.Text.Trim.IndexOf(".") > -1 Then
If MyBase.Text.Trim.Length - MyBase.Text.Trim.IndexOf(".") - 1 >= intdecimal Then
e.Handled = True
End If
End If
Else
Select Case e.KeyChar
Case "-"
If MyBase.Text.Trim.Length > 0 Then
e.Handled = True
End If
Case "."
If MyBase.Text.Trim.IndexOf(".") > -1 Then
e.Handled = True
End If
Case Else
e.Handled = True
End Select
End If
End Sub
End Class
hitwjj311 2004-05-12
  • 打赏
  • 举报
回复
对于 textbox 建议直接设置属性type 为 number
taofirst 2004-05-11
  • 打赏
  • 举报
回复
Private Sub ComboxBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
Dim MidStr As String
MidStr = "0123456789"
If MidStr.IndexOf(e.KeyChar) = -1 Then
e.Handled = True
End If
End Sub
taofirst 2004-05-11
  • 打赏
  • 举报
回复
执行的是同一事件 TextBox1.KeyPress

16,718

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧