最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
禁止在TextBox中输入
时间:2022-07-02 12:06:37 编辑:袖梨 来源:一聚教程网
方法一:(有光标闪烁,输入、删除等操作无效)
Text1.Locked = True
方法二:(无光标闪烁,不能输入、删除,界面变色、文字反白)
Text1.Enabled = False
方法三:(有光标闪烁,可删除,不能输入)
此法用两个API函数,略为复杂些。请在标准工程添加两个按钮和一个文本框:
Option Explicit
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
Const GWL_STYLE = (-16)
Const ES_NUMBER = &H2000&
Public Sub SetNumber(NumberText As TextBox, Flag As Boolean)
Dim CurrentStyle As Long, NewStyle As Long
'返回正常样式
CurrentStyle = GetWindowLong(NumberText.hwnd, GWL_STYLE)
If Flag Then
CurrentStyle = CurrentStyle Or ES_NUMBER
Else
CurrentStyle = CurrentStyle And (Not ES_NUMBER)
End If
'设置新样式
NewStyle = SetWindowLong(NumberText.hwnd, GWL_STYLE, CurrentStyle)
NumberText.Refresh '刷新
End Sub
Private Sub Command1_Click()
SetNumber Text1, True
Text1.SetFocus
End Sub
Private Sub Command2_Click()
SetNumber Text1, False
Text1.SetFocus
End Sub
Private Sub Form_Load()
Command1.Caption = "禁止输入"
Command2.Caption = "可以输入"
End Sub
Text1.Locked = True
方法二:(无光标闪烁,不能输入、删除,界面变色、文字反白)
Text1.Enabled = False
方法三:(有光标闪烁,可删除,不能输入)
此法用两个API函数,略为复杂些。请在标准工程添加两个按钮和一个文本框:
Option Explicit
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
Const GWL_STYLE = (-16)
Const ES_NUMBER = &H2000&
Public Sub SetNumber(NumberText As TextBox, Flag As Boolean)
Dim CurrentStyle As Long, NewStyle As Long
'返回正常样式
CurrentStyle = GetWindowLong(NumberText.hwnd, GWL_STYLE)
If Flag Then
CurrentStyle = CurrentStyle Or ES_NUMBER
Else
CurrentStyle = CurrentStyle And (Not ES_NUMBER)
End If
'设置新样式
NewStyle = SetWindowLong(NumberText.hwnd, GWL_STYLE, CurrentStyle)
NumberText.Refresh '刷新
End Sub
Private Sub Command1_Click()
SetNumber Text1, True
Text1.SetFocus
End Sub
Private Sub Command2_Click()
SetNumber Text1, False
Text1.SetFocus
End Sub
Private Sub Form_Load()
Command1.Caption = "禁止输入"
Command2.Caption = "可以输入"
End Sub
相关文章
- 《无限暖暖》天星之羽获得位置介绍 12-20
- 《流放之路2》重铸台解锁方法介绍 12-20
- 《无限暖暖》瞄准那个亮亮的成就怎么做 12-20
- 《无限暖暖》魔气怪终结者完成方法 12-20
- 《无限暖暖》曙光毛团获得位置介绍 12-20
- 《无限暖暖》日光果获得位置介绍 12-20