Share via


DomainUpDown.SelectedIndex Proprietà

Definizione

Ottiene o imposta il valore di indice dell'elemento selezionato.

public:
 property int SelectedIndex { int get(); void set(int value); };
[System.ComponentModel.Browsable(false)]
public int SelectedIndex { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectedIndex : int with get, set
Public Property SelectedIndex As Integer

Valore della proprietà

Valore dell'indice in base zero dell'elemento specificato. Il valore predefinito è -1.

Attributi

Eccezioni

Il valore assegnato è minore del valore predefinito -1.

-oppure-

Il valore assegnato è maggiore del numero di Items.

Esempio

Nell'esempio di codice seguente viene creato e inizializzato un DomainUpDown controllo . L'esempio consente di impostare alcune delle relative proprietà e di creare una raccolta di stringhe da visualizzare nella casella di selezione. Il codice presuppone che un'istanza TextBoxdi , CheckBoxe Button sia stata creata un'istanza in un modulo. L'esempio presuppone anche che sia presente una variabile membro a livello di classe dichiarata come intero con segno a 32 bit denominato myCounter. È possibile immettere una stringa nella casella di testo e aggiungerla alla Items raccolta quando si fa clic sul pulsante. Facendo clic sulla casella di controllo, è possibile attivare o disattivare la Sorted proprietà e osservare la differenza nella raccolta di elementi nella casella di selezione.

protected:
   DomainUpDown^ domainUpDown1;

private:
   void MySub()
   {
      // Create and initialize the DomainUpDown control.
      domainUpDown1 = gcnew System::Windows::Forms::DomainUpDown;
      
      // Add the DomainUpDown control to the form.
      Controls->Add( domainUpDown1 );
   }

   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( String::Concat(
         (textBox1->Text->Trim()), " - ", myCounter.ToString() ) );
      
      // Increment the counter variable.
      myCounter = myCounter + 1;
      
      // Clear the TextBox.
      textBox1->Text = "";
   }

   void checkBox1_Click( Object^ sender, 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;
      }
   }

   void domainUpDown1_SelectedItemChanged( Object^ sender, EventArgs^ e )
   {
      // Display the SelectedIndex and SelectedItem property values in a MessageBox.
      MessageBox::Show( String::Concat( "SelectedIndex: ",
      domainUpDown1->SelectedIndex.ToString(), "\n", "SelectedItem: ",
      domainUpDown1->SelectedItem->ToString() ) );
   }
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());
 }
Protected domainUpDown1 As DomainUpDown


Private Sub MySub()
    ' Create and initialize the DomainUpDown control.
    domainUpDown1 = New System.Windows.Forms.DomainUpDown()
    
    ' Add the DomainUpDown control to the form.
    Controls.Add(domainUpDown1)
End Sub


Private Sub button1_Click(sender As System.Object, e As System.EventArgs)
    ' 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 = ""
End Sub


Private Sub checkBox1_Click(sender As System.Object, e As System.EventArgs)
    ' If Sorted is set to true, set it to false; 
    ' otherwise set it to true.
    If domainUpDown1.Sorted Then
        domainUpDown1.Sorted = False
    Else
        domainUpDown1.Sorted = True
    End If
End Sub


Private Sub domainUpDown1_SelectedItemChanged _
    (sender As System.Object, e As System.EventArgs)
    
    ' Display the SelectedIndex and SelectedItem property values in a MessageBox.
    MessageBox.Show(("SelectedIndex: " & domainUpDown1.SelectedIndex.ToString() & _
        ControlChars.Cr & "SelectedItem: " & domainUpDown1.SelectedItem.ToString()))
End Sub

Commenti

La SelectedIndex proprietà contiene il valore di indice dell'elemento nell'insieme attualmente selezionato nella casella di selezione ,noto anche come controllo di scorrimento. Gli elementi della raccolta possono essere riassegnati nuovi valori di indice se la Sorted proprietà è stata modificata da false a true. Quando la raccolta viene riordinata in ordine alfabetico, agli elementi verrà assegnato un nuovo valore di indice.

Nota

Se l'utente ha immesso un elemento nella casella di selezione o se non è stato selezionato alcun elemento, il SelectedIndex valore sarà il valore predefinito, -1.

Si applica a

Vedi anche