Gin*_*Gin 0 c# devexpress winforms
我有一个小问题ComboBoxEdit(DevExpress.XtraEditors)。我无法为SelectedIndex我的ComboBoxExit.
ComboBoxEdit combo = new ComboBoxEdit();
ComboBoxItemCollection coll = combo.Properties.Items;
coll.BeginUpdate();
try
{
coll.Add(new PersonInfo("Sven", "Petersen"));
coll.Add(new PersonInfo("Cheryl", "Saylor"));
coll.Add(new PersonInfo("Dirk", "Luchte"));
}
finally
{
coll.EndUpdate();
}
combo.SelectedIndex = -1; Comboboxedit1.Properties.Items.Add(combo);
Run Code Online (Sandbox Code Playgroud)
它不起作用,只是添加显示:

用这一行:
Comboboxedit1.Properties.Items.Add(combo);
您正在将 ComboBox 对象添加到其自身内部。ComboBoxEditToString() 方法返回您在屏幕截图中看到的名称。
所以,删除这一行。
您的代码取自官方DevExpress 文档(除了您应该删除的上面的行),并且工作正常:确实添加了项目。
但是,将SelectedIndex属性设置为 -1 不会选择任何内容,如文档所述:
出于演示目的,BaseListBoxControl.SelectedIndex 属性设置为 -1(默认情况下该属性设置为 -1)。这可确保组合框中当前没有选择任何项目。
你可以做 :
combo.SelectedIndex = 0; // Select Sven
Run Code Online (Sandbox Code Playgroud)
或者
combo.SelectedIndex = 1; // Select Cheryl
Run Code Online (Sandbox Code Playgroud)
或者
combo.SelectedIndex = 2; // Select Dirk
Run Code Online (Sandbox Code Playgroud)