Building control derived from Windows Forms control in Visual Studio 2005: Part I


In the article we have discussed how a control can be built in Visual Studio 2005. We combined the functionality of more than one Windows Forms controls with our code and used inheriting from the UserControl Class. But if you want just to extend the functionality of an existing Windows Forms control (add some property to the ComboBox control, changed back color  of the TextBox  on some event etc.) you can create a control (for example: ComboBox_C control) derived from an existing control (ComboBox control) through inheritance. In this article I share how you can build your own Windows derived from an Windows Forms control in Visual Studio 2005. The examples are written using C#.

Our task will be defined as follows :

  1. to build ComboBox_C control that derived from the Windows Forms ComboBox control;
  2. to add to the control three properties : the first one, named C_AddRow, has  type of  "bool"; the second property, named C_AddValue, has  type of "object" ; and the third property, named  C_AddDisplay , has type of "string"; by default value of the C_AddRow property is "false" and in this case the ComboBox_C is completely equivalent to the ComboBox;  there is possibility to add one item to the top of the items (received from the DataSource property of the control) by changing the C_AddRow property to "true"; the "display" part of the added item equals to the C_AddDisplay property and the "value" part of the added item equals to the C_AddValue (that can be changed to a string or to an integer). 

Let's create a solution  for building and testing our ComoBox_C control.

 

The first step is : creating our "basic" Windows Control Library project , named "ComoBox_C" , and solution , named WinProjects_Test (fig. 1).

 



Figure 1.

 

Inside the project we , in fact , need user control named "ComboBox_C ". The best way to do that is : to delete the UserControl1 (fig. 2) and then to add a user control with the name which we need (fig. 3, fig. 4, fig. 5).

 



Figure 2.

 



Figure 3.

 



Figure 4.

 



Figure 5.

 

We have to create the control that derived from an existing control (ComboBox control) through inheritance. OK! With this purpose  inside the code of our control ( fig. 6 ) change UserControl to ComboBox (fig. 7).

 



Figure 6.

 



Figure 7.

 

After these changes in the code the ComboBox_C.cs[Design] at once will change (fig. 8).

 

Figure 8.

 

From now our control has all properties, events etc. of  an existing Windows Forms control , namely, of the ComboBox control. We just have to add some lines of code to our ComboBox _C control in order to "adjust" the control to our requirements.

But first of all we have to finish building of our solution. With this purpose add to the solution project "UC_Test" (fig. 9, fig. 10) which includes only one Windows Form named "Form1". With its help we just test our ComboBox _C  control.

 



Figure 9.

 



Figure 10.

 

In order to get some data source for our control we will use a project named "GetData" (fig.11)  that has Output Type of  "Class Library" and includes only one class named "GetDataHelp". With its help we just will get data table. By the way, you can use real data base, or can use the "GetData" dll from the article  etc. and don't add the "GetData" project

 

 

Figure 11.

           

OK! We have finished to build our solution (fig. 12):

 

 

Figure 12.

 

Before adding some code to our classes let's test the solution . We are going to build solution and then just "run"  it. We will receive two errors. The first one is : "ComboBox_C.ComboBox_C does not contain a definition for AutoScaleMode" . The AutoScaleMode property is new in the .NET Framework version 2.0 and specifies the current automatic scaling mode of the control. But (do you remember ?!) we derived our control  from an existing control , namely, ComboBox  control and not from UserControl.

 

Taking it into consideration we will eliminate  the error (without any "complications" for the control) very quickly : inside "ComboBox_C.Designer.cs" we just remove line of  the code (fig. 13).

 

Figure 13.

           

Now , if you click "run" button , you will get an exception like this: "Assembly 'D:\ml_Projects\WindowsApplication_2005\WinProject_Test\ComboBox_C\obj\Debug\ComboBox_C.dll' doesn't contain any UserControl types.". OK! If you remember, we began to build our solution from the "ComboBox_C" project; and , of course, by default this project is the startup project in the solution. All that have to be done is: to open the Solution Properties window and to change the startup project (fig. 14).

 

Figure 14.

 

OK! Now we can add code. But we will do that in the second part and now let's drink a cup of coffee.

 

Good luck in programming !