Creating a new instance of an existing control? (a bit more complicated than it sounds)

StackOverflow https://stackoverflow.com/questions/16754432

  •  30-05-2022
  •  | 
  •  

سؤال

I know that if I want to dynamically create a new instance of an existing form, I can simply do this...

Dim NewForm As New ExistingForm

... and a new instance of all of the controls from ExistingForm will be available in NewForm.

But what if I want to create a new instance of an existing control that's inside a separate existing form?

More specifically, let's say I have 2 forms - FormA and FormB. Inside FormB is an existing panel (we'll call it ExistingPanel). And inside ExistingPanel are several buttons, text boxes, etc., arranged exactly how I want them.

OK, so... inside a button's click event in FormA, I want to do something like this (it doesn't work, obviously, but should give you a good idea as to what I'm trying to accomplish)...

Dim NewExistingPanel As New FormB.ExistingPanel

And then I want NewExistingPanel to contain new instances all of the individual controls from ExistingPanel (so that I can dynamically manipulate each one individually).

So basically, my program will need to create several NewExistingPanels at runtime. And I will need to access each one (and all of its child controls) individually, after they have been created.

How can I accomplish this? I've been experimenting with this for several hours with no luck so far, so figured I'd break down and ask the real professionals :)

هل كانت مفيدة؟

المحلول

When you create a new user control, you could use a dictionary with the tag value as key and the control name as value. Then you can access the control directly by using the IndexOfKey method.

    Dim NewForm2 As New Form2
    Dim ControlIndex As New Dictionary(Of String, String)
    For I = 0 To 2
        Dim NewPanel As New WindowsControlLibrary2.MyCustomPanel
        NewPanel.Name = "Panel-" + I.ToString
        NewPanel.Location = New Point(0 + (NewPanel.Size.Width * I), 0)
        NewPanel.Tag = "MyTagValue" + I.ToString
        ControlIndex.Add(NewPanel.Tag, NewPanel.Name)
        NewForm2.Controls.Add(NewPanel)
        NewForm2.Update()
    Next
    NewForm2.Show()
    Dim CurrentPanel As WindowsControlLibrary2.MyCustomPanel =NewForm2.Controls(NewForm2.Controls.IndexOfKey(ControlIndex("MyTagValue1")))

نصائح أخرى

To add control such as panel with textboxes ang button in it ..

  1. In your project - Add > Usercontrol -->
    in this part you can make panel with textboxes and button as you want
    name it as "myPanel" and save it as "myPanel.vb" --> or whatever you want

  2. In your form and the button named button1 you can add to your button1_click event :

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim mypanel1 As New MyPanel
    
            mypanel1.Location = New Point(0, 0)
    
            Controls.Add(mypanel1)
            mypanel1.TextBox1.Text = "Hi, Im new"
    
    End Sub
    

And you will get your panel at runtime ...

And you will get the textbox1.text fill with "Hi, I'm new" ...

You could create a user control that lays out your sub-controls the way you want them, then create new instances of that. Add New > User Control in the solution menu, then build, and your controls should be ready to drag into the designer at the top of the Toolbox or to instantiate in code like any other control.

To expand on my comment:

Private somePanels As New Dictionary(Of String, YourUserControl)

Then you can just add them as follows:

Dim c As New YourUserControl()
…
Me.Controls.Add(c)
somePanels.Add("SomeTag", c) ' Instead of using Tag; if your tags are numbers, use a List instead of a Dictionary

And access them as:

Dim c = somePanels("SomeTag")
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top