SelectedIndex Property

DomainUpDown.SelectedIndex Property

 

Gets or sets the index value of the selected item.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

[BrowsableAttribute(false)]
public int SelectedIndex { get; set; }

Property Value

Type: System.Int32

The zero-based index value of the selected item. The default value is -1.

Exception Condition
ArgumentOutOfRangeException

The assigned value is less than the default, -1.

-or-

The assigned value is greater than the Items count.

The SelectedIndex property holds the index value of the item in the collection that is currently selected in the spin box (also known as an up-down control). Collection items can be reassigned new index values if the Sorted property has been changed from false to true. As the collection is re-sorted alphabetically, the items will be assigned a new index value.

System_CAPS_noteNote

If the user has entered an item in the spin box, or if no item has been selected, the SelectedIndex value will be the default value, -1.

The following code example creates and initializes a DomainUpDown control. The example allows you to set some of its properties and create a collection of strings for display in the spin box. The code assumes that a TextBox, CheckBox, and Button have been instantiated on a form. The example also assumes that you have a member variable at the class level declared as a 32-bit signed integer named myCounter. You can enter a string in the text box and add it to the Items collection when the button is clicked. By clicking the check box, you can toggle the Sorted property and observe the difference in the collection of items in the spin box.

protected DomainUpDown domainUpDown1;

private void MySub()
 {
    // Create and initialize the DomainUpDown control.
    domainUpDown1 = new System.Windows.Forms.DomainUpDown();

    // Add the DomainUpDown control to the form.
    Controls.Add(domainUpDown1);
 }

 private void button1_Click(System.Object sender, 
                           System.EventArgs e)
 {   
    // Add the text box contents and initial location in the collection
    // to the DomainUpDown control.
    domainUpDown1.Items.Add((textBox1.Text.Trim()) + " - " + myCounter);

    // Increment the counter variable.
    myCounter = myCounter + 1;

    // Clear the TextBox.
    textBox1.Text = "";
 }

 private void checkBox1_Click(System.Object sender, 
                             System.EventArgs e)
 {
    // If Sorted is set to true, set it to false; 
    // otherwise set it to true.
    if (domainUpDown1.Sorted)
    {
       domainUpDown1.Sorted = false;
    }
    else
    {
       domainUpDown1.Sorted = true;
    }
 }

 private void domainUpDown1_SelectedItemChanged(System.Object sender, 
                                               System.EventArgs e)
 {
    // Display the SelectedIndex and SelectedItem property values in a MessageBox.
    MessageBox.Show("SelectedIndex: " + domainUpDown1.SelectedIndex.ToString() 
       + "\n" + "SelectedItem: " + domainUpDown1.SelectedItem.ToString());
 }

.NET Framework
Available since 1.1
Return to top
Show:
© 2016 Microsoft