vb6user-controls

Access Text box value from User control to another User control in VB6


I have a requirement in VB6 to access the value of textbox from one User Controls user1.ctl in another User controls user2.ctl of the same project, and if the textbox value > 100, need to set it 100. I have not worked on such things till now in Visual basic, your help is appreciated.


Solution

  • The textbox will not be accessible outside of the user control so you will need to expose the textbox by adding public properties to your user control:

    Public Property Get SomeTextBox() As String
       SomeTextBox = Text1.Text
    End Property
    
    Public Property Let SomeTextBox(ByVal Value As String)
       Text1.Text = Value
    End Property
    

    If you drop that user control onto a form then from that form you will say:

    If user1.SomeTextBox > 100 Then
       user1.SomeTextBox = 100
    End If
    

    If you drop a second user control onto the form and want to update the first user control from the second user control then from the second user control you will say:

    If UserControl.Parent.user1.SomeTextBox > 100 Then
       UserControl.Parent.user1.SomeTextBox = 100
    End If