0

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.

4

1 に答える 1

1

M4N の回答を C# の質問から借りて、VB に翻訳するには:

最もクリーンな方法は、目的のプロパティをユーザー コントロールのプロパティとして公開することです。

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

このようにして、公開するプロパティと、それらを読み取り/書き込みにするか読み取り専用にするかを制御できます。(もちろん、その意味に応じて、プロパティにはより適切な名前を使用する必要があります)。

このアプローチのもう 1 つの利点は、ユーザー コントロールの内部実装が隠されることです。リッチテキスト コントロールを別のものと交換したい場合でも、コントロールの呼び出し元/ユーザーを壊すことはありません。

2 番目の質問に答えるために、動的に作成されたコントロールにアクセスする必要がある場合は、次のように名前を使用して簡単にアクセスできます。

Dim c As ClientesEmpresa= CType(Me.ClientesTabControl.TabPages("Tab1").Controls("Control1"), ClientesEmpresa)
c.CheckBoxProperty = True
于 2012-09-05T20:50:09.327 に答える