Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add the same instance of a Windows Forms control to multiple containers?

Tags:

c#

winforms

What I am trying to accomplish:

I have a wizard with multiple pages. On each page, I need to have a ToolStripDropDownButton, each of which has the exact same functionality and UI. The entire wizard and all pages are created before any of them are displayed. The ToolStripDropDownButton is displayed within the bounds of the page. When the user changes the value of the ToolStripDropDownButton on one page, it should be updated on the others.

The alternative solution I am trying to avoid:

Having a separate instance of the control on each page which subscribes to an event fired when any of the the other controls' value is changed. There is nothing wrong with this solution beyond the complication of it.

What I have tried:

I created the ToolStripDropDownButton as a static member of the wizard page class, instantiating it at the first request and adding the same object to each page as it is created. This results in the ToolStripDropDownButton appearing on no page (presumably because I have added it to multiple controls and Winforms doesn't know how to handle that).

If it is not possible to add the same control instance to multiple containers, is there a better way to accomplish the task than raising and subscribing to events?

like image 376
the-drew Avatar asked Mar 08 '17 13:03

the-drew


People also ask

How can we add the control into a group box?

Add other controls to the group box, drawing each inside the group box. If you have existing controls that you want to enclose in a group box, you can select all the controls, cut them to the Clipboard, select the GroupBox control, and then paste them into the group box. You can also drag them into the group box.

How do I add different controls to my form?

Add the control by drawing Select the control by clicking on it. In your form, drag-select a region. The control will be placed to fit the size of the region you selected.


1 Answers

You can't do what you are wanting, controls can only have 1 Parent.

A workaround would be: Move the control from one page to the next on the VisibleChanged event of each page. From the user's view, the control exists on every page, but really it is being moved to the 'current' page as it becomes visible.

like image 162
David Avatar answered Oct 05 '22 23:10

David