Click here to Skip to main content
15,888,733 members
Articles / Desktop Programming / Windows Forms
Tip/Trick

DesignTime adding of Controls to a UserForm on a WinForm

Rate me:
Please Sign up or sign in to vote.
4.67/5 (5 votes)
15 Jul 2011CPOL2 min read 36.6K   3   7
An interesting change in behavior in VS 2010 Pro ?
Note 1: The result reported here is a hypothesis based on testing in Visual Studio Pro 2010, with the WinForms project set to compile against .NET FrameWork 4 Client Profile, only.

Note 2: This tip does not consider using Component Classes which are sub-classed as Containers. Only UserControl is discussed here.

Note 3: I appreciate all feedback, and correction, on the content of this Tip: hypothesis refutation welcome.

Background



0. Consider the action of drag-dropping a Control from the ToolBox onto a Panel, or GroupBox, on a Form at DesignTime: you will get visual feedback from the drop target, and the dropped Control will become a child of the Panel or GroupBox.

However, if you create a UserForm in a WinForms .NET solution, and then build the project, and then drag-drop an instance of the UserControl on a Form from the ToolBox: it will not 'accept' ('swallow' may be a better term)other Controls: in other words, the drag-dropped Controls will not become 'child' controls of the UserControl: rather, they will become 'child' Controls of the Form. And you do not have any visual feedback as you do when you drag-drop to a Panel, or other Container Control.
  1. An early Microsoft tech-note(1)[^] showed how to make a UserControl at DesignTime in a Windows Form 'accept' Controls drag-dropped from the ToolBox. This was accomplished by using the IDesigner attribute.
  2. In 2009, Henry Minute published a CodeProject article(2)[^] addressing the problem that a UserControl placed on a Windows Form at DesignTime inside another UserForm (i.e., nested) could not have new controls drag-dropped to it from the Visual Studio Toolbox, and have the UserControl 'accept' these dragged Controls as 'child' controls of the UserForm: these dragged Controls would remain 'child' controls of the Parent Form.

Something appears to have changed in VS 2010



0. adorning a UserControl with the IDesigner Attribute like this:
// you must have a reference set to System.ComponentModel.Design;
using System.ComponentModel.Design
//
namespace yourForm_NameSpace
{   
//
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
    public partial class yourUserControl: UserControl
    {
      // your UserControl code
    }
}

  1. As expected (based on the Microsoft tech-note cited: (1)): you now will find that you can drag-drop other Controls onto the top-level UserForm at DesignTime, and get DesignTime feedback.

  2. What is not expected: you can nest instances of this UserControl inside other instances of this UserControl, and you will still be able to drag-drop new Controls from the ToolBox onto the nested UserControls, and get Design-Time feedback, as if you were drag-dropping.

  3. What is also not expected: you can drag-drop a Control onto the Form surface, and then move it inside a top-level or nested UserControl adorned with the IDesigner Attribute, and it will be automatically 're-Parented:' however, you will not get the Design-Time drag-feedback you do when you drag-drop from the 'ToolBox.


Hypothesis



VS 2010 Pro, Windows Forms, now makes it easier to deal with DesignTime support ... via drag-drop of Controls from the ToolBox ... of nested UserControls without going to the lengths that Henry Minute went to in 2009 ... of implementing some complex DesignerProvider code ... in his excellent CodeProject article cited here(2).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer
Thailand Thailand
Human being, mortal, flawed.

Comments and Discussions

 
GeneralRe: Thanks, Henry, I'll download your code, and examine it tonig... Pin
BillWoodruff17-Jul-11 17:09
professionalBillWoodruff17-Jul-11 17:09 
GeneralRe: Bill, I have created a small app (using VS2008) to demonstr... Pin
Henry Minute17-Jul-11 9:30
Henry Minute17-Jul-11 9:30 
Bill,
I have created a small app (using VS2008) to demonstrate a few things. I have zipped it up and you can download it from http://dl.dropbox.com/u/17020164/ContainerControlTest.zip if you wish.

