Custom Panel
-
Please read on and help me! :doh: When you create a custom component and inherit (standard) from System.Windows.forms.Usercontrol, VS.NET shows a nice workarea in Design where you can place all kinds of winform controls like a textbox, button, etc. But when I try to inherit from Panel, the area in Design disappears and is replaced by screen which says "to add components to your class.... click here to switch to code view" :confused: I can't use this to make custom panels! What I'm trying to do is the following: I want a winform app (like Outlook) and everytime the user chooses a menuitem,I want to load the corresponding panel. I don't want to load all the panels at once, this will take to much time and memory. So that's why I want to inherit from Panel, add the needed controls and then load the panel at runtime when needed. Please, if you can help, mail me! Or if you think I should tackle this another way, tell me so! Thanks! Ernst Wolthaus
-
Please read on and help me! :doh: When you create a custom component and inherit (standard) from System.Windows.forms.Usercontrol, VS.NET shows a nice workarea in Design where you can place all kinds of winform controls like a textbox, button, etc. But when I try to inherit from Panel, the area in Design disappears and is replaced by screen which says "to add components to your class.... click here to switch to code view" :confused: I can't use this to make custom panels! What I'm trying to do is the following: I want a winform app (like Outlook) and everytime the user chooses a menuitem,I want to load the corresponding panel. I don't want to load all the panels at once, this will take to much time and memory. So that's why I want to inherit from Panel, add the needed controls and then load the panel at runtime when needed. Please, if you can help, mail me! Or if you think I should tackle this another way, tell me so! Thanks! Ernst Wolthaus
When you inherit from Panel,if you want to add controls just double click on a controls on toolbox and they added to your form,then you should manully set the positions , size and other properties in code-behind. Whats your problem with it? Mazy No sig. available now.
-
Please read on and help me! :doh: When you create a custom component and inherit (standard) from System.Windows.forms.Usercontrol, VS.NET shows a nice workarea in Design where you can place all kinds of winform controls like a textbox, button, etc. But when I try to inherit from Panel, the area in Design disappears and is replaced by screen which says "to add components to your class.... click here to switch to code view" :confused: I can't use this to make custom panels! What I'm trying to do is the following: I want a winform app (like Outlook) and everytime the user chooses a menuitem,I want to load the corresponding panel. I don't want to load all the panels at once, this will take to much time and memory. So that's why I want to inherit from Panel, add the needed controls and then load the panel at runtime when needed. Please, if you can help, mail me! Or if you think I should tackle this another way, tell me so! Thanks! Ernst Wolthaus
If you can't use the designer, just code the controls manually. As far as displaying custom panels (or any scrollable control, for that matter), derive your custom panel Types (classes) and store the Type of the derivative panels as properties of the item on which the action occurs, such as clicking on a menu item. Since you're dealing with
MenuItem
s, also extendMenuItem
with your own class and add a property, kind of like:public class MyMenuItem : MenuItem
{
private Type panelType;
public MyMenuItem() : this(null, null) {}
public MyMenuItem(string text) : this(text, null) {}
public MyMenuItem(string text, Type panelType) : base(text)
{
this.panelType = panelType;
}
public Type PanelType
{
get { return this.panelType; }
set { this.panelType = value; }
}
}Treat this menus like normal, adding them to a
MenuItems
collection just like normal. When a user clicks on theMenuItem
, you will use that Type as you'll see shortly. For brevity, assume that eachMenuItem
uses the sameClick
event handler:private void menu_Click(object sender, EventArgs e)
{
MyMenuItem menu = sender as MyMenuItem;
if (menu != null)
{
if (menu.PanelType != null)
{
// If you derived from Panel, cast to the base class here
// (or even simply Control).
Panel p = Activator.CreateInstance(menu.PanelType) as Panel;
if (p != null)
{
p.Dock = DockStyle.Fill;
leftContainer.Controls.RemoveAt(0);
leftContainer.Controls.Add(p);
}
}
}
}This is just a very basic example but I hope you get the idea. Creating instances of unknown Types (or from a collection of Types, perhaps configured in a .config file) is fairly common. I do this A LOT in our enterprise app I designed. It goes much deeper than this, but this is the essential idea.
-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
Please read on and help me! :doh: When you create a custom component and inherit (standard) from System.Windows.forms.Usercontrol, VS.NET shows a nice workarea in Design where you can place all kinds of winform controls like a textbox, button, etc. But when I try to inherit from Panel, the area in Design disappears and is replaced by screen which says "to add components to your class.... click here to switch to code view" :confused: I can't use this to make custom panels! What I'm trying to do is the following: I want a winform app (like Outlook) and everytime the user chooses a menuitem,I want to load the corresponding panel. I don't want to load all the panels at once, this will take to much time and memory. So that's why I want to inherit from Panel, add the needed controls and then load the panel at runtime when needed. Please, if you can help, mail me! Or if you think I should tackle this another way, tell me so! Thanks! Ernst Wolthaus
You can create a new class A inheriting from Panel adding the next lines of code. After that all the classes inheriting from A will have again the design view.
[Designer(typeof(System.Windows.Forms.Design.DocumentDesigner),typeof(System.ComponentModel.Design.IRootDesigner))] public class A : System.Windows.Forms.Panel { .... } ... public yourpanel : A { .... }
-
You can create a new class A inheriting from Panel adding the next lines of code. After that all the classes inheriting from A will have again the design view.
[Designer(typeof(System.Windows.Forms.Design.DocumentDesigner),typeof(System.ComponentModel.Design.IRootDesigner))] public class A : System.Windows.Forms.Panel { .... } ... public yourpanel : A { .... }
Thanks to all responses to my questions! Jose Fco Bonnin gave me just what I needed! After making my own custom panel type I inherited from that to create a panel I'm going to use in a winform app and it did show the design view again, great! :-D
-
When you inherit from Panel,if you want to add controls just double click on a controls on toolbox and they added to your form,then you should manully set the positions , size and other properties in code-behind. Whats your problem with it? Mazy No sig. available now.
When you inherit from Panel, you lose the design view in VS.NET. That was my problem. And when I lose the design view, I can't just add controls visually because it just doesn't work right anymore. But fortunately Jose gave me the solution, by added some attributes to the custom control. Thanks anyway!