开发者

C# Combobox and TabControl woes

开发者 https://www.devze.com 2023-02-01 02:01 出处:网络
enter code hereI 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

enter code hereI 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 = AutoCompleteSourc开发者_开发技巧e.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;

}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消