Question How to search in Panel Controls and copy each Control to a new Panel

Magnos

Member
Joined
Aug 3, 2019
Messages
5
Programming Experience
Beginner
Maybe something like this

C#:
foreach (var Control in MyPanel.Controls)

{

    MyNewPanel.Controls.Add((ControlType)Control);

}

How to get Control Type in this Place?

Or how to copy all a Panel and all of it's Controls as a new panel to use it Dynamically in TabControl?

C#:
var NewPanel = new MyPanel;
 
Time to learn about reflection and activation.

In C#, you can always call GetType() get the type of an object. You can then use the Activator class to create an instance of that type.
 
You can check controls types with either if statements, although the later is more profound. The link Skydiver is referring to are Object.GetType Method (System)
C#:
        private void GetControlTypes()
        {
            foreach (Control c in panel1.Controls)
            {
                if (c.GetType().ToString() == "System.Windows.Forms.ListBox")
                {
                    Console.WriteLine("Found a listbox");
                }
                /* Or simpler */
                if (c is ListBox)
                {
                    Console.WriteLine("Found a listbox");
                }
            }
        }
 
Or how to copy all a Panel and all of it's Controls as a new panel to use it Dynamically in TabControl?
That suggests to me that you are going about this in completely the wrong way. It sounds like you have created a form, added a TabControl to that, added a Panel to one of the TabPages and then, when adding new TabPages, you want to create duplicates of that Panel and its contents. That's wrong.

What you should is is start by creating a user control. You add a user control to your project in much the same way as you do a form, then you design it and add code in exactly the same way as you do a form. All the child controls that you're currently adding to your Panel, you would add to the user control instead.

When you build your project, your user control will be automatically added to the Toolbox and you can then use it exactly the same way as you would any other control, both in the designer and in code. That means that you would add an instance of the user control to your first TabPage in the designer and then, when you wanted to create new TabPages, you would simply add an instance of your user control to them in code. There's no need to worry about copying anything; you simply add one control to each TabPage.

If you wanted to take things a step further, you could even create your own custom TabControl and TabPage classes. The custom TabPage could add the user control instance to itself and the custom TabControl could provide a means to access instances of that custom TabPage.

Just note that you would likely need to add some pass-through functionality to your user control. By that I mean that it should provide members that enable indirect access to members of the child controls so that the form only needs to deal with the user control. For instance, let's say that you have a TextBox whose Text you need to get when a Button is clicked. In that case, the user control would handle the Click event of the Button and then raise its own event that the form could handle, as well as providing a property for the form to get and set that would then get and set the Text of the TextBox.
C#:
public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    public string TextBox1Text
    {
        get => textBox1.Text;
        set => textBox1.Text = value;
    }

    public event EventHandler Button1Click;

    private void button1_Click(object sender, EventArgs e)
    {
        Button1Click?.Invoke(this, EventArgs.Empty);
    }
}
 
That suggests to me that you are going about this in completely the wrong way. It sounds like you have created a form, added a TabControl to that, added a Panel to one of the TabPages and then, when adding new TabPages, you want to create duplicates of that Panel and its contents. That's wrong.

What you should is is start by creating a user control. You add a user control to your project in much the same way as you do a form, then you design it and add code in exactly the same way as you do a form. All the child controls that you're currently adding to your Panel, you would add to the user control instead.

When you build your project, your user control will be automatically added to the Toolbox and you can then use it exactly the same way as you would any other control, both in the designer and in code. That means that you would add an instance of the user control to your first TabPage in the designer and then, when you wanted to create new TabPages, you would simply add an instance of your user control to them in code. There's no need to worry about copying anything; you simply add one control to each TabPage.

If you wanted to take things a step further, you could even create your own custom TabControl and TabPage classes. The custom TabPage could add the user control instance to itself and the custom TabControl could provide a means to access instances of that custom TabPage.

