Closer Look: Adding Properties with Named Values

In this lesson, you will learn how to add a property that contains a list of values to your user control.

The properties that you added to your user control to this point all took string values, but properties can be of many different types. Sometimes you will want to provide a list of pre-set values to select from, like the SizeMode property of the PictureBox control that you set in an earlier lesson.

Say, for example, that you want to create a property for the NamesControl that enables you to choose how the FullName label displays the name. You will need a list of the values to choose from: first name first, last name first, first and last name only, and so forth.

In Visual Basic you can create an enumeration that contains the values that you want. "Enumeration" is just a fancy word for "numbered list"; Visual Basic stores the numbers for you so that you can refer to the values by name. You declare an enumeration by using the Enum keyword, as in the following example.

Public Enum Display
End Enum

Once you have created an enumeration, you can use it just like any other data type. To add a property that displays a list of values, you first declare a variable of the same data type as Enum, and then declare a property of the same data type. At design time, a list of the values that are contained in the enumeration will appear in the Properties window.

To add a property that displays a list of values

  1. Open the NamesUserControl project that you created in the previous lesson. If you did not save it, you will first have to go back to the previous lesson, Adding Code to Your User Control, and complete the procedures in that lesson.

  2. In Solution Explorer, select NamesControl.vb, and then on the View menu click Code.

  3. In the Code Editor, add the following code to create an enumeration.

    Public Enum Display
    End Enum
  4. Add the following code to add a new property.

    Private DisplayStyleList As Display 
    Property DisplayStyle() As Display
            Return DisplayStyleList
        End Get
        Set(ByVal value As Display)
            DisplayStyleList = value
        End Set
    End Property
  5. Delete the existing code in the FirstName_TextChanged event handler, and replace it with the following code.

    Select Case DisplayStyleList
        Case Display.FirstLast
            FullName.Text = FirstName.Text & " " & LastName.Text
        Case Display.FirstMiddleLast
            FullName.Text = FirstName.Text & " " & MiddleName.Text & " " & LastName.Text
        Case Display.LastFirst
            FullName.Text = LastName.Text & ", " & FirstName.Text
        Case Display.LastFirstMiddle
            FullName.Text = LastName.Text & ", " & FirstName.Text & " " & MiddleName.Text
    End Select
  6. Press F5 to run the program. Enter your first, last, and middle names in the three text boxes.

  7. In the UserControl TestContainer,scroll to the bottom of the Properties grid and select the DisplayStyle property. Select a different value and then change the text in one of the text boxes see how it affects the label.

  8. On the File menu, click Save All to save your work.

In this lesson, you learned how to use an enumeration to create a property that has a list of values. In the next lesson, you will learn how to test your user control by putting it on a form.

Next Lesson: Testing Your User Control