| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2568 人关注过本帖
标题:[求助]vb的textbox控件
只看楼主 加入收藏
griefforyou
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:3336
专家分:0
注 册:2004-4-15
收藏
得分:0 

Option Explicit

Private Sub Text1_KeyPress(KeyAscii As Integer) Dim Pos As Integer

If (KeyAscii < Asc("0") Or KeyAscii > Asc("9")) And KeyAscii <> Asc(".") And KeyAscii <> 8 Then KeyAscii = 0 End If Pos = InStr(Text1.Text, ".") If Pos <> 0 And KeyAscii <> 8 Then If KeyAscii = Asc(".") Then KeyAscii = 0 Else If Len(Mid(Text1.Text, Pos)) > 2 And Text1.SelStart >= Len(Text1.Text) - 2 Then KeyAscii = 0 End If End If End If End Sub 这个代码只针对键盘输入起作用,如果要防止Ctrl+V 或鼠标右键粘贴还要另加代码


天津网站建设 http://www./
2005-03-19 15:56
griefforyou
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:3336
专家分:0
注 册:2004-4-15
收藏
得分:0 
以下是引用leon2在2005-3-19 15:53:46的发言: 我不知道楼主是不是想要这个代码,因为楼主没有表达清楚…… Private Sub Text1_KeyPress(ByVal KeyAscii As Integer, ByVal Shift As Integer) If Format(Text1.Text, "###.##") <> Text1.Text Then '格式是否为 xxx.xx KeyAscii = 0 Beep '播放警告声音 End If End Sub
思路不错,不过KeyPress事件哪来的Shift参数?而且这段代码你运行过没有?根本行不通

[此贴子已经被作者于2005-3-19 16:02:18编辑过]


天津网站建设 http://www./
2005-03-19 15:58
leon2
Rank: 3Rank: 3
等 级:新手上路
威 望:7
帖 子:731
专家分:0
注 册:2005-3-18
收藏
得分:0 
SORRY,我不记得了……
2005-03-19 16:01
access
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2005-3-18
收藏
得分:0 
还是不对,我的意思就是这个文本框里面只能输入数值型数据,就这么简单。按楼上所说就什么也输不上去了!!!
2005-03-19 16:27
access
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2005-3-18
收藏
得分:0 

还是不行啊。如果按下面的代码去运行的话,小数位就输不上了!!!!!(11楼所说,)另外怎么防止Ctrl+V 或鼠标右键粘贴???? Option Explicit

Private Sub Text1_KeyPress(KeyAscii As Integer) Dim Pos As Integer

If (KeyAscii < Asc("0") Or KeyAscii > Asc("9")) And KeyAscii <> Asc(".") And KeyAscii <> 8 Then KeyAscii = 0 End If Pos = InStr(Text1.Text, ".") If Pos <> 0 And KeyAscii <> 8 Then If KeyAscii = Asc(".") Then KeyAscii = 0 Else If Len(Mid(Text1.Text, Pos)) > 2 And Text1.SelStart >= Len(Text1.Text) - 2 Then KeyAscii = 0 End If End If End If End Sub 这个代码只针对键盘输入起作用,如果要防止Ctrl+V 或鼠标右键粘贴还要另加代码

2005-03-19 16:34
griefforyou
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:3336
专家分:0
注 册:2004-4-15
收藏
得分:0 
我运行没有问题呀。

天津网站建设 http://www./
2005-03-19 16:39
access
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2005-3-18
收藏
得分:0 
真的啊。我试了半天就是不行
2005-03-19 16:43
griefforyou
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:3336
专家分:0
注 册:2004-4-15
收藏
得分:0 
那我就管不了了,我代码没什么问题。

天津网站建设 http://www./
2005-03-19 16:49
leon2
Rank: 3Rank: 3
等 级:新手上路
威 望:7
帖 子:731
专家分:0
注 册:2005-3-18
收藏
得分:0 
这段代码应该可以实现:
Dim HasDot As Boolean '为全局变量

Private Sub Text1_KeyPress(KeyAscii As Integer)
   If KeyAscii = Asc(".") Then
      HasDot = True
   End If
   If KeyAscii = vbKeyBack And Right(Text1.Text, 1) = "." Then
      HasDot = False
   End If
   If KeyAscii &lt; Asc("0") Or KeyAscii &gt; Asc("9") Then
      If KeyAscii &lt;&gt; Asc(".") And HasDot Then
        KeyAscii = 0
        Beep                '播放警告声音
      End If
   End If
End Sub

[此贴子已经被作者于2005-3-19 17:00:48编辑过]


2005-03-19 16:50
griefforyou
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:3336
专家分:0
注 册:2004-4-15
收藏
得分:0 
以下是引用leon2在2005-3-19 16:50:35的发言: 这段代码应该可以实现: Dim HasDot As Boolean '为全局变量 Private Sub Text1_KeyPress(ByVal KeyAscii As Integer, ByVal Shift As Integer) If KeyAscii = Asc(".") Then HasDot = True End If If KeyAscii = vbKeyBack And Right(Text1.Text, 1) = "." Then HasDot = False End If If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then If KeyAscii <> Asc(".") And HasDot Then KeyAscii = 0 Beep '播放警告声音 End If End If End Sub
不知道你用的是什么版本的VB,在KeyPress事件里哪来的Shift参数?即使去掉这个参数,你的代码还可以输入多个 . ,甚至可以输入字母 ,而且BackSpace有时也不好用。你帖上代码之前最好运行一下,不要瞎闹好不好

[此贴子已经被作者于2005-3-19 17:00:30编辑过]


天津网站建设 http://www./
2005-03-19 16:56
快速回复:[求助]vb的textbox控件
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012800 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved