VB开发——自定义控件源码

源代码如下:

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
            KeyAscii = KeyAscii
        Else
            KeyAscii = 0
        End If
       
    Case Else
        KeyAscii = 0
    End Select
End Sub

Private Sub Text1_LostFocus()
    If IsNumeric(Text1.text) = False And Trim(Text1.text) <> "" Then
        MsgBox "输入中含有非法字符!", 16, ""
        Text1.SetFocus
        Text1.SelStart = 0
        Text1.SelLength = Len(Text1.text)
    End If
End Sub

'text属性
Public Property Get text() As String
    text = Text1.text
End Property

Public Property Let text(str1 As String)
    Text1.text = str1
    PropertyChanged "text"
End Property

'文本框高度
Public Property Get txtheight() As Integer
    txtheight = Text1.height
End Property

Public Property Let txtheight(iheight As Integer)
    Text1.height = iheight
End Property

'width
'文本框宽度
Public Property Get txtwidth() As Integer
    txtwidth = Text1.Width
End Property

Public Property Let txtwidth(iwidth As Integer)
    Text1.Width = iwidth
End Property

'字体
Public Property Get fontsize() As Integer
    fontsize = Text1.fontsize
End Property

Public Property Let fontsize(isize As Integer)
    Text1.fontsize = isize
End Property

P:

怎样写自定义控件的?~就是像你上面的源码~~怎样转成控件的?~~请指教~~
-----------------------------------
是这样的,新建一个用户控件,[注意不是建一个窗体,看好选项.]
然后在上面放一个文本框.然后将我的代码全面复制到代码栏中就可以了.
然后在工具箱中就会出现这个自定义控件了.

posted on 2006-03-29 21:59  爱你一万年123  阅读(348)  评论(0编辑  收藏  举报

导航