Search code examples
propertiesvb6user-controlsactivex

Add property to user control in vb6


I am finding code like this all over the web but it does not seem to work for me,

    Private FText As String

Public Property Get Text() As String
  Text = FText
  lblText.Caption = Text
End Property

Public Property Let Text(ByVal Value As String)
  FText = Value
End Property

Let me explain more what I am doing, I am creating a command button and the only part I am stuck by is getting the caption for the control. I got the property to show 'Text' and when I type in it set the caption but then when I run the program the caption is erased! What is wrong with the code I am doing?


Solution

  • I solved it!

    Const m_def_Caption = "Cmd"
    
    'Dim m_Picture As Picture
    Dim m_Caption As String
    
    Private Sub UserControl_InitProperties()
      m_Caption = m_def_Caption
    End Sub
    
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
      m_Caption = PropBag.ReadProperty("Caption", m_def_Caption)
      lblText.Caption = m_Caption
    End Sub
    
    Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
      Call PropBag.WriteProperty("Caption", m_Caption, m_def_Caption)
    End Sub
    
    Public Property Get Caption() As String
      Caption = m_Caption
    End Property
    
    Public Property Let Caption(ByVal New_Caption As String)
      m_Caption = New_Caption
      PropertyChanged "Caption"
    End Property
    

    This works, thanks for your help, and I am glad I was able to solve this myself.