|
To use ParentControlDesigner you will need to add two references to your project.
1) System.Windows.Forms.Design
The IDE might even suggest and add that one for you, if you click somewhere in the middle of ParentControlDesigner , you will see a small rectangle under the final 'r' move the mouse over that and select it from the options offered.
2) System.Design
You will have to add this yourself from the Solution Explorer window. There are a lot of these type of classes that use System.Design, and the only way I know to determine if it is needed is to examine the MSDN documentation.
From my Local MSDN installation
ParentControlDesigner Class
Members Example See Also Send Feedback
Extends the design mode behavior of a Control that supports nested controls.
Namespace: System.Windows.Forms.Design
Assembly: System.Design (in System.Design.dll)
From MSDN on t'web
ParentControlDesigner Class
Base designer class for extending the design mode behavior of a Control that supports nested controls.
For a list of all members of this type, see ParentControlDesigner Members.
System.Object
System.ComponentModel.Design.ComponentDesigner
System.Windows.Forms.Design.ControlDesigner
System.Windows.Forms.Design.ParentControlDesigner
System.Windows.Forms.Design.ScrollableControlDesigner
[Visual Basic]
Public Class ParentControlDesigner
Inherits ControlDesigner
[C#]
public class ParentControlDesigner : ControlDesigner
[C++]
public __gc class ParentControlDesigner : public ControlDesigner
[JScript]
public class ParentControlDesigner extends ControlDesigner
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
ParentControlDesigner provides a base class for designers of controls that can contain child controls. In addition to the methods and functionality inherited from the ControlDesigner and ComponentDesigner classes, ParentControlDesigner enables child controls to be added to, removed from, selected within, and arranged within the control whose behavior it extends at design time.
You can associate a designer with a type using a DesignerAttribute. For an overview of customizing design time behavior, see Enhancing Design-Time Support.
Requirements
Namespace: System.Windows.Forms.Design
Note that the clue is in different places depending on how you are viewing the page.
Hope this helps.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
I used the same code but it is not allowing me to drop controls....
|
|
|
|
|
I have just created the simplest control that I could think of. It allows me to drop controls on to it, dock them etc. etc. etc.....
Here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace ExplorerBarLibrary
{
[
Designer(typeof(ParentControlDesigner))
]
public partial class TestControl : UserControl
{
public TestControl()
{
InitializeComponent();
}
}
}
and the *.Designer.cs file
namespace ExplorerBarLibrary
{
partial class TestControl
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
private void InitializeComponent()
{
this.SuspendLayout();
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 14F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Name = "TestControl";
this.Size = new System.Drawing.Size(148, 148);
this.ResumeLayout(false);
}
#endregion
}
}
If that doesn't work for you, then there is something wrong with your system is all I can think.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
This code is working fine but i need a panel to work like this if you place a panel and try the same code the panel is not holding the controls...
You try to place a single panel as a user control and then try to implement the code such that the panel can hold the controls.. Thats what i need...
Dont think otherwise i am a beginner a trainee in a company and i am asked to do this. I am not expert in .net.
Sorry if am wrong in asking like this.....
|
|
|
|
|
honey.rpk wrote: Dont think otherwise i am a beginner a trainee in a company and i am asked to do this. I am not expert in .net.
Sorry if am wrong in asking like this.....
You are not wrong to ask, at all. Please do not think I was being rude, I was not. I am as confused as you are. I am not an expert in anything.
This next bit is a joke.
You should never aspire to be an expert. 'ex' signifies a has-been and a 'spurt' is a drip under pressure.
I will investigate the Panel thing and will get back to you whatever I find.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
It occurred to me just after I sent my previous reply, that a panel already has the capability to host child controls. You do not have to do anything to make it do so. Therefore I can only assume that you are doing something to the panel, which is preventing it from behaving normally.
To confirm this I have just put a Panel on a Form , added the TestControl I sent earlier to the Panel and then added another Panel to the TestControl . All were visible, all moved together when the base Panel was moved, all were dockable , and so on.
So could you post the code for your Panel control. Maybe I will be able to see something that will enable you to solve your problem.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Actually the user control that i have to develop is a combination with two panels one below the other. and the first panel has a buttons and a label on it. Now when ever the button click happens the panel that is present on the down should visible and invisible alternatively.By default it is in 'visible' state.
This is the user control.
And now when i use this usercontrol in a form i should be able to drag controls in to the second panel which is by default visible and all the controls should act as child controls to the user control. I mean when ever we drag the user control they should move along with it..
for that this is the code i have written
partial class tool
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.panel2 = new System.Windows.Forms.Panel();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.SandyBrown;
this.panel1.Controls.Add(this.button2);
this.panel1.Controls.Add(this.button1);
this.panel1.Controls.Add(this.label1);
this.panel1.Location = new System.Drawing.Point(14, 13);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(200, 40);
this.panel1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(136, 5);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(23, 23);
this.button1.TabIndex = 3;
this.button1.Text = "1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(20, 10);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(29, 13);
this.label1.TabIndex = 2;
this.label1.Text = "label";
//
// panel2
//
this.panel2.BackColor = System.Drawing.Color.Peru;
this.panel2.Location = new System.Drawing.Point(14, 47);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(200, 115);
this.panel2.TabIndex = 1;
//
// UserControl1
//
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(221, 172);
this.Load += new System.EventHandler(this.tool_Load);
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private string labeltext;
private Color pnl1color;
private Color pnl2color;
public string Labeltext
{
get
{
return labeltext;
}
set
{
labeltext = value;
label1.Text = labeltext;
}
}
public Color Pnl1color
{
get
{
return pnl1color;
}
set
{
pnl1color = value;
panel1.BackColor = pnl1color;
}
}
public Color Pnl2color
{
get
{
return pnl2color;
}
set
{
pnl2color = value;
panel2.BackColor = pnl2color;
}
}
}
and in .cs file i have written this code
namespace tool
{
[Designer(typeof(ParentControlDesigner))]
public partial class tool : UserControl
{
public tool()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
some code to make panel visible and invisible
}
private void tool_Load(object sender, EventArgs e)
{
panel2.Visible = true;
label1.Text = "enter name of panel";
}
Thats it thats what i did. and when i tried to add controls to the second panel during design time i am not able to do that....
thanks for ur support.....
|
|
|
|
|
I have solved your problem!!!!
I am just going to try to find some references before I post the code, in case you are unable to understand my explanation.
Don't go away.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Thanks for the reply and kind approach.
|
|
|
|
|
Before I post the code, I would like to give you a couple of pointers about the code you posted.
1) the namespace. Do try not to use the same name for your namespace as you do for your class. This can sometimes confuse Visual Studio IDE
and generally cause delays that you don't need, and you have to write stuff like 'tool.tool.whatever', which just looks ugly . For example, if the code you posted is in the same project as your main form, have the namespace as something like toolTestApp, if it is a separate project have the namespace something like toolLibrary.
2) Where you have put your code. Just from the way the code was posted it looks as if this:
private string labeltext;
private Color pnl1color;
private Color pnl2color;
public string Labeltext
{
get
{
return labeltext;
}
set
{
labeltext = value;
label1.Text = labeltext;
}
}
public Color Pnl1color
{
get
{
return pnl1color;
}
set
{
pnl1color = value;
panel1.BackColor = pnl1color;
}
}
public Color Pnl2color
{
get
{
return pnl2color;
}
set
{
pnl2color = value;
panel2.BackColor = pnl2color;
}
}
is in the 'tool.Designer.cs' file. Bad Idea! The IDE is continually rewriting this file, your stuff could get deleted/overwritten, it probably won't at the moment but Microsoft could change the way that it creates the file so that it totally deletes the original first. Don't take the chance, move all that stuff to the 'tool.cs' file.
OK now the good bit.
The first thing to do is to create your own Designer to use instead of ParentControlDesigner . You will need what ParentControlDesigner does, so inherit from that. Here's the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms.Design;
namespace toolLibrary
{
public class toolDesigner : ParentControlDesigner
{
public override void Initialize(System.ComponentModel.IComponent component)
{
base.Initialize(component);
if (this.Control is tool)
{
this.EnableDesignMode(((tool)this.Control).ControlPanel, "ControlPanel");
}
}
}
}
Now the changes to your code. There aren't many, and I've tried to highlight them all, but read it very carefully in case I missed any.
I'm only posting the main file as the only changes I made to the 'tool.Designer.cs' file was to move the stuff I talked about earlier.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace toolLibrary
{
[Designer(typeof(toolDesigner))]
public partial class tool : UserControl
{
public tool()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void tool_Load(object sender, EventArgs e)
{
panel2.Visible = true;
label1.Text = "enter name of panel";
}
public string Labeltext
{
get
{
return this.label1.Text;
}
set
{
label1.Text = value;
}
}
public System.Drawing.Color Pnl1color
{
get
{
return this.panel1.BackColor;
}
set
{
panel1.BackColor = value;
}
}
public System.Drawing.Color Pnl2color
{
get
{
return panel2.BackColor;
}
set
{
panel2.BackColor = value;
}
}
[
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
]
public Panel ControlPanel
{
get
{
return this.panel2;
}
}
}
}
Well, that's it. Give it a try. If it doesn't work, or you don't understand anything, please come back.
I couldn't find any decent references except for MSDN and you know where that is don't you.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
modified on Tuesday, June 30, 2009 2:14 PM
|
|
|
|
|
Kudos....Sir Henry.
Why can't we have a privilege to have a decent blog post or an article?
|
|
|
|
|
I started an article almost before I had finished the post.
I have to say that I am more excited about finding this, than anything I have coded for ages. Tehe tehe tehe.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Hi Henry,
I couldn't wait to try out your code today ! This is an area I'm actively studying and frankly have not found the MSDN examples on this very grokkable. The one book I have on hand that is very useful is Matthew McDonald's book on Pro WinForms and Controls 2.0, chapters 13, and 26, but that's dated now.
I think your solution provides an elegant way to construct usercontrols with container sub-components some of which can function as design-time containers, and others of which can't. Much better than the awkwardness of the idea I came up which would have required building such critters at design-time on a form by individually placing each element as a separate UserControl.
One very small suggestion : only setting the text in the Label at design-time will NOT allow design-time changes to persist at run-time; right now the Load event called at run-time will over-write any changes.
fyi : this is working fine in VS 2010 beta.
Since starting to work on this I've come across all kinds of programming sites where people are posting questions complaining that 'ControlDesigner doesn't show up : you would think MS would do somthing to automatically hook in the System.Design.dll when you declared your usage of the System.Windows.Forms.Design library (ha, ha, right, in the "ideal" world).
Again, thanks for the "lesson."
best, Bill
"Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844
modified on Wednesday, July 1, 2009 5:37 AM
|
|
|
|
|
BillWoodruff wrote: The one book I have on hand that is very useful is Matthew McDonald's book on Pro WinForms and Controls 2.0, chapters 13, and 26, but that's dated now.
I have that book as well, very useful, as you say. I also have Windows Forms 2.0 Programming by Sells and Weinhardt which of course is also somewhat dated now.
BillWoodruff wrote: One very small suggestion : only setting the text in the Label at design-time will allow design-time changes to persist at run-time; right now the Load event called at run-time will over-write any changes.
As far as this is concerned, you are, of course, correct. However, the code I posted is really just the OPs with additions from me to enable the design time behaviour and I regret to say that apart from any glaring problems I didn't really read it closely.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Bravo Henry !
I'd vote for that response for "above and beyond" humanity points
Look forward to your article.
best, Bill
"Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844
|
|
|
|
|
Thank you so much Henry Minute..
Its really working very good....
Once again thank you so much for solving my problem.....
|
|
|
|
|
I have one more doubt..
If at all i want to make the first panel also active at design time as the second panel,along with the controls already present on it(1st panel) ,i mean the button and label should be there. And the functionality of visible and invisible alternatively on clicking the button should work..
Is it possible for me to do so..
IF it is possible can u guide me how...
Thanks & regards
|
|
|
|
|
According to the MSDN documentation all you have to do is repeat the steps I showed you for your lower panel with the upper panel.
1) create a public property for it, with the DesignerSerializationVisibility attribute
2) in the designer copy the EnableDesignMode line in Initialize changing the Control references to those you made in 1)
As far as the controls within those controls are concerned, I have not yet got round to experimenting along those lines. You might be able to repeat the two steps above for them as well, or you might have to change your panels into user controls deriving from Panel and give them their own designer which will use the EnableDesignMode stuff.
The thing is, I found out how to do it by experimenting and that is the best way to learn. Be brave, try things out. Start a new thread in the forum if you get half way and get stuck.
Good Luck!
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
|
You need to add a reference to System.Design in your project. (use the 'References' context menu in Solution Explorer) you will then need using System.Windows.Forms.Design in your code file.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
THanks for your help..
I completed the work given for me successfully only because of your help...
Thank you...
Both the panels are editable for me now.....
|
|
|
|
|
My pleasure.
This time I learned something new as well, so it was good for both of us.
Good Luck with the rest of your work!
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
|
You should post this as a new thread.
Hijacking another thread is considered rude.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Hi Honey.rpk, D@nish, and Henry,
The roosters have been crowing loud since before 5am here, but nested UserControl hosting at design-time is not what's keeping me up, I assure you
Let me clear this up :
1. I'm using VS 2010 beta.
2. Creating a new Win Forms project in VS 2010 beta does not auto-include a reference to the System.Design dll which, I guess, the other .Design classes depend on. Was this also the case in VS 2008 ?
3. If you make sure you have a reference set to the System.Design dll, and you have declared :
using System.Design;
using System.Windows.Forms.Design;
you can use Henry's technique which is also shown in Matthew McDonald's aforementioned book on page 926.
4. If you don't have a reference in your project to System.Design.dll, you can still use the technique I included in C# which D@nish also pointed you to in an article doing the same thing in VB. I'm surprised this technique works without a reference to System.Design dll being set !
5. Nesting :
a. forget it : an instance of userControl #2 with ParentControlDesigner attribute placed inside userControl #1 with same attribute : followed by placing usercontrol #1 on a form at design-time ... after a build ... is NOT going to let the instance of UserControl #2 capture other controls.
b. if you can't forget it : there is a way (ugly, it is) :
1. build your separate usercontrols 1,2
2. adorn each of them with the ParentControlDesigner attribute
3. at design time on a Form :
a. place an instance of usercontrol #1 on the form
b. place an instance of usercontrol #2 on the form and drag it so is "captured" usercontrol #1
c. you will now find both usercontrols are capable of capturing controls.
d. disclaimer : not tested with derived controls inheriting from containers like Panel
Disclaimer : for all I know there are other more elegant methods to achieve such weirdness; but a good question is : "what design advantages come from such design-time nested usercontrols or derived controls ?"
If anything, I can see such "beasts" as a liability, but, who knows, maybe there is some scenario where such "critters" are just right for the zoo.
best, Bill
"Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844
|
|
|
|
|