TonysOpenSource
|
|
A User Control In C# |
Having just finished a project and put it up on the internet I started to look at what I wanted to write next. Nothing serious yet just looking at the interface and collecting my thoughts about how it was going to work. One thing I noticed when doing the front end for the dialog was that there were no controls that did exactly what I wanted. So thinking about it I decided that what I required was simply a way of entering a time in hours, minutes and seconds into a control of some sort, I didn't need real time updating of the control and I didn't care about the day or the date just a simple time entry control. There wasn't one. It's true that for something that simple it could have easily been mocked up with an edit box and a small amount of code to do the checking but having looked into C# for a while now I thought it might be interesting to take a look at if it could do Activex type controls. Controls are something I haven't done for a few years so I could probably do with a brush up anyway.
The project was developed with Visual Studio .NET on Windows XP
To create a control with C# you start a project for a user control from the new projects menu which will generate a project containing a main project file ( in this case the TimeSetControl.cs file ) that contains the code for the control. This is a standard C# file with a .cs extension. The control part is in the fact that the generated class inherits from the UserControl class which essentially gives a few form on which you can place other controls that are available to the development environment. These do not have to be predefined controls but can be controls of you own making. As this project isnt too technical all that was effectively done is the adding of a few drop down boxes and some labels that make up the data entry control that I required.
Once the control is built or even if you get a new control from somewhere else you are going to want to add it to the development environment so that it can be used in future projects. This is done through the Tools menu, in the Customize toolbox option. When you click on the Customize Toolbox option a dialog opens that allows the addition or removal of com or .net controls. Select the .net tab and there is a list of all the controls that are installed on the current computer, you may add or remove these controls from the development environment by selecting or unselecting the check boxes next to the names. To insert a new control click on the browse button and select the control that you wish to add and it will appear on the General Tab of the toolbar. This may all seem a little easy, especially if you remember OLE Controls and Activex but with the new common runtime the control itself is a class just like any other and to the outside world at least requires no special treatment in order to work with everything else.
The control itself is little more than a container that holds a few drop down boxes so that data can be added in the correct format. The interesting point here is the use of the get and set accessors because they are normally declared public the gui will allow you to set any variables through the Miscallaneous section of the properties. This can be either a blessing or not depending on if you want the variables to be accessed through the gui. If you dont want the variables to be accessed by the gui then there are two ways that you you can do it. The first is to declare and implement the get and set functions with a C++ style syntax which will be ignored by the gui and the second is to declare the get and set as private.
It has to be said that these days controls of this type are almost too easy to implement to be worth writing about. They are easy to create and once created they act just as any other form within the project so unless you are getting in to owner draw controls then creating a user control is simplicity itself.