ComboBox.SelectionChangeCommitted 事件

定义

当用户更改所选的项并在 ComboBox 中显示此更改时发生。

public:
 event EventHandler ^ SelectionChangeCommitted;
public event EventHandler SelectionChangeCommitted;
public event EventHandler? SelectionChangeCommitted;
member this.SelectionChangeCommitted : EventHandler 
Public Custom Event SelectionChangeCommitted As EventHandler 

事件类型

示例

下面的代码示例使用 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仅当用户更改组合框选择时,才会引发 事件,并且你可以为此事件创建处理程序,以便在用户更改列表中的选定项时为 ComboBox 提供特殊处理。 但是,根据 的配置方式 ComboBox 以及用户如何更改所选项, SelectionChangeCommitted 可能不会引发 事件。 或者,可以处理 SelectedIndexChanged,但请注意,无论索引是通过编程方式更改还是由用户更改,都会发生此事件。

有关处理事件的详细信息,请参阅 处理和引发事件

适用于