|Authors||Yaroshko Sergii, Yaroshko Svitlana|
|Name of the article||Usage of the event mechanism of C# .NET in creation of Windows Forms component|
|Abstract||Let us suppose we want to ask a user of our application to make a choice from some alternatives. This task is quite simple: we can put several radio buttons into the application window and recognize programmatically which of them the user has selected. If we want to have several separate groups of RadioButton controls, the most common method is to group them in some container controls, such as Panel or GroupBox. It works well at first time, but after a couple iterations with container and buttons, we will desire to have something more useful, for example, a control containing a collection of radio buttons. It would be similar to the TRadioGroup component from Visual Component Library that was very popular in earlier time.
The article describes in details the creation process of a new user-defined control by C# for the Windows Forms framework – the RadioGroup control. It is paid attention to the IDE tools of design time support of the RadioGroup control, such as categories and definitions of properties and events, property editors, smart-tags and so on. To achieve the goal we used in many cases the events – one of the most important mechanisms of object-oriented programming in C#. While the RadioGroup control were created, we have handled existing events of many components of the framework, have created and used several events of the control and of its parts.
The control will display a group of radio buttons bounded by border with a caption text. The key property of the RadioGroup control will be Items – a collection of strings representing the buttons names. The control creates radio buttons automatically, according to the Items content, like a ListBox control does, arranges the buttons uniformly over the own surface in one or more columns. The RadioGroup control notifies a program about the button index that the user has selected.
At first glance, there are no obstacles to achieve this goal. We can construct a user control with a GroupBox (to provide the control with a Text and a border), a TableLayoutPanel (to arrange the buttons), an indexed collection of names (a StringCollection) and an indexed collection of buttons (a List
We have to do some work to fix the problem with the Item property. Every changes of the names collection must reflect on the buttons collection. The name collection should signal about changes to the parent control. For this purpose we can define a new class that derives from StringCollection, extend it with an event and redefine every add-remove methods to signal the event.
The IndexChanged event is a specific event of the RadioGroup control. It is also the default event. It raises when a user clicks on an unchecked button or sets a new value to the IndexSelected property. Mouse events occur with the enclosed controls of the RadioGroup: with a button or a table layout panel. Thus we have to take care to transfer the events to the parent control. So the button_Click() method calls the RadioGroup.OnClick() and the theTableLtPnl_DoubleClick() method calls the RadioGroup. OnDoubleClick().
To teach our control to build buttons at design time we can write a special designer for our control and make it responsible for assigning the handler to the IcomponentChange-Service.ComponentChanged event. This event raises every time any property of any control of the application has changed while the application is being designed, so we have to recognize the type of control and the name of its changed property.
|PDF format||Yaroshko Sergii, Yaroshko Svitlana |