|
Creating inherited .NET components in C# • The skeleton
Visual Studio .NET offers a lot of various components, that cover most of purposes. But there's always a hunger for something special. Some more functional, more easy-to-work, more fine component. New componets creation in Visual Studio is a very easy affair, although, sometimes it's connected with some inconvenience and illogical solutions. Nevertheless, soon we'll see how to create a full custom component in three simple steps
For a start, it's necessary to think out, what exactly we are going to implement. I was thinking for a long time, and I didn't find nothing better than a button for dialog windows, that changes a caption, according to the value of DialogResult property. Of course, a value of such a component is questionable, but it's quite good for a sample. Now, when we have set the strategic task, it's time to appeal to tactics and learn, how we'll attain our high aims.
First, we need to create a solution, such a metaproject, which contains some other projects. In Visual Studio main menu choose File | New | Project... In the opened window we choose Windows Control Library, enter a name (I entered in3steps), choose a path for files and press Ok. Editor should open, in which we can see files, created by environment for us. Then in Solution Explorer find UserControl1.cs, and rename it to something more plain. I chose nice name DialogButton
Now, we'll open this file, and look inside. There are many useless stuff. In particular, by default, Visual Studio creates user controls with keyword partial. In other words, these controls implemented in two files. Probably, it's made for complex components, where environment gives ability to insert components into components with visual form editor. We have a simple button, that won't contain any other components, so we don't need this stuff. So, we delete DialogButtin.Designer.cs from the project, delete word partial from class declaration, replace UserControl with Button in base type, remove InitializeComponent call from constructor. Now, the skeleton of our component is finished:
namespace in3steps
{
public class DialogButton : Button
{
public DialogButton()
{
}
}
}
|
Of course, now it's just a copy of standart button, but we want to take a look at it, and make sure, that everything is done correctly. We press F5, the environment hangs down a little and displays an error, saying, that there's no user controls are in our solution. What a pity! Automatic and handy TestContainer, for descendants of Button and other standard components, as for UserControl descendants would be very useful. We have to make a test application manually.
In Visual Studio menu choose File | New | Ğroject... In opened windows choose WindowsApplication, enter some name (i.e. "Test"), in Solution item choose Add to Solution, to add new project to our current solution and press ok. Then, find our new project Test in Solution Explorer, right-click on it and choose Set as StartUp Project, to make it run by default. Then, save the strenght and don't rename anything, since this project is needed only for testing of our button.
Now, find file Form1.cs in Solution Explorer window, double-click it to open form editor, find in3step Components tab and put our new DialogButton component on the form. Then, after pressing F5, we can admire our button, which is differs from standard only with class name though. How to make it differ more - we'll discover in next step.
|