Clone Controls - C# (Winform)

How to Clone a Windows Forms Control

I'll suggest you to create a user control which has the groupbox you shown with the child controls.

Let's call it as MyUserControl so when you create instance of MyUserControl you get all controls with the groupbox.

To show the controls without overlapping each other you can use FlowLayoutPanel which arranges the controls automatically. Then in button click code you'd just write

void addButton_Click(object sender, EventArgs e)
{
int contaniners = 0;
int.TryParse(txtContainers.Text, out contaniners);

for (int i = 0; i < contaniners; i++)
{
flowLayoutPanel.Controls.Add(new MyUserControl());
}
}

It is possible to copy all the properties of a certain control? (C# window forms)

You'll need to use reflection.

You grab a reference to each property in your source control (based on its type), then "get" its value - assigning that value to your target control.

Here's a crude example:

    private void copyControl(Control sourceControl, Control targetControl)
{
// make sure these are the same
if (sourceControl.GetType() != targetControl.GetType())
{
throw new Exception("Incorrect control types");
}

foreach (PropertyInfo sourceProperty in sourceControl.GetType().GetProperties())
{
object newValue = sourceProperty.GetValue(sourceControl, null);

MethodInfo mi = sourceProperty.GetSetMethod(true);
if (mi != null)
{
sourceProperty.SetValue(targetControl, newValue, null);
}
}
}

Copy the state of controls and variables from one Form to another in c#

You can add these method inside your form:

public void RestoreState(Dictionary<string, object> controlStates, 
Dictionary<string, object> membersStates)
{
InternalRestoreControls(controlStates);
InternalRestoreMembers(membersStates);
}

private void InternalRestoreControls(Dictionary<string, object> states)
{
foreach (var state in states)
{
Control c = this.Controls.Find(state.Key, true).FirstOrDefault();

if (c is TextBox)
{
(c as TextBox).Text = state.Value == null ? null : state.Value.ToString();
}
else if (c is CheckBox)
{
(c as CheckBox).Checked = Convert.ToBoolean(state.Value);
}
}
}

private void InternalRestoreMembers(Dictionary<string, object> membersStates)
{
// you might need to tweek this a little bit based on public/instance/static/private
// but this is not the point of your question

BindingFlags flags = BindingFlags.Instance | BindingFlags.Static
| BindingFlags.Public | BindingFlags.NonPublic;

var props = this.GetType().GetProperties(flags);
var fields = this.GetType().GetFields(flags);

foreach(var variable in membersStates)
{
var prop = props.FirstOrDefault(x => x.Name == variable.Key);

if(prop != null)
{
prop.SetValue(this, variable.Value);
continue;
}

var field = fields.FirstOrDefault(x => x.Name == variable.Key);

if(field != null)
{
field.SetValue(this, variable.Value);
continue;
}
}
}

private Dictionary<string, object> GetControlsState()
{
return new Dictionary<string, object>()
{
{ txtBox1.Name, txtBox1.Text },
// continue to the rest
};
}

private Dictionary<string, object> GetMembersState()
{
return new Dictionary<string, object>()
{
{ nameof(variable1), variable1 },
// continue to the rest
};
}

Usage:

Form duplicate = new Form();
duplicate.RestoreState(this.GetControlsState(), this.GetMembersState());

Clone a container control and its child controls during runtime

Hi
The only way for copying an object is to implement IClonable interface. But as far as I know, windows controls do not implement this interface, So you should create your own conrtols derived from the container and child control which will implement IClonable interface.

Copy controls in VS C# project

You're not going to create two windows forms, but one form containing the tab control, and the other an UserControl containing the panel.

public class MainForm : Forms
{

}

public class MyPanel: UserControl
{
}

You could drag&draw the UserControl in the design time. And in the runtime, initialize an instance of the UserControl, and then add it to the new tab.

newTab.Controls.Add(new MyPanel());

C# Clone a panel to other form

Sounds like a dirty hack to me, but for what it's worth: Create a reference to form2 from form1. When you perform your "copy", you create a list of all controls on form1, and then clear form1. You then add the controls to form2.

Add this method into form1...form2 is your reference to your second form. Fire this with an event like a button click.

public void CopyControls()
{
List<Control> ctrls = new List<Control>();
foreach (Control c in this.Controls)
{
ctrls.Add(c);
}
this.Controls.Clear();
form2.Controls.AddRange(ctrls.ToArray());
}

I personally wouldn't recommend doing this, it's horrid, and I bet there will be a cleaner way to achieve what you want!

Copying a Control between forms moves it instead

You are only copying the reference to your control. But a control can only be used in one form. So the control is dissapearing in the "old" form. You need real copies of your controls.

This Question describes a way to copy a control via reflection. Try it with a solution like this:

private void copyControl(Control sourceControl, Control targetControl)
{
// make sure these are the same
if (sourceControl.GetType() != targetControl.GetType())
{
throw new Exception("Incorrect control types");
}

foreach (PropertyInfo sourceProperty in sourceControl.GetType().GetProperties())
{
object newValue = sourceProperty.GetValue(sourceControl, null);

MethodInfo mi = sourceProperty.GetSetMethod(true);
if (mi != null)
{
sourceProperty.SetValue(targetControl, newValue, null);
}
}
}


Related Topics



Leave a reply



Submit