[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Reparent a control in C#

[Reparent a control in C#]

Changing a control's parent is easy. Simply set its Parent property to the control that should contain it. This example uses the following code.

// Move the Button from one GroupBox to the other. private void btnReparentMe_Click(object sender, EventArgs e) { if (btnReparentMe.Parent == groupBox1) { // Move into GroupBox2. btnReparentMe.Parent = groupBox2; } else { // Move into GroupBox1. btnReparentMe.Parent = groupBox1; } }

The program compares the Button's current parent with the two GroupBoxes and moves the Button into the one that does not currently contain it.

The control's other properties remain the same. In particular, the control's Location property is the same so the Button has the same position in its new parent that it had in it old parent.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.