C# Combobox 和 TabControl 的问题
在此处输入代码
我在表单上有一个 TabControl,在 TabPage 中有 ComboBox。
当表单 OnLoad 时,我填充组合框中的 ListItems 并尝试将默认值设置为 string.Empty。
但是, ComboBox.SelectedText = string.Empty 仅适用于第一个 TabPage。其他组合框忽略该命令并采用默认值作为列表中的第一项。为什么会这样呢?我怎样才能克服它?
组合框都是通过这个函数设置的
public static void PrepareComboBox(ComboBox combobox, FieldValueList list)
{
combobox.DropDownStyle = ComboBoxStyle.DropDown;
combobox.AutoCompleteSource = AutoCompleteSource.ListItems;
combobox.AutoCompleteMode = AutoCompleteMode.Suggest;
combobox.DataSource = list.DataSource;
combobox.DisplayMember = list.DisplayMember;
combobox.ValueMember = list.ValueMember;
combobox.Text = string.Empty;
combobox.SelectedText = string.Empty;
}
enter code here
I have a TabControl on a Form and in the TabPages there are ComboBoxes.
When the form OnLoad, I populate the ListItems in the ComboBoxes and the attempt to set default values to string.Empty.
However, the ComboBox.SelectedText = string.Empty only works for the first TabPage. The other ComboBoxes ignore the command and take the default value as the first item in the list. Why is this so? How can I overcome it?
The ComboBoxes are all set up by this function
public static void PrepareComboBox(ComboBox combobox, FieldValueList list)
{
combobox.DropDownStyle = ComboBoxStyle.DropDown;
combobox.AutoCompleteSource = AutoCompleteSource.ListItems;
combobox.AutoCompleteMode = AutoCompleteMode.Suggest;
combobox.DataSource = list.DataSource;
combobox.DisplayMember = list.DisplayMember;
combobox.ValueMember = list.ValueMember;
combobox.Text = string.Empty;
combobox.SelectedText = string.Empty;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现原因可能是组合框在至少显示一次之前不是“活动的”。可以看到,第一次选择TabPage时,加载时间稍长。我想它是第一次创建/初始化子控件。
为此,我在修改值属性之前调用 tabControl.SelectTab() 并且它起作用了......尽管感觉像是黑客。
I found that the reason could be that the ComboBox is not "active" until they are at least shown once. You can see that when the TabPage is selected for the first time, it takes a slightly longer time to load. I suppose it is creating/initialising the child controls for the first time.
For that, I call tabControl.SelectTab() before modifying the value properties and it worked... although it feels like a hack.
这是由于数据绑定。除了在数据源中添加空/虚拟条目前缀之外,您对此无能为力。
This is due to databinding. Not much you can do about it, except prefix the datasource with an empty/dummy entry.