共用方式為


ComboBox.OnSelectionChangeCommitted(EventArgs) 方法

定義

引發 SelectionChangeCommitted 事件。

protected:
 virtual void OnSelectionChangeCommitted(EventArgs ^ e);
protected virtual void OnSelectionChangeCommitted (EventArgs e);
abstract member OnSelectionChangeCommitted : EventArgs -> unit
override this.OnSelectionChangeCommitted : EventArgs -> unit
Protected Overridable Sub OnSelectionChangeCommitted (e As EventArgs)

參數

e
EventArgs

包含事件資料的 EventArgs

範例

下列程式碼範例會使用 SelectionChangeCommitted 事件和 SelectionLength 屬性,根據使用者選取和認可的專案來變更文字方塊的長度。

void comboBox1_SelectionChangeCommitted( Object^ sender, EventArgs^ /*e*/ )
{
   ComboBox^ senderComboBox = dynamic_cast<ComboBox^>(sender);
   
   // Change the length of the text box depending on what the user has 
   // selected and committed using the SelectionLength property.
   if ( senderComboBox->SelectionLength > 0 )
   {
       textbox1->Width = 
           senderComboBox->SelectedItem->ToString()->Length * 
           ((int)this->textbox1->Font->SizeInPoints);
       textbox1->Text = senderComboBox->SelectedItem->ToString();				
   }
}
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{

    ComboBox senderComboBox = (ComboBox) sender;
  
    // Change the length of the text box depending on what the user has 
    // selected and committed using the SelectionLength property.
    if (senderComboBox.SelectionLength > 0)
    {
        textbox1.Width = 
            senderComboBox.SelectedItem.ToString().Length *
            ((int) this.textbox1.Font.SizeInPoints);
        textbox1.Text = senderComboBox.SelectedItem.ToString();
    }
}
Private Sub comboBox1_SelectionChangeCommitted(ByVal sender _
As Object, ByVal e As EventArgs) _
Handles comboBox1.SelectionChangeCommitted

    Dim senderComboBox As ComboBox = CType(sender, ComboBox)

    ' Change the length of the text box depending on what the user has 
    ' selected and committed using the SelectionLength property.
    If (senderComboBox.SelectionLength > 0) Then
        textbox1.Width = _
            senderComboBox.SelectedItem.ToString().Length() * _
            CType(Me.textbox1.Font.SizeInPoints, Integer)
        textbox1.Text = senderComboBox.SelectedItem.ToString()
    End If
End Sub

備註

SelectionChangeCommitted只有在使用者變更下拉式方塊選取專案,或設定 SelectedIndex 時,才會引發 。 不過,視 設定的方式 ComboBox 而定,以及使用者如何變更選取的專案, SelectionChangeCommitted 可能不會引發事件。 或者,您可以處理 SelectedIndexChanged ,但請注意,不論索引是以程式設計方式或使用者變更,都會發生此事件。

引發事件會透過委派叫用此事件處理常式。 如需詳細資訊,請參閱 處理和引發事件

OnSelectionChangeCommitted 方法也允許衍生類別處理事件,而不用附加委派。 這是在衍生類別中處理事件的慣用技巧。

給繼承者的注意事項

當在衍生類別中覆寫 OnSelectionChangeCommitted(EventArgs) 時,請確定呼叫基底類別的 OnSelectionChangeCommitted(EventArgs) 方法,使已註冊的委派能接收到事件。

適用於