c# - C# 将组合框的 SelectedIndex 设置为 -1 有什么作用?
问题描述
当我在处理我的一个项目时,我试图写入数据源组合框,然后将值写入组合框,如下所示:
//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;
}
但是该行在cbCompanyName.Text = StaticValues.billAddress.CompanyName;
没有向组合框写入任何文本的情况下运行,直到我将组合框的选定索引设置为-1。将组合框选定索引设置为 -1 会改变这一点,而不是将选定索引设置为 0?
解决方案
将 ComboBox 上的 SelectedIndex 设置为 -1 会取消选择(SelectedItem 为 NULL)。设置为 0 选择 Items 中的第一项
推荐阅读
- curl - Mendeley API - 无法使用客户端凭据授权流程进行授权
- r - 使用 R 计算 3d 核密度估计中给定范围的总概率
- azure - Azure 自动数据分类
- angular - 如何知道我在 Angular 8 中是否有垂直滚动条?
- python - 如何找到熊猫中第一行和后续行的百分比差异?
- python - 如何在 Dash 回调中更新元素样式
- visual-studio-code - Visual Studio 代码给出错误“拒绝访问”和“系统无法执行指定的程序”。交替
- python - 对同时包含字母和数字的 Python 列表进行排序
- javascript - 关于嵌套 try catch 语句和错误处理的问题
- python - python pandas合并数据框