It is one form that is divided into two rows each containing three user designed controls. The top row is ContainerControl based and the bottom one is UserControl based.
Top Row:
Item 1. A control derived from ContainerControl just has BackColour changed. This is added to form. After adding to the Form, in the Form Designer, a Panel was added and a Button was added to this Panel. So ContainerControl supports design-time behaviour, as Johannes suggests.

Item 2. A control derived from ContainerControl has BackColour changed to match Item 1 then has a Panel added in the Control Designer. Small amount of code to resize the inner Panel. This is then added to the Form and a Button is Dragged and Dropped onto the inner Panel (which is why it is a different colour, so that I could see where I was Dropping). The Button appears to be OK but if you select it in the designer and press escape you will see that its Parent is in fact the controlHolder(x) which the control was placed on, NOT the Panel and NOT the ContainerControl that the Panel is part of You can also discover the parent by clicking the button at Run-Time.

Item 3. Basically the same as Item 2 except the inner Panel has been replaced by another ContainerControl. So it is a ContainerControl nested inside a ContainerControl. This was added to the Form and in the Form Designer a Button was Dropped onto the Inner ContainerControl. If you have your specs on, you might notice that it is not visible Smile | :) . That is because in spite of it's having been dropped onto the inner control it was added to the Controls collection of the outer ContainerControl. You can verify this by clicking the button alongside its caption at run-time (it has the up-arrow).

Bottom Row:
Item 1. This is a UserControl with just BackColour changed and your Designer Attribute added. It was then added to the form and treated exactly the same as Item 1 from the top row. It works exactly the same, so in this simple scenario the two approaches are identical except that the UserControl based one has some additional properties (BorderStyle etc.)

Item 2. The same as for the top row during its design a Panel was added (Contrasting colour, resize code) NOTE this one does not have the Designer Attribute. It was added to the Form and had a Button dropped on the inner Panel. Do the Parent test and you will get exactly the same result as the ContainerControl version.

Item 3. Same control as Item 2 but the Designer Attribute was applied. Added to Form. Button Dropped onto it. Not visible, as the one from the top row. When did you last see your parents? Same answer as the top row. The outer UserControl NOT the inner Panel.

So, point 1 the Attribute works in VS2008 .NET 3.5 as well as VS2010 and .NET 4.0.
Point 2, the Attribute does not replace all the kerfuffle in my article. What my article demonstrated was that you could design a control which contains other controls and then when you add that control to a Form you can select the inner control and therefore you can add controls to it.

Now that may not be required, after all you can build a functional equivalent of a SplitContainer by adding a Panel to your Form then adding two panels and a Splitter to that Panel and write the code that takes care of resizing, fixing one panel etc. etc. etc. and then do it again for the next form that you want this on. How much simpler to have a control ready built that will allow you to add controls to its various parts.

If you go back to my article and in the Form designer select the inner panel of the control then in the ToolBox Double-Click on Button, a Button gets added to that inner Panel. For the controls in this test app that will not work except for the first in each row.

Hope that this makes sense.

Cheers Smile | :) )
GeneralRe: Thanks, Henry. I would appreciate your commenting on my obse... Pin
BillWoodruff17-Jul-11 4:23
professionalBillWoodruff17-Jul-11 4:23 
GeneralHi, Johann, Your question is a very good one, and one I also... Pin
BillWoodruff15-Jul-11 14:05
professionalBillWoodruff15-Jul-11 14:05 
GeneralHmm, I'm not really into this subject, but when I once creat... Pin
johannesnestler14-Jul-11 22:59
johannesnestler14-Jul-11 22:59 
GeneralRe: Hi Johannes, I've posted a message to Henry Minute, in the f... Pin
BillWoodruff16-Jul-11 14:36
professionalBillWoodruff16-Jul-11 14:36 
GeneralRe: You are quite correct. You can base your own controls on Con... Pin
Henry Minute17-Jul-11 1:09
Henry Minute17-Jul-11 1:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.