Create a Visual Basic. NET Control from scratch (eight)

Source: Internet
Author: User
Tags reset visual studio
visual| Create | Control step 7th: Build and Test controls
The design of the TrafficLight control is now complete. Select Build | Build MyControls (Generate | Generate MyControls) to create the final control library.

To test the control, we need a Windows forms project. You can do this in other solutions, but it is easier to execute in the solution that is used to develop the control. Choose File | from the menu ADD Project | New Project (File | Add Items | New project). Select the Windows application (Windows application) project type to name the project TestTrafficLight. Click OK to start the Windows application required for the test.

The TrafficLight control must be placed in the toolbox before it can be dragged and dropped into a blank form 1 of the test application. Right-click the Windows Forms tab in the Toolbox and select Customize Toolbox (custom Toolbox). Select the. NET Framework components (. NET Framework Components) tab, and then click the Browse button. Browse to the location of your MyControls project, and then go to the/bin directory for the project. Select the MyControls.dll component and click OK (OK). The dialog box should now look like Figure 2.



Figure 2: In the Customize Toolbox (Custom Toolbox) dialog box, the TrafficLight control is selected.

You can see a check mark next to the TrafficLight control. Click the OK button, and on the Windows Forms tab of the Toolbox, the TrafficLight control appears at the bottom of the list of controls. Figure 3 shows the toolbox at the bottom as the TrafficLight control.



Figure 3: The TrafficLight control at the bottom of the toolbox

You can now drag and drop the TrafficLight control into a blank form 1 in TestTrafficLight. By default, it is named TrafficLight1. You can resize the control, reset the properties of the control, including the Status property, which has a Drop-down menu that contains three possible values for the property. Note that when you resize a control or change its properties, the control is automatically refreshed in the designer.

To restore the default value for a property, change the Status property to Statusred. Then, right-click the Status property in the Properties window and choose Reset (reset) as shown in Figure 4. This property changes back to Statusgreen. If you set the BorderWidth property to a value other than 1, you can also restore its default value using the same method.



Figure the Reset (reset) option for the Status property in the 4:properties (properties) window. Note the description of the status (status) attribute at the bottom of the window.

You can also insert a statuschanged event for a control, if you want. You can then use the following line of code in the event to view the changed status:

MsgBox ("New status" & Newstatus.tostring)

To test the control in an operation, you need to start the TestTrafficLight project. At this point, it is not the startup project for the solution, so you need to resolve it. In Solution Explorer (Solution Explorer), right-click Solution (solution) name-the first line in Solution Explorer (Solution Explorer). Select Properties, and then change the single startup Project setting from MyControls to TestTrafficLight, and then click OK.

Press the F5 key to start the project. The form with the TrafficLight control is displayed. Test controls: Press the different lights to see if they are lit. You can also test the BorderWidth property to try to set the Status property of the lamp in code.

Summary
Although TrafficLight is a simple control (although it was once used by developers to use it in real projects), it shows all the principles needed to develop complex controls, including:

Add a property to the control.
Use default values and descriptions to reconcile properties with the Visual Studio IDE.
Inserts logic into the Paint event to draw the control.
Use GDI + in the drawing logic.
Sets a bitmap for the control to display in the toolbox.
The key to creating complex controls is to familiarize yourself with GDI + 's drawing capabilities. If you understand the principle of trafficlight drawing borders and colored circles, you have a good starting point. The point is that with Visual Basic. NET, even lazy programmers like me can create advanced Windows forms.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.