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:

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值