VB文本框textbox的那点事

VB文本框textbox的那点事,第1张

概述1、限制文本框中只允许输入数字 在textbox的keypress事件里面写上 (1)判断输入的数字是否为小数 IfkeyAscii=46 and not(InStr(txtnumber,"."))Then Exit Sub (2)限制输入为数字 IfkeyAscii<48 or keyAscii>57 then keyAscii=0 (3)解决屏蔽BackSpace退格键            1、限制文本框中只允许输入数字

textBoxkeypress事件里面写上

1)判断输入的数字是否为小数

IfkeyAscii=46 and not(InStr(txtnumber,"."))Then Exit Sub

2)限制输入为数字

IfkeyAscii<48 or keyAscii>57 then keyAscii=0

3)解决屏蔽BackSpace退格键

if keyAscii=8 then exit sub

代码如下:

<strong><span >IfkeyAscii=46 and not(InStr(txtnumber,"."))Then Exit SubIfkeyAscii=8 then exit subIfkeyAscii<48 or keyAscii>57 then msgBox"只能输入数字" keyAscii=0End if</span></strong>


2、限制文本框字节的长度问题

如果用MaxLength只是限制了字符数,不限制字节数,所以一般最好的办法是用API函数

	Public Declare Function SendMessage lib "user32" Alias "SendMessageA" (ByVal hwnd As Long,ByVal wMsg As Long,ByVal wParam As Long,lParam As Any) As Long		Private Sub Form_Load()	SendMessage Text1.hwnd,&HC5&,20&,0&	End Sub

3、全部选中文本框的内容

	Txtusername.SelStart=0	Txtusername.SelLength=Len(txtusername.text)

4、将文本框变成透明色

效果图:


代码实现:

	Private Declare Function getwindowlong lib "user32" Alias "getwindowlongA" (ByVal hwnd As Long,ByVal nIndex As Long) As Long 	Private Declare Function SetwindowLong lib "user32" Alias "SetwindowLongA" (ByVal hwnd As Long,ByVal nIndex As Long,ByVal DWNewLong As Long) As Long 	Private Declare Function SetlayeredWindowAttributes lib "user32" (ByVal hwnd As Long,ByVal crKey As Long,ByVal bAlpha As Byte,ByVal DWFlags As Long) As Long 	Private Const WS_EX_layered = &H80000 	Private Const GWL_EXSTYLE = (-20) 	Private Const LWA_Alpha = &H2 	Private Const LWA_colorKEY = &H1 		Private Sub Form_Load() 	Text1.Backcolor = vbBlue 	Dim rtn As Long 	rtn = getwindowlong(hwnd,GWL_EXSTYLE) 	rtn = rtn Or WS_EX_layered 	SetwindowLong hwnd,GWL_EXSTYLE,rtn 	SetlayeredWindowAttributes hwnd,vbBlue,LWA_colorKEY 	End Sub 

5、窗体设置为仅仅显示文本框


代码实现:

<strong><span >	Private Declare Function getwindowlong lib "user32" Alias "getwindowlongA" (ByVal hwnd As Long,ByVal nIndex As Long) As Long	Private Declare Function SetwindowLong lib "user32" Alias "SetwindowLongA" (ByVal hwnd As Long,ByVal DWNewLong As Long) As Long	Private Declare Function SetlayeredWindowAttributes lib "user32" (ByVal hwnd As Long,ByVal DWFlags As Long) As Long		Private Sub Form_Load()	Me.borderStyle = 0	Me.Caption = ""	Me.Backcolor = vbRed	SetwindowLong Me.hwnd,-20,getwindowlong(Me.hwnd,-20) Or &H80000	SetlayeredWindowAttributes Me.hwnd,vbRed,1	End Sub</span></strong>
总结

以上是内存溢出为你收集整理的VB文本框textbox的那点事全部内容,希望文章能够帮你解决VB文本框textbox的那点事所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1269211.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存