防范在TextBox中輸入
發(fā)表時(shí)間:2024-02-16 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]方法一:(有光標(biāo)閃爍,輸入、刪除等操作無(wú)效)Text1.Locked = True方法二:(無(wú)光標(biāo)閃爍,不能輸入、刪除,界面變色、文字反白)Text1.Enabled = False方法三:(有光標(biāo)閃爍,可刪除,不能輸入)此法用兩個(gè)API函數(shù),略為復(fù)雜些。請(qǐng)?jiān)跇?biāo)準(zhǔn)工程添加兩個(gè)按鈕和一個(gè)文本框:Op...
方法一:(有光標(biāo)閃爍,輸入、刪除等操作無(wú)效)
Text1.Locked = True
方法二:(無(wú)光標(biāo)閃爍,不能輸入、刪除,界面變色、文字反白)
Text1.Enabled = False
方法三:(有光標(biāo)閃爍,可刪除,不能輸入)
此法用兩個(gè)API函數(shù),略為復(fù)雜些。請(qǐng)?jiān)跇?biāo)準(zhǔn)工程添加兩個(gè)按鈕和一個(gè)文本框:
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
'設(shè)置新樣式
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