gpt4 book ai didi

c# - 如何设置组合框索引?

转载 作者:行者123 更新时间:2023-11-30 22:59:15 25 4
gpt4 key购买 nike

“我知道问题标题可能会重复,但没有找到我正在寻找的解决方案。”

一个包含大约 50 个文本框和组合框组合控件的组框。我必须为它们设置值并且不想写 50 行来为每个控件设置值,所以我想出了下面的代码。但这在组合框的情况下不起作用。或者,如果你们能提出更好的建议,那就太好了。

if(controlsInGroupBox == editStep.Count)
{
int i = 0;
foreach (Control ctr in universalGroupBoxObject.Controls)
{
if (ctr is TextBox)
{
ctr.Text = editStep[i];
}
if (ctr is ComboBox)
{
//ctr.SelectedIndex = cntrlObjListMain.comboBoxLocation.FindStringExact(editStep[i]);
//ctr.SelectedIndex is not working
}
i++;
}
}

最佳答案

如果您将数据绑定(bind)到组合框,则默认情况下会选择第一项,而不必选择一个。

例如

List<string> items = new List<string>() { "aa", "bb", "cc", "dd" };

combobox1.DataSource = items;

只要您的列表中有项目,SelectedIndex 就应该有效。

ctr.SelectedIndex = ctr.Items.Count > 0 ? 0 : -1;

如果有任何项目,上面将选择一个项目,否则不会选择任何东西。

关于c# - 如何设置组合框索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52338286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com