2017-10-19 72 views
0

我有一个ComboBox它有一个DataSource这是一个List<CustomClass>。现在在这个自定义类中,有一个属性被用作DisplayMemberValueMember价值成员和DropDownStyle.DropDown

现在,这一切工作得很好,当我使用DropDownStyle.DropDownList。但如果将类型更改为DropDownStyle.DropDown并在ComboBox中输入自定义文本,DisplayMemberValueMember会发生什么变化?那么DisplayMemberValueMember会是什么呢?

简单的例子

// Create empty list of type CustomClass 
List<CustomClass> comboBoxDataSource = new List<CustomClass>(); 

// Create a property for the selected item 
CustomClass CurrentSelectedItem { get; set; } 

// Use external function to fill the list with some data 
LoadDataAndInsertIntoList(comboBoxDataSource); 

// Set datasource, display- and value-member 
cboComboBox.DataSource = comboBoxDataSource; 
cboComboBox.DisplayMember = "DisplayName"; 
cboComboBox.ValueMember = "Value"; 

private void cboComboBox_SelectionChangeCommitted(object sender, EventArgs e) 
{ 
    // What happens here, if using DropDownStyle.DropDown and 
    // a custom text is entered, instead of the predefined options? 
    CurrentSelectedItem = (CustomClass)cboComboBox.SelectedItem; 
} 
+0

也许你需要检查下拉或下拉列表,然后只使用不同的转换,如果是下拉也许((CustomClass)cboComboBox.SelectedItem).displaymember? –

回答

1

这里是我输入:

  • 如果你改变了文本,你不能触发 SelectionChangeCommitted事件处理程序。
  • 为了触发SelectionChangeCommitted,您需要选择列出数据源的下拉图标,其中不包含您在组合框中键入的内容。
  • 您的数据源将保持不变,因为您的数据源,显示成员或值成员没有修改。
+0

如果我想将我的组合框用作排序自动填充文本框,应该使用哪个事件? – Noceo

+1

使用KeyPress代替,然后从那里更改数据源。 –