Develop a simple composite control with Visual C # .NET
Summary: This article describes how to develop a simple composite control (Composite Control) with Visual C # .NET 2002, primarily explain the property (property) and event processing during the development process, so that the developer is based on the .NET platform I need to develop suitable controls.
When we do Windows Forms development under the .NET platform, we often need to combine existing controls for a special purpose, such as the combination of Label and TextBox controls are very easy to lay out on the form (remember Delphi 6 There is such a control), and the control combined with specific patterns and text is ideal for displaying the company's LOGO. The .NET platform provides us with great tools and techniques to create such custom controls. Let's let us create a control that can automatically display time.
It is worth noting that .NET platform provides us with the development technology of three controls, namely, inheritance control, Composite Control, and Custom Control, the translation may vary Everyone can be in English). We are now concerned about the second - composite control. OK, Let's Go!
1. Create a control engineering. Built a "Windows Control Library" project in Visual C # .NET, named "TimeLabel", as shown in Figure 1:
2. Change the namespace, the control class name. The default namespace is TimeLabel (like the project name), the control class name is UserControl1. Please change the namespace to your development habits. If you are leoyang.controls.timelabel, of course, you can also choose to keep the default namespace; preferably change the control class name to meaningful name, such as Timelabel, When the control appears on the toolbox, it is displayed as TimeLabel instead of userControl1. Note that once the class name is changed, the corresponding construction method name is also changed accordingly, such as:
Public userControl1 ()
{
...
}
It is necessary to change.
Public Timelabel ()
{
...
}
3. Add an existing control. Since we want to create a new control on the basis of existing controls, you should first add available existing controls to the control design interface. Please double-click Label in Toolbox, and double-click Timer. Two existing controls are then added to our design interface, as shown in Figure 2:
4. Announces control properties. Because you want to write time into the Text property of the Label, we need to disclose the label1.text attribute. Add the following code in the Timelabel class:
Public String LabelText
{
get
{
Return label1.text;
}
}
Note that we don't need to let the control user change the value of Label1.Text, so LabelText is a read-only attribute.
In addition, for aesthetic consideration, we also publish a LabelBackColor property to the control user to obtain and set the Label's BackColor property. code show as below:
Public Color LabelbackColor
{
get
{
Return label1.backcolor;
}
set
{
Label1.backcolor = value;
}
}
Of course, you can also add some properties as needed, such as font, control size, etc., so that control users can use controls more flexibly.
Here, I need to add some of you. The important one in the composite control is: Any constitute control (constructance control) must be disclosed by adding the properties of the composite control, not directly to PUBLICs. Level is open. For example, we should not set the access level of the Label control to public (default is private). The purpose of this is to let us better grasp the data security of the control, so that only the most needed attributes will be made to the control user.
5. Time display processing. To now, we can increase the code to make our controls show time. First, set the TIMER1's Interval property to 1000, which is the time separated by 1 second. Then double-click Timer1, add the following code during its Tick event processing:
Label1.text = system.datetime.now.tolongtimeString ();
This way, every other second, our Label will reset the current system time. Finally, double-click the blank place on the Timelabel control, add the following code during the Timelabel_Load event process, add the following code to activate Timer:
Timer1.enabled = True;
In this way, Timer will be loyal to calculate the time and update the time text on the Label.
6. Event processing. Since it is inherited from UserControl, TimeLabel has Click, DragDrop, FontChanged events from the beginning. What we have to do now is to add a custom event --Tick so that the form time of notifying the use of our control has changed. Because we only need to simply let this incident, we don't need to create our own agent (DELEGATE) function, and do not need to create a special event department. OK, please see the following code:
First add TIMLabel class to the TIMLABEL class:
Public Event EventHandler Tick;
Then write a calling process to the event, please note the naming of the process:
Protected Void ONTICK (Eventargs E)
{
IF (Tick! = NULL)
{
Tick (this, e);
}
}
In addition, during the TIMER TIMER TIMER TIMER, the call to the ONTICK should also be added, the code is as follows:
Private void Timer1_Tick (Object Sender, System.EventArgs E)
{
Label1.text = system.datetime.now.tolongtimeString ();
ONTICK (E);
}
In this way, our control is written, create the project. In order to test it, we need to create a new Windows project. Proceed as follows:
7. Create a test item. In Visual C # .NET, create a new Windows application, create a new Windows application, name TestTimeLabel, and add the current solution, as shown in Figure 3:
8. Add control references. Before using custom controls, we must add controls to "Toolbox". The method is: Right-click "Toolbox", click "Custom Toolbox", select ".NET Framework Components" page in the "Custom Toolbox" dialog box pop-up, then click "Browse", locate and open us The Timelabel control proprietary assembly created, so that the control appears in the ".NET Framework component" list, as shown in Figure 4: Click "OK" to add TimeLabel control to "Toolbox "In Figure 5:
9. Use the control. Now we can drag and drop the Timelabel we created to a Windows Form like other controls, set its properties and respond to its events. For example, you can set the color LabelbackColor of Timelabel in the Properties window for your favorite colors. Of course, except for LabelbackColor, there is a large number of properties for setting, and if you are willing, you can also return to the Timelabel project to add other properties to the above-mentioned method, so that the control functions and user interfaces are more abundant.
10. Response event. We have added a TIMELabel to TimeLabel, and every time the time is changed. So how do our programs know that Tick events have happened and respond to it? Methods as below:
First increase the event handling process as follows (the name can be customized, but must have the parameters of the object and Eventargs type, and return with the Void type):
Private void Tickhandler (Object Sender, Eventargs E)
{
System.Diagnostics.debug.writeline (Timelabel1.Labeltext);
}
Second, register the above process to Timelabel's Tick Events during Form.initializationComponent:
This.Timelabel1.Tick = New EventHandler (THIS.TICKHANDLER);
In this way, our test items have been completed and you can debug. The test program starts the interface as shown in Figure 6:
At the same time, in the output window of the debugger, there will be a new Debug record written every other second, and the content is the labelText property of Timelabel (ie the time displayed). This shows that our event is successful :)
Postscript: This article demonstrates how to create a simple composite control using Visual C # .NET through a simple DEMO. Everyone can further improve this example in accordance with the actual needs of the project or learning, such as adding a custom icon to the control. If there is any insufficiency in the program or other insights in the creation control, I also hope to contact me, my email address is: yanghada@vip.sina.com.