做了一个文本框控件,可是拖到窗体上时,可以设置自定义属性,当时可以反映,可是保存不了.下次打开时又是这个控件设计时的大小了.
-----------------
比如在窗体上设置高和宽的属性.可以在窗体上反映出来.但是无法保存,下次打开窗体时又恢复到控件设计时的大小了.请问我的控件设计代码错在哪里.下面是代码,请教大家.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 SubPrivate 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 SubPrivate 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 PropertyPublic Property Let text(str1 As String)
    Text1.text = str1
    PropertyChanged "text"
End Property'文本框高度
Public Property Get txtheight() As Integer
    txtheight = Text1.height
End PropertyPublic Property Let txtheight(iheight As Integer)
    Text1.height = iheight
End Property'width
'文本框宽度
Public Property Get txtwidth() As Integer
    txtwidth = Text1.Width
End PropertyPublic Property Let txtwidth(iwidth As Integer)
    Text1.Width = iwidth
End Property'字体
Public Property Get fontsize() As Integer
    fontsize = Text1.fontsize
End PropertyPublic Property Let fontsize(isize As Integer)
    Text1.fontsize = isize
End Property

解决方案 »

  1.   

    以一个属性为例吧:'从存贮器中加载属性值
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)    wskPLC.RemoteHost = PropBag.ReadProperty("RemoteHost", "")
        m_ComMode = PropBag.ReadProperty("ComMode", m_def_ComMode)
        m_RemoteHost = PropBag.ReadProperty("RemoteHost", m_def_RemoteHost)
        m_Settings = PropBag.ReadProperty("Settings", m_def_Settings)
        m_PortOpen = PropBag.ReadProperty("PortOpen", m_def_PortOpen)
        m_Port = PropBag.ReadProperty("Port", m_def_Port)
        m_SNA = PropBag.ReadProperty("SNA", m_def_SNA)
        m_DNA = PropBag.ReadProperty("DNA", m_def_DNA)
        m_DA1 = PropBag.ReadProperty("DA1", m_def_DA1)
        m_BytesReceived = PropBag.ReadProperty("BytesReceived", m_def_BytesReceived)
        m_CommandCode = PropBag.ReadProperty("CommandCode", m_def_CommandCode)
        m_AddressCode = PropBag.ReadProperty("AddressCode", m_def_AddressCode)
        m_Address = PropBag.ReadProperty("Address", m_def_Address)
        m_MemorySize = PropBag.ReadProperty("MemorySize", m_def_MemorySize)    
    End Sub
    '字体
    Public Property Get fontsize() As Integer
        fontsize = Text1.fontsize
    End PropertyPublic Property Let fontsize(isize As Integer)
        Text1.fontsize = isize
        PropertyChanged “fontsize”
    End Property'从存贮器中加载属性值
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    Text1.fontsize=PropBag.ReadProperty("fontsize",15)'其中函数的第二个参数可以用一个常量来替代   
    End Sub
    '将属性值写到存储器
    Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
        Call PropBag.WriteProperty("fontsize", Text1.fontsize, 15)'其中函数的第二个参数可以用一个常量来替代   End Sub
      

  2.   

    还是无法保存呀?在属性窗口中对这个控件设置大小后,当时起作用.关闭窗体再打开,又变成控件设计时的大小了.我把改后的代码贴出来,请大家指教.: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 SubPrivate 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 SubPrivate 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 PropertyPublic Property Let text(str1 As String)
        Text1.text = str1
        PropertyChanged "text"
    End Property'文本框高度
    Public Property Get txtheight() As Integer
        txtheight = Text1.height
    End PropertyPublic Property Let txtheight(iheight As Integer)
        Text1.height = iheight
    End Property'width
    '文本框宽度
    Public Property Get txtwidth() As Integer
        txtwidth = Text1.Width
    End PropertyPublic Property Let txtwidth(iwidth As Integer)
        Text1.Width = iwidth
    End Property'字体
    Public Property Get fontsize() As Integer
        fontsize = Text1.fontsize
    End PropertyPublic Property Let fontsize(isize As Integer)
        Text1.fontsize = isize
        PropertyChanged "fontsize"
    End Property
    Private Sub UserControl_Initialize()
        Text1.fontsize = 12
    End SubPrivate Sub UserControl_ReadProperties(PropBag As PropertyBag)
        Text1.fontsize = PropBag.ReadProperty("fontsize", 12)              '其中函数的第二个参数可以用一个常量来替代
    End SubPrivate Sub UserControl_WriteProperties(PropBag As PropertyBag)
        Call PropBag.WriteProperty("fontsize", Text1.fontsize, 18)         '其中函数的第二个参数可以用一个常量来替代
    End Sub
      

  3.   

    谢谢benyfeifei(狒狒) 的热心,能对我的代码改一下吗?错在哪里呀?