源代碼如下:
Option Explicit
'自定義文本框輸入控件
'檢測使用者輸入是否為數值
Private Sub Text1_Change()
If IsNumeric(Text1.text) = False And Trim(Text1.text) <> "-" And Trim(Text1.text) <> "" Then
MsgBox "輸入中含有非法字元!", 16, ""
Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1.text)
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim str As String
str = Trim(Text1.text)
Select Case KeyAscii
Case 8, 9, 13, &H30 To &H39
KeyAscii = KeyAscii
Case 45 '負号[隻充許字元的第一個字元是負号]
If str = "" Then
KeyAscii = KeyAscii
Else
KeyAscii = 0
End If
Case 46 '小數點處理[前面字元中沒有小數點則可以輸入]
If (IsNumeric(str) = True And InStr(1, str, ".") = 0) Or str = "" Then
Case Else
KeyAscii = 0
End Select
Private Sub Text1_LostFocus()
If IsNumeric(Text1.text) = False And Trim(Text1.text) <> "" Then
'text屬性
Public Property Get text() As String
text = Text1.text
End Property
Public Property Let text(str1 As String)
Text1.text = str1
PropertyChanged "text"
'文本框高度
Public Property Get txtheight() As Integer
txtheight = Text1.height
Public Property Let txtheight(iheight As Integer)
Text1.height = iheight
'width
'文本框寬度
Public Property Get txtwidth() As Integer
txtwidth = Text1.Width
Public Property Let txtwidth(iwidth As Integer)
Text1.Width = iwidth
'字型
Public Property Get fontsize() As Integer
fontsize = Text1.fontsize
Public Property Let fontsize(isize As Integer)
Text1.fontsize = isize
P:
怎樣寫自定義控件的?~就是像你上面的源碼~~怎樣轉成控件的?~~請指教~~
-----------------------------------
是這樣的,建立一個使用者控件,[注意不是建一個窗體,看好選項.]
然後在上面放一個文本框.然後将我的代碼全面複制到代碼欄中就可以了.
然後在工具箱中就會出現這個自定義控件了.