开发者

Duplicate Group Box

How can I开发者_运维问答 Duplicate a Groupbox in C# window application , the group box contain 30 textbox and i need to duplicate it on a special event and I don't know the maximum number of needed groupbox


If you are using winforms, possible solutions:

  1. Put a groupbox on form, customize it, copy the generated code (from ..designer.cs file) and put it to a method. When you event occurs you just call the method that will create new control.
  2. Using reflection create a new object of the same type and any writable property will get their values copied, use this method:

    private object CloneObject(object o)
    {
        Type t = o.GetType();
        PropertyInfo[] properties = t.GetProperties();
    
    
    
    Object p = t.InvokeMember("", System.Reflection.
        BindingFlags.CreateInstance, null, o, null);
    
    
    foreach (PropertyInfo pi in properties)
    {
        if (pi.CanWrite)
        {
            pi.SetValue(p, pi.GetValue(o, null), null);
        }
    }
    return p;
    
    }

Use it like:

private void button1_Click(object sender, EventArgs e)
{
GroupBox g = (GroupBox)CloneObject(groupBox1);
}

Both methods are described here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