Just note that you would likely need to add some pass-through functionality to your user control. By that I mean that it should provide members that enable indirect access to members of the child controls so that the form only needs to deal with the user control. For instance, let's say that you have a TextBox whose Text you need to get when a Button is clicked. In that case, the user control would handle the Click event of the Button and then raise its own event that the form could handle, as well as providing a property for the form to get and set that would then get and set the Text of the TextBox.
C#:
public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    public string TextBox1Text
    {
        get => textBox1.Text;
        set => textBox1.Text = value;
    }

    public event EventHandler Button1Click;

    private void button1_Click(object sender, EventArgs e)
    {
        Button1Click?.Invoke(this, EventArgs.Empty);
    }
}

Man I have already fixed it with adding a Source panel to edit it easily from the designer
and every tab duplicate it easily with it's childs and grandchilds


C#:
        var Main = new Panel(); // New Panel for the new tab
        Main.Size = Dynamic_Main.Size;
        Main.Location = Dynamic_Main.Location;

        foreach (Control Control in Dynamic_Main.Controls)
        { // I made a Hidden panel as a Source for this process (designer)
            var NewControl = Activator.CreateInstance(Control.GetType()) as Control;
            //the New Control taken from the source
            //Pingo is "Control" Type because it's a subject for every Control...

            NewControl.Size = Control.Size;
            NewControl.Location = Control.Location;
            NewControl.Text = Control.Text;

                if (NewControl.GetType() == typeof(RichTextBox)) NewControl.Text = File.ReadAllText(FilePath.Text);
                // a tiny thing for mine

            foreach (Control grandson in Control.Controls)
            { // check for grandsons
                var tinyControl = Activator.CreateInstance(grandson.GetType()) as Control;
                tinyControl.Size = grandson.Size;
                tinyControl.Location = grandson.Location;
                tinyControl.Text = grandson.Text;
                NewControl.Controls.Add(tinyControl); //add this son to dad
                //this operation may cause extinction :D
            }
            Main.Controls.Add(NewControl); // add this dad with his son to the grand
            //Extinction is coming no way XD
        }

        TabPage New = new TabPage(); //the new tab
        New.Controls.Add(Main);//adding the panel to the tab
        MultiMain.TabPages.Add(New);//Finally the new tab is ready :)

it's better than making a class and surely better than making a form for every Tool...
 
The advice outlined on post 2 and 8 are both correct, and you can do it either way. It's preference. But post 8 is the way you really should do it. The fact that you'd consider arguing with your reasoning is why its often easier to just give you what you want, rather than what you really need and let you be on your way. Most developers of today aren't eager to put in the extra work and go about things correctly even when advised by those they seek advice from. Chances are you be back soon enough looking to change it. But hay, its your project, do as you please. ;)

it's better than making a class and surely better than making a form for every Tool...

Really? Do you understand what a user control does? A user control is not a form... We use user controls to group a set of controls together in a reusable way. Rather than setting controls properties on a new tab control, you can group your controls and place them concisely inside your user control and when adding your user control to your tab control page, everything is already set as they where when you created and designed your user control.

Well, if you're happy, then use what you got so.
 
Last edited:
it's better than making a class and surely better than making a form for every Tool...
I bow to your superior knowledge. I thought that my Computer Science degree, nearly 20 years experience as a professional developer and 16 years as a .NET developer would count for something but I guess not. Now, that's not to say that I know everything and there are always multiple ways to skin a cat, but I've picked up a few things over the years and I do know that some ways are better than others. If you want to follow best practice, you'll go with the user control option. If you've already implemented something else, it may not be worth making the change for this project. Then again, it may be worthwhile because it may save you time in the long run. You should definitely consider it for future projects.
 
Last edited:
I bow to your superior knowledge. I thought that my Computer Science degree, nearly 20 years experience as a professional developer and 16 years as a .NET developer would count for something but I guess not. Now, that's not to say that I know everything and there are always multiple ways to skin a cat, but I've picked up a few things over the years and I do know that some ways are better than others. If you want to follow best practice, you'll go with the user control option. If you've already implemented something else, it may not be worth making the change for this project. Then again, it may be worthwhile because it may save to in the long run. You should definitely consider it for future projects.

I will surely try it someday, I bookmarked this page url
 
Back
Top Bottom