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

Source: Internet
Author: User
Tags include insert reset
visual| Create | Control Step 3rd: Implementing Properties and Events
To implement the Status property, you first create an enumeration for the possible property values. Insert the following lines below the line starting with Inherits:

Public Enum TrafficLightStatus
statusred = 1
Statusyellow = 2
Statusgreen = 3
End Enum

This enumeration is exposed, meaning that the form that uses the control can access it.

Add the following three lines below these lines:

Dim mstatus as TrafficLightStatus = Trafficlightstatus.statusgreen
Dim Msngborderwidth as Single = 1.0!
Public Event statuschanged (ByVal newstatus as TrafficLightStatus)

The two variables in the first two rows can be used to store property values for the Status and BorderWidth properties, and the default values are set for these properties. The variable that holds the borderwidth must be a single type because it is the type required by the graphical statement used to draw the border. The exclamation point in the default value also indicates that it is a single type. The last line in this collection declares the StatusChanged event.

Now, we write code for the BorderWidth property. Insert the following line under the code area labeled Windows Form Designer generated code (generated by the Windows Forms Designer):

<defaultvalue (1.0!), _
Description ("width of border around traffic lights") > _
Public Property BorderWidth () as single
Get
Return Msngborderwidth
End Get
Set (ByVal Value as single)
If msngborderwidth <> Value Then
Msngborderwidth = Value
Me.invalidate ()
End If
End Set
End Property

The first two lines include properties that make the property better use the IDE. The DefaultValue attribute allows the property value to be reset to the default value in the Properties window (described later in the procedure). The Description attribute provides the text that is displayed at the bottom of the Properties window when the property is selected.

The DefaultValue feature also has one trick. If you place the TrafficLight control on a form and leave the default value for the BorderWidth property, the form designer will not generate a line of code that sets the property value. This makes it no different from other Windows forms controls. If you view the designer-generated code for a typical control, such as a TextBox, you will find that only lines of code that have properties set to Non-default values are included. We give the TrafficLight control the same capabilities.

Property Get is simple and straightforward. The property Set clause includes common logic in visual control properties. When you set a property, it is important to be able to repaint the control when the new property value changes the appearance of the control. Therefore, the SET clause is responsible for determining whether the new value passed is not the same as the existing value in the property. If the same, the operation is not performed. If different, the new value is accepted, and then the Invalidate method of the control is accessed. This method indicates that the control's viewable area has expired and the control needs to be redrawn.

The Status property is handled somewhat differently because it is an enumeration value. The DefaultValue attribute does not provide an automatic reset capability for enumerated properties. In this case, DefaultValue cannot tell the designer when to stop the code that sets the property value. Therefore, the DefaultValue attribute is not required in the implementation of the Status property. The following is the code for the Status property:

<description ("The Status (color) of the traffic light") > _
Public Property Status () as TrafficLightStatus
Get
Status = Mstatus
End Get
Set (ByVal Value as TrafficLightStatus)
If mstatus <> Value Then
Mstatus = Value
RaiseEvent statuschanged (Mstatus)
Me.invalidate ()
End If
End Set
End Property

Looks similar to the implementation of the BorderWidth property, except that when the Status property is changed, the StatusChanged event is triggered in addition to the forced redraw of the control.

To handle automatic resets of properties in the Properties window, we need to use a special method. Because our property is named Status, the Reset method must be named Resetstatus. The Reset method is simply the default value for the Recovery property. Here's the code:

Public Sub Resetstatus ()
Me.status = Trafficlightstatus.statusgreen
End Sub

To indicate when the designer needs to include one line of code to set the Status property, we need to include a method named Shouldserializestatus. This method returns a Boolean value of True when the property requires a line of code, or False. Here's the code:

Public Function Shouldserializestatus () as Boolean
If mstatus = Trafficlightstatus.statusgreen Then
Return False
Else
Return True
End If
End Function


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.