Gets or sets the index value of the selected item.
[Visual Basic] Public Property SelectedIndex As Integer [C#] public int SelectedIndex {get; set;} [C++] public: __property int get_SelectedIndex(); public: __property void set_SelectedIndex(int); [JScript] public function get SelectedIndex() : int; public function set SelectedIndex(int);
The zero-based index value of the selected item. The default value is-1.
Exception Type | Condition |
---|---|
ArgumentException | 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 up-down control. Collection items may be re-assigned 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.
Note If the user has entered an item in the up-down control, or no item is selected, the SelectedIndex value will be the defalut value,-1.
The following example instantiates 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 up-down control. The code assumes that a Label, TextBox, CheckBox and a Button have been instantiated on a form. You can enter a string in the text box and add it to the System.WinForms.DomainUpDownItems.Items collection when the button is clicked. By clicking the check box, you can toggle the Sorted property and observe the the difference in the collection of items in the up-down control.
[Visual Basic]
' Instantiate the DomainUpDown control. Private DomainUpDown1 As System.WinForms.DomainUpDown Private myCounter as Integer Private Sub MySub() ' Initialize the DomainUpDown control. Form1.DomainUpDown1 = New System.WinForms.DomainUpDown End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) ' Add the text box contents to the DomainUpDown control and ' and its initial location in the collection. DomainUpDown1.Items.Add(Trim(TextBox1.Text) & " - " & myCounter) ' Increment the counter variable. myCounter = myCounter + 1 'Clear the TextBox. TextBox1.Text = "" End Sub Private Sub CheckBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) ' If Sorted is set to true, set it to false; otherwise set it to true. If DomainUpDown1.Sorted = True Then DomainUpDown1.Sorted = False Else DomainUpDown1.Sorted = True End If End Sub Private Sub DomainUpDown1_SelectedItemChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) ' Display the SelectedIndex & SelectedItem property values in the label. Label1.Text = "SelectedIndex: " & DomainUpDown1.SelectedIndex & vbcrlf & _ "SelectedItem: " & DomainUpDown1.SelectedItem End Sub
DomainUpDown Class | DomainUpDown Members | System.WinForms Namespace | SelectedItem