VB文本框textbox的那点事

前端之家收集整理的这篇文章主要介绍了VB文本框textbox的那点事前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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 style="font-size:18px;">IfkeyAscii=46 and not(InStr(txtnumber,"."))Then Exit Sub
IfkeyAscii=8 then exit sub
IfkeyAscii<48 or keyAscii>57 then
 msgBox"只能输入数字"
 keyAscii=0
End 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 style="font-size:18px;">	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/257325.html

猜你在找的VB相关文章