How to sublass visually a C# class
-
I want to create my class based on C# class (like Button, Panel or ComboBox). The wizard allows to select a base class only from my classes (not built-in classes). What I'm doing now is create a class based on nothing and then change the declaration of the class (Ex: from class MyClass {} to class MyClass :Panel {}. But my goal is to create visually class. I also tried to create a class based on UserControl (this gives me opportunity to create visually class), where I can drop controls to this class (btw, UserControl doesn't have Dock and BorderStyle properities). But when I subclass this class again I cannot work visually. I have to do everything in the code. Is there any way to create visually classes? (Btw: Why, depending where the cursor is positioned, I getting different interface for creating the class. For example: when the cursor is positioned in the class view and I choose Add New Class from "Project\Add Class.." I'm getting "C# Class Wizard" (which usually crashes) where I can choose a base class. When the cursor is positioned somewhere else I'm getting "Add New Item" wizard where I cannot choose the base class. Thanks, Jerzy
-
I want to create my class based on C# class (like Button, Panel or ComboBox). The wizard allows to select a base class only from my classes (not built-in classes). What I'm doing now is create a class based on nothing and then change the declaration of the class (Ex: from class MyClass {} to class MyClass :Panel {}. But my goal is to create visually class. I also tried to create a class based on UserControl (this gives me opportunity to create visually class), where I can drop controls to this class (btw, UserControl doesn't have Dock and BorderStyle properities). But when I subclass this class again I cannot work visually. I have to do everything in the code. Is there any way to create visually classes? (Btw: Why, depending where the cursor is positioned, I getting different interface for creating the class. For example: when the cursor is positioned in the class view and I choose Add New Class from "Project\Add Class.." I'm getting "C# Class Wizard" (which usually crashes) where I can choose a base class. When the cursor is positioned somewhere else I'm getting "Add New Item" wizard where I cannot choose the base class. Thanks, Jerzy
You can't visually design a Control it -- by definition -- is only drawn via OnPaint and you wouldn't gain anything from visually designing because it isn't a container. A UserControl is what you use if you want to visually design a new control. It is a container that contains other controls. Therefore a visual interface is a nice feature to use (almost required in some cases). JerzyPeter wrote: (btw, UserControl doesn't have Dock and BorderStyle properities). It does have the Dock property since it inherits from Control; all Control's have the Dock property, BorderStyle doesn't exist though. JerzyPeter wrote: Btw: Why, depending where the cursor is positioned, I getting different interface for creating the class. I think this is by design, this has been the case since Beta 2. Hope I gave you some direction on what you should use for a new control. If you want to create a class and not a control, create a new class via the the C# Class Wizard then in the class view you should be able to add methods, properties, fields, and indexers (accessing a class instance like an array). James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978
-
You can't visually design a Control it -- by definition -- is only drawn via OnPaint and you wouldn't gain anything from visually designing because it isn't a container. A UserControl is what you use if you want to visually design a new control. It is a container that contains other controls. Therefore a visual interface is a nice feature to use (almost required in some cases). JerzyPeter wrote: (btw, UserControl doesn't have Dock and BorderStyle properities). It does have the Dock property since it inherits from Control; all Control's have the Dock property, BorderStyle doesn't exist though. JerzyPeter wrote: Btw: Why, depending where the cursor is positioned, I getting different interface for creating the class. I think this is by design, this has been the case since Beta 2. Hope I gave you some direction on what you should use for a new control. If you want to create a class and not a control, create a new class via the the C# Class Wizard then in the class view you should be able to add methods, properties, fields, and indexers (accessing a class instance like an array). James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978
Thanks for your answer. 1. UserControl doesn't have Dock property, it has DockPadding which determines the size of the border for docked control. 2. My problem is this: when I create UserControl with bunch of controls (buttons, labels, comboboxes) I want to be able reuse this control and create a subclasses control with extra, for example, 3 buttons, 3 lables and one slider, then I would have to do it in code instead dropping these control on my UserControl. Anyway, thank you Jerzy
-
Thanks for your answer. 1. UserControl doesn't have Dock property, it has DockPadding which determines the size of the border for docked control. 2. My problem is this: when I create UserControl with bunch of controls (buttons, labels, comboboxes) I want to be able reuse this control and create a subclasses control with extra, for example, 3 buttons, 3 lables and one slider, then I would have to do it in code instead dropping these control on my UserControl. Anyway, thank you Jerzy
JerzyPeter wrote: 1. UserControl doesn't have Dock property It does have a Dock property, you may be confused as to when you can use it though. The Dock property is used to tell the control where it should position itself on its parent. Thus you can't see it in the property grid while you are designing the UserControl, only when you place the UserControl on a form or another UserControl will you see it. JerzyPeter wrote: 2. My problem is this: when I create UserControl with bunch of controls (buttons, labels, comboboxes) I want to be able reuse this control and create a subclasses control with extra, for example, 3 buttons, 3 lables and one slider, then I would have to do it in code instead dropping these control on my UserControl. I've just created a test for this and it worked. However there are some rules that you need to be aware of. When you visually inherit from a Form or a UserControl VS.NET bases the appearance on the latest build available; this can cause problems if you change both the base class and the inherited class without doing a build in-between. Once you have your base UserControl done the way you would like, build your project/solution; this will create a compiled version of the UserControl so that you can edit the inherited class visually. Now add another UserControl to your project, open it up in CodeView then change the line that declares your class so that inherits from the UserControl you want to base your new UserControl from. Now you should be able to open the new UserControl up in the VS.NET Forms Designer. If you want e-mail me and I can post a quick sample showing this done on my website. HTH, James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978
-
JerzyPeter wrote: 1. UserControl doesn't have Dock property It does have a Dock property, you may be confused as to when you can use it though. The Dock property is used to tell the control where it should position itself on its parent. Thus you can't see it in the property grid while you are designing the UserControl, only when you place the UserControl on a form or another UserControl will you see it. JerzyPeter wrote: 2. My problem is this: when I create UserControl with bunch of controls (buttons, labels, comboboxes) I want to be able reuse this control and create a subclasses control with extra, for example, 3 buttons, 3 lables and one slider, then I would have to do it in code instead dropping these control on my UserControl. I've just created a test for this and it worked. However there are some rules that you need to be aware of. When you visually inherit from a Form or a UserControl VS.NET bases the appearance on the latest build available; this can cause problems if you change both the base class and the inherited class without doing a build in-between. Once you have your base UserControl done the way you would like, build your project/solution; this will create a compiled version of the UserControl so that you can edit the inherited class visually. Now add another UserControl to your project, open it up in CodeView then change the line that declares your class so that inherits from the UserControl you want to base your new UserControl from. Now you should be able to open the new UserControl up in the VS.NET Forms Designer. If you want e-mail me and I can post a quick sample showing this done on my website. HTH, James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978
Hi James! Thank you very much for you answer. I was really confused with this property Dock (because I didn't see it in design time). If you don't mind to send me the project I would appreciate very much. (Or just explain how to visually sublcass a UserControl because I think I won't be able to figure out from the project how to do that.) Thanks, you are a very kind person. Jerzy My e-mail: jerzypeter@hotmail.com
-
Hi James! Thank you very much for you answer. I was really confused with this property Dock (because I didn't see it in design time). If you don't mind to send me the project I would appreciate very much. (Or just explain how to visually sublcass a UserControl because I think I won't be able to figure out from the project how to do that.) Thanks, you are a very kind person. Jerzy My e-mail: jerzypeter@hotmail.com
My SMTP server is having problems right now so I'll just post it here. Sample Project Here’s what I did to create the sample. Create a new Windows Application, right click on the project in the solution explorer and choose Add UserControl, call it BaseUserControl.cs. On the BaseUserControl set the ForeColor to the System palettes Highlight color. Resize the canvas so it is 232x40 pixels. Now add a new label control to it and place it at (8, 8) with the default size. Change the Text on the label to say “base”. Build the project. Now Add another UserControl to the project (right click on the project choose Add UserControl). Call it ExtendedUserControl.cs. Now close the designer for it, right click on ExtendedUserControl in the solution explorer and choose View Code. Now change the class declaration so that it inherits from BaseUserControl instead of System.Windows.Forms.UserControl.
public class ExtendedUserControl : System.Windows.Forms.UserControl
changes topublic class ExtendedUserControl : BaseUserControl
Build the project again. Now open up the designer for the ExtendedUserControl (double click ExtendedUserControl in the solution explorer). When you open up the designer for the ExtendedUserControl you should see the BaseUserControl, now add another label placing it at (120, 8) with the default size. Change the Text to read “extended”. Now click on the background of the UserControl, and set the ForeColor to the System palette’s ControlText color. Build the project again. Now open up Form1 by double-clicking on it in the Solution Explorer. In the toolbox you should see two new entries at the bottom; these are the new UserControls you just created. Drag one of each onto the form. VS.NET will tell you when you make a change that requires a build for you to see it; but to be on the safe side I always do a build. If you have any other questions feel free to ask. James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978 -
My SMTP server is having problems right now so I'll just post it here. Sample Project Here’s what I did to create the sample. Create a new Windows Application, right click on the project in the solution explorer and choose Add UserControl, call it BaseUserControl.cs. On the BaseUserControl set the ForeColor to the System palettes Highlight color. Resize the canvas so it is 232x40 pixels. Now add a new label control to it and place it at (8, 8) with the default size. Change the Text on the label to say “base”. Build the project. Now Add another UserControl to the project (right click on the project choose Add UserControl). Call it ExtendedUserControl.cs. Now close the designer for it, right click on ExtendedUserControl in the solution explorer and choose View Code. Now change the class declaration so that it inherits from BaseUserControl instead of System.Windows.Forms.UserControl.
public class ExtendedUserControl : System.Windows.Forms.UserControl
changes topublic class ExtendedUserControl : BaseUserControl
Build the project again. Now open up the designer for the ExtendedUserControl (double click ExtendedUserControl in the solution explorer). When you open up the designer for the ExtendedUserControl you should see the BaseUserControl, now add another label placing it at (120, 8) with the default size. Change the Text to read “extended”. Now click on the background of the UserControl, and set the ForeColor to the System palette’s ControlText color. Build the project again. Now open up Form1 by double-clicking on it in the Solution Explorer. In the toolbox you should see two new entries at the bottom; these are the new UserControls you just created. Drag one of each onto the form. VS.NET will tell you when you make a change that requires a build for you to see it; but to be on the safe side I always do a build. If you have any other questions feel free to ask. James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978Hi James! I'm sorry but it doesn't work for me. I did everything as you did (and I was doing the same before). When I double-click ExtendedUserControl in the solution explorer I'm getting an error: ExtendedUserControl - The base class test.BaseUserControl coud not be loaded. Ensure the assembly has been referenced or built it is part of the project. Jerzy:confused:
-
Hi James! I'm sorry but it doesn't work for me. I did everything as you did (and I was doing the same before). When I double-click ExtendedUserControl in the solution explorer I'm getting an error: ExtendedUserControl - The base class test.BaseUserControl coud not be loaded. Ensure the assembly has been referenced or built it is part of the project. Jerzy:confused:
Did you remember to build the project? That is the error message I get when I forget to do that step. Give my sample project a try (you'll have to build it before you can view the user controls though). James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978
-
Did you remember to build the project? That is the error message I get when I forget to do that step. Give my sample project a try (you'll have to build it before you can view the user controls though). James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978
Hi James! I'm scared to write to you again but belive me it still doesn't work for me. I'm doing exactly the same things as you. When I open your project I can easily create another subclass of ExtendedUserCotrol. I don't know what's going on. I tried on different machines with the same result. I was thinking maybe I have different setting in IDE. My Microsoft .NET framework is 1.0 ver. 1.0.3705, Development Environment 2002 ver. 7.0.9466 Thanks Jerzy Btw: I found another way of subclassing (but still only in your project): you can 'Add New Item' and choose Inherited Control and select base ExtendedUserControl for example. :confused: :confused: :confused:
-
Hi James! I'm scared to write to you again but belive me it still doesn't work for me. I'm doing exactly the same things as you. When I open your project I can easily create another subclass of ExtendedUserCotrol. I don't know what's going on. I tried on different machines with the same result. I was thinking maybe I have different setting in IDE. My Microsoft .NET framework is 1.0 ver. 1.0.3705, Development Environment 2002 ver. 7.0.9466 Thanks Jerzy Btw: I found another way of subclassing (but still only in your project): you can 'Add New Item' and choose Inherited Control and select base ExtendedUserControl for example. :confused: :confused: :confused:
JerzyPeter wrote: I'm scared to write to you again but belive me it still doesn't work for me. Nah don't be scared, I don't bite :) I'm curious as to why it isn't working for you when I have no problems doing it. JerzyPeter wrote: My Microsoft .NET framework is 1.0 ver. 1.0.3705, Development Environment 2002 ver. 7.0.9466 Same versions I'm showing. Did your box have another version of .NET on it before (one of the betas or an RC)? I repaved this one about 2 weeks ago. Funky things happen when you mix a pre 1.0 beta with the final :-D JerzyPeter wrote: Btw: I found another way of subclassing (but still only in your project): you can 'Add New Item' and choose Inherited Control and select base ExtendedUserControl for example. Yeah, I forgot about that one. I was thinking it was only there for forms. :) James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978