4

New to Visual Studio 2010, but have been doing Windows development using other platforms for over 10 years. I've done the following in other platforms such as Visual FoxPro, but it doesn't seem to work in VS...

I created a User Control consisting of a panel that contains a few labels and a couple of buttons. When I make a new form (WinForm) and drop this User Control onto the form, I cannot add additional controls to the panel. The User Control appears in the document outline as a single item (purple gear icon) without access to the panel or the controls contained in the panel.

Is this an inappropriate use of a User Control? In this application, I will have many panels with the same buttons and labels in them along with unique combinations of other controls. It seemed natural to make a User Control that provided the panel and contained controls that are common, then just drop in the controls that are unique to the various instances of the panel in the designer. If I have to build all of the panels in the designer from the base controls, I will, but I was hoping to use what seemed to be an obvious OOP process.

twitort
  • 313
  • 2
  • 11

2 Answers2

1

If you inherit not from UserControl but from Panel your control remains a Panel and you can add child controls on it.

Cautions :

1) Child controls added by the control itself are considered as child controls by many Panel methods and getters (they aren't automatically hidden). I never tried this. But subclassing Form and derivating user forms from subclassed forms works fine. And the WinForms Designer gracefully display inherited childs as unmodifiable elements.

2) In Panel derivated class prefer to override OnXxx() method to do internal actions on Panel events than attaching handler to Xxx events: This will allow you to decide if your code should be executed before or after attached handlers by placing it before or after the call to base.OnXxx(sender, e);

Renaud Bancel
  • 853
  • 6
  • 5
0

Though the User Control itself that you are creating my contain a Panel, its technically not a Panel anymore in your application. It is technically a new control.

Maybe you should build a dynamic enough control where all the panels, and controls are on the same custom user control but dynamically show and hide them as needed.

logixologist
  • 3,694
  • 4
  • 28
  • 46
  • I have never tried this, but you could create a panel and drop the custom control on top of it. then you could add anything you wanted to your panel. – logixologist Jan 15 '14 at 23:44
  • This might be my ultimate solution. Clearly a user control won't work and subclassing panel sounds troublesome. Thanks! – twitort Jan 16 '14 at 17:47