防范在TextBox中輸入
發(fā)表時間:2024-06-11 來源:明輝站整理相關軟件相關文章人氣:
[摘要]方法一:(有光標閃爍,輸入、刪除等操作無效)Text1.Locked = True方法二:(無光標閃爍,不能輸入、刪除,界面變色、文字反白)Text1.Enabled = False方法三:(有光標閃爍,可刪除,不能輸入)此法用兩個API函數,略為復雜些。請在標準工程添加兩個按鈕和一個文本框:Op...
方法一:(有光標閃爍,輸入、刪除等操作無效)
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