C# 将组合框的 SelectedIndex 设置为 -1 有什么作用?

问题描述

当我在我的一个项目中工作时,我试图写入数据源组合框,然后将一个值写入组合框,如下所示:

StationObject prevIoUsstation = null;
foreach (var currentStation in busLinestations.OrderBy(st => st.AscendingKeyProperty))
{
  currentStation.BusstationKeyPrevIoUs = prevIoUsstation.Code;
  if (prevIoUsstation != null) prevIoUsstation.BusstationKeyNext = currentStation.Code;
  prevIoUsstation = currentStation;
}

然而,//Create list for comboBox List<string> companyList= new List<string>() { "","Company1","Company2" }; //Datsource list to comboBox cbCompanyName.DataSource = companyList; //If form is set to import data and the billing address is not null if (importAddress && StaticValues.billAddress != null) { //Fill all fields with data from Static Values class cbCompanyName.Text = StaticValues.billAddress.CompanyName; cbCountry.Text = StaticValues.billAddress.Country; } else { //Set country to US cbCountry.Selectedindex = 0; } 行在没有向组合框写入任何文本的情况下运行,直到我将组合框的选定索引设置为 -1。将组合框选定的索引设置为 -1 会如何改变与将选定的索引设置为 0 的情况?

解决方法

将 ComboBox 上的 SelectedIndex 设置为 -1 会取消选择(SelectedItem 为 NULL)。设置为 0 选择 Items 中的第一项

,

Combobox 需要知道我的值和显示成员是什么, 仅提供数据源是不够的。

我认为你可以这样使用或

  //  comboBox.DisplayMember = "Text";
  //  comboBox.ValueMember = "Value";
int =0;
companyList.forEach(x=> comboBox.Items.Add(new { Text = x.toString(),Value =i++  })); 
comboBox1.SelectedIndex = 0;

你可以看看这篇文章

similar question and answers

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...