VB的TextBox的KeyPress代码怎样写才简化
本人刚学VB,现在有10个TextBox,每个都只能输入数字、“.”和“-”。并且“.”和“-”只能出现一次,“-”只能出现在第一位。代码如下:PrivategNumbe...
本人刚学VB,现在有10个TextBox,每个都只能输入数字、“.”和“-”。并且“.”和“-”只能出现一次,“-”只能出现在第一位。代码如下:
Private gNumbers As String
-----------------------------------------------------------
Private Sub Form_Load()
gNumbers = "1234567890" + Chr(8) + Chr(45) + Chr(46)
End Sub
-----------------------------------------------------------------------------
Private Sub Text1_KeyPress(KeyAscii As Integer)
If InStr(gNumbers, Chr(KeyAscii)) = 0 Then
KeyAscii = 0
End If
If KeyAscii = 46 And InStr(Text1.Text, ".") <> 0 Then
KeyAscii = 0
End If
If KeyAscii = 45 Then
If InStr(Text1.Text, "-") <> 0 Then
KeyAscii = 0
Else
Text1.SelStart = 0
End If
End If
End Sub
我现在是在每个Textbox的KeyPress都写入以上代码,能不能只写一次然后调用这些代码。是不是要在模块中写代码,然后调用。
请高手帮忙,越详细越好。谢谢
好像不行,是不是在模块里写啊? 展开
Private gNumbers As String
-----------------------------------------------------------
Private Sub Form_Load()
gNumbers = "1234567890" + Chr(8) + Chr(45) + Chr(46)
End Sub
-----------------------------------------------------------------------------
Private Sub Text1_KeyPress(KeyAscii As Integer)
If InStr(gNumbers, Chr(KeyAscii)) = 0 Then
KeyAscii = 0
End If
If KeyAscii = 46 And InStr(Text1.Text, ".") <> 0 Then
KeyAscii = 0
End If
If KeyAscii = 45 Then
If InStr(Text1.Text, "-") <> 0 Then
KeyAscii = 0
Else
Text1.SelStart = 0
End If
End If
End Sub
我现在是在每个Textbox的KeyPress都写入以上代码,能不能只写一次然后调用这些代码。是不是要在模块中写代码,然后调用。
请高手帮忙,越详细越好。谢谢
好像不行,是不是在模块里写啊? 展开
4个回答
展开全部
Private gNumbers As String
-----------------------------------------------------------
Private Sub Form_Load()
gNumbers = "1234567890" + Chr(8) + Chr(45) + Chr(46)
End Sub
-----------------------------------------------------------------------------
Public Sub TextNum(KeyAscii As Integer)
Dim TextNumBers As String
TextNumBers = Me.ActiveControl.Text '返回当前获得焦点的控件的值
If InStr(gNumbers, Chr(KeyAscii)) = 0 Then
KeyAscii = 0
End If
If KeyAscii = 46 And InStr(TextNumBers, ".") <> 0 Then
KeyAscii = 0
End If
If KeyAscii = 45 Then
If InStr(TextNumBers, "-") <> 0 Then
KeyAscii = 0
Else
Me.ActiveControl.SelStart = 0 ‘把“-”放在当前获得焦点的控件的第一位
End If
End If
End Sub
-----------------------------------------------------------------------------
'下面是调用TextNum过程代码
Private Sub Text1_KeyPress(KeyAscii As Integer)
call TextNum(KeyAscii )
End Sub
-----------------------------------------------------------------------------
Private Sub Text2_KeyPress(KeyAscii As Integer)
call TextNum(KeyAscii )
End Sub
-----------------------------------------------------------------------------
Private Sub Text3_KeyPress(KeyAscii As Integer)
call TextNum(KeyAscii )
End Sub
..................
-----------------------------------------------------------------------------
Private Sub Text10_KeyPress(KeyAscii As Integer)
call TextNum(KeyAscii )
End Sub
这个够详细了吧.
-----------------------------------------------------------
Private Sub Form_Load()
gNumbers = "1234567890" + Chr(8) + Chr(45) + Chr(46)
End Sub
-----------------------------------------------------------------------------
Public Sub TextNum(KeyAscii As Integer)
Dim TextNumBers As String
TextNumBers = Me.ActiveControl.Text '返回当前获得焦点的控件的值
If InStr(gNumbers, Chr(KeyAscii)) = 0 Then
KeyAscii = 0
End If
If KeyAscii = 46 And InStr(TextNumBers, ".") <> 0 Then
KeyAscii = 0
End If
If KeyAscii = 45 Then
If InStr(TextNumBers, "-") <> 0 Then
KeyAscii = 0
Else
Me.ActiveControl.SelStart = 0 ‘把“-”放在当前获得焦点的控件的第一位
End If
End If
End Sub
-----------------------------------------------------------------------------
'下面是调用TextNum过程代码
Private Sub Text1_KeyPress(KeyAscii As Integer)
call TextNum(KeyAscii )
End Sub
-----------------------------------------------------------------------------
Private Sub Text2_KeyPress(KeyAscii As Integer)
call TextNum(KeyAscii )
End Sub
-----------------------------------------------------------------------------
Private Sub Text3_KeyPress(KeyAscii As Integer)
call TextNum(KeyAscii )
End Sub
..................
-----------------------------------------------------------------------------
Private Sub Text10_KeyPress(KeyAscii As Integer)
call TextNum(KeyAscii )
End Sub
这个够详细了吧.
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
展开全部
写过程 调用过程
例如
private sub aaa(KeyAscii As Integer)
'一大段程序
end sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
call aaa(KeyAscii)
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
call aaa(KeyAscii)
End Sub
Private Sub Text3_KeyPress(KeyAscii As Integer)
call aaa(KeyAscii)
End Sub
顺便和楼下说一下 vb没有自带的正则表达式..
例如
private sub aaa(KeyAscii As Integer)
'一大段程序
end sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
call aaa(KeyAscii)
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
call aaa(KeyAscii)
End Sub
Private Sub Text3_KeyPress(KeyAscii As Integer)
call aaa(KeyAscii)
End Sub
顺便和楼下说一下 vb没有自带的正则表达式..
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
首先在公共模块里声明如下函数:
Public Function AcceptNumber(ByVal KeyAscii As Integer) As Integer
Select Case KeyAscii '用Select Case限制输入的字符
Case vbKey0, vbKey1, vbKey2, vbKey3, vbKey4, vbKey5, vbKey6, vbKey7, vbKey8, vbKey9, 45, 46,8
AcceptNumber = KeyAscii
Case Else
AcceptNumber = 0
End Select
End Function
将窗体中文本框设置成数组,再在文本框的KeyPress事件中输入如下代码:
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
KeyAscii = AcceptNumber(KeyAscii)
End Sub
希望能帮到你
Public Function AcceptNumber(ByVal KeyAscii As Integer) As Integer
Select Case KeyAscii '用Select Case限制输入的字符
Case vbKey0, vbKey1, vbKey2, vbKey3, vbKey4, vbKey5, vbKey6, vbKey7, vbKey8, vbKey9, 45, 46,8
AcceptNumber = KeyAscii
Case Else
AcceptNumber = 0
End Select
End Function
将窗体中文本框设置成数组,再在文本框的KeyPress事件中输入如下代码:
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
KeyAscii = AcceptNumber(KeyAscii)
End Sub
希望能帮到你
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
请用正则式!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询