E_Blue
E_Blue

Reputation: 1151

How to access to the properties of an UserControl from code side?

make my own UserControl and I can aggregate new TabPages to a TabControl and then, inside of then TabPage, I add my own UserControl using the following code.

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim TabX As New Windows.Forms.TabPage("Tab " & TabCount.ToString) '(ConfiguracionTabPage)
    Dim MyControl As New ClientesEmpresa
    MyControl.Name = "Control" & TabCount.ToString

    If ClientesTabControl.TabPages.Count = 10 Then
        ClientesTabControl.TabPages.RemoveAt(9)
    End If
    TabX.Controls.Add(MyControl)

    TabX.Name = "Tab" & TabCount.ToString
    TabX.Text = "Tab" & TabCount.ToString

    MyControl.TitularLbl.Text = "Coca Cola"

    Me.ClientesTabControl.TabPages.Insert(0, TabX)
    Me.ClientesTabControl.SelectedIndex = 0
    TabCount += 1
End Sub

My user control have several Labels, TextBox and TabPages(inside of a TabControl).

Now I want to change some properties dynamically from the source code, but I don't know how to access them. The most similar theme that I found is this How to Acces of an User control in c#, but, as the title says, is in C#, how I can do it in VB.NET?


Sorry, I just notice that the Enter key post the comment. :(

Thanks for your feedback, I understand what are you saying but I missing something in the middle.

When I create the control in running time in the above code I can access easily to the properties of the created object, in this case my UserControl, but I don't understand how to reach the properties of a particular instance of that control from outside of Button_Click; ie. another button_click event(second button)

I was thinking to use something like

Dim ControlList As Windows.Forms.Control() = Me.ClientesTabControl.TabPages(0).Controls.Find("ModeloLbl", True)

or

ClientesTabControl.TabPages(0).Controls.OfType(Of AlarmasVehiculo)()

But I'm stuck here.

------------------------------------- 3th post ---------------

Thanks Steve, I was resolved using "Control.Find" and a For Each but your solution is easier.

There's any way to get the name of the selected tab or I must to create an Array when I create the New TabPage?, the idea is to update the text of the controls inside of the selected tab only when is selected by the user or every 5 seconds but just the in selected one.

Thanks.

Upvotes: 0

Views: 10902

Answers (1)

Steven Doggart
Steven Doggart

Reputation: 43743

To borrow M4N's answer from the C# question, and translate it to VB:

Cleanest way is to expose the desired properties as properties of your usercontrol, e.g:

Public Class MyUserControl
    ' expose the Text of the richtext control (read-only)
    Public ReadOnly Property TextOfRichTextBox As String
        Get 
            Return richTextBox.Text
        End Get
    End Property

    ' expose the Checked Property of a checkbox (read/write)
    Public Property CheckBoxProperty As Boolean
        Get 
            Return checkBox.Checked
        End Get
        Set (value As Boolean)
            checkBox.Checked = value
        End Set
    End Property

    '...
End Class

In this way you can control which properties you want to expose and whether they should be read/write or read-only. (of course you should use better names for the properties, depending on their meaning).

Another advantage of this approach is that it hides the internal implementation of your user control. Should you ever want to exchange your richtext control with a different one, you won't break the callers/users of your control.

To answer your second question, if you need to access your dynamically created controls, you can do so easily using their names, for instance:

Dim c As ClientesEmpresa= CType(Me.ClientesTabControl.TabPages("Tab1").Controls("Control1"), ClientesEmpresa)
c.CheckBoxProperty = True

Upvotes: 1

Related Questions