简体   繁体   中英

Copy Label from GroupBox to another GroupBox

I'm going to copy all Labels from a GroupBox to another GroupBox. Here is the code:

        foreach (var ctrl in grpA.Controls)
        {
            if (ctrl.GetType() == typeof(Label))
            {
                Label lbl = ctrl as Label;
                grpB.Controls.Add(lbl);
            }
        }

The problem is that all Labels are moved to grpB where as I need to be copied. How can I solve the problem?

Try creating new Label for each Label in other group.

    foreach (var ctrl in grpA.Controls)
    {
        if (ctrl.GetType() == typeof(Label))
        {
            Label lbl = ctrl as Label;
            Label b = new Label();
            // copy required properties
            b.Text = lbl.Text
            b.TextAlign = lbl.TextAlign;
            // ... other properties

            grpB.Controls.Add(b);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM