Creating Classes & ActiveX Controls

Specifying default values for properties

The easiest way to specify a default value of a property is to create a constant called something like this:

Const m_def_Text = "Default Value" '// default value of Text property

Then, add the following code to your control (presuming you already have a m_Text variable, a m_def_Text constant and a Text property):

Private Sub UserControl_InitProperties()
    m_Text = m_def_Text '// set Text property to default value
End Sub

Then, you will also need to specify m_def_Text in the WriteProperties and ReadProperties procedures (see below).

If you want to have a property that defaults to the name of the control (such as the caption property of the label control), then you need to do things slightly differently. Firstly, you cannot set a constant for the default value, as you will not know the name of the control (ie Control1, Control2 etc) until run-time. Therefore, you declare a variable called m_def_propertyname instead, and set its value in the UserControl_Initialize procedure:

Private m_def_Text As String
Private Sub UserControl_Initialize()
    m_def_Text = UserControl.Name '// set its default value to the name of the control
End Sub

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is anticipated with distaste, performed with reluctance, and bragged about forever.” - Dan Kaminsky