Home > Articles > Programming > User Interface (UI)

This chapter is from the book

Using the DomainUpDown Control

The DomainUpDown control is similar to the NumericUpDown control, the difference being that the DomainUpDown can display a list of strings, whereas the NumericUpDown control can display only a list of integers. The DomainUpDown control displays a list of options in a very space-efficient way because it can display only one option at a time to the user.

The control appears as a textbox with a pair of up and down arrows on the right side of the control. The user can move through the list in three different ways:

  • Clicking the up arrow moves you up the list of options; the down arrow moves you down the list.n If the ReadOnly property is set to false, the user can type the name of the item in the list.

  • On most Pocket PC devices, there is a button that can be used for navigating the DomainUpDown control. Pressing this button up and down moves you up and down the list items, respectively.

If ReadOnly is false, the user can enter text into the control that does not match an item in the list. The Text property will return the current text in the control. This means that you may need to do some input validation if ReadOnly is set to false. Because no input validation is performed by the DomainUpDown control, entering text into the control will not select a matching item. Setting ReadOnly to true restricts the user to selecting only items in the list, thus eliminating the need for input validation.

The DomainUpDown control exposes the Items property that represents the list of items in the control. You can add items to the list by using the Add method exposed on the Items property. The following code demonstrates how to add three items to a DomainUpDown control:

C#
DomainUpDown dud = new DomainUpDown();
dud.Items("TX");
dud.Items("LA");
dud.Items("WA");
VB
Dim dud as DomainUpDown
dud.Items("TX")
dud.Items("LA")
dud.Items("WA")

You can also add items to the DomainUpDown control at design time. First, select the control in the Form Designer. Then, in the Properties windows, click the ellipsis next to the Items property. This brings up the String Collection Editor dialog box (see Figure 3.9). You can enter the items there, one item per line.

The DomainUpDown exposes the two properties that allow you to determine the current text in the control. The Text property is the string that is currently being displayed in the control. The return value is not necessarily an item in the list. The SelectedIndex property is the index of the item in the list that is currently being display to the user. You can use this property in conjunction with the Items property to get the item's string value. When the Text property is changed, a TextChanged event is fired, and when the SelectedIndex property is changed, a SelectedIndexChanged event is fired. You can handle these events if your application needs to perform some operation when the item that is displayed in the control changes. The SelectedIndexChanged event is fired only when the index is changed through code or via the up and down arrows. The event will not be fired when a user types input into the control. The following code snippet demonstrates how to use handle both events:

C#
private void domainUpDown1_SelectedItemChanged(object sender,
    System.EventArgs e) {
 int selNdx = this.domainUpDown1.SelectedIndex;
 string selStr = this.domainUpDown1.Items[selNdx].ToString();
 MessageBox.Show("You selected " + selStr);
}

private void domainUpDown1_TextChanged(object sender, System.EventArgs e) {
 string selStr = this.domainUpDown1.Text;
 MessageBox.Show("You selected " + selStr);
}
VB
Private Sub _
DomainUpDown1_SelectedItemChanged(ByVal sender As Object,
    ByVal e As System.EventArgs)_ Handles DomainUpDown1.SelectedItemChanged
 Dim selNdx As Int32
 Dim selStr As String
 selNdx = DomainUpDown1.SelectedIndex
 selStr = domainUpDown1.Items[selNdx].ToString()
End Sub

Private Sub
DomainUpDown1_TextChanged(ByVal sender As Object, 
    ByVal e As System.EventArgs) _Handles DomainUpDown1.TextChanged
 Dim selStr As String
 selStr = DomainUpDown1.Text
 MessageBox.Show("You selected " & selStr)
End Sub
  • + Share This
  • 🔖 Save To Your Account