我正在使用具有多個組合框的Windows窗體。根據第一個組合框中選擇的內容決定哪些項目被填充到第二個組合框中。我遇到的問題是,如果我在ComboBox1中選擇了ChoiceA,ComboBox2是clear()ed,然後充滿了ChoiceX,ChoiceY和ChoiceZ。然後我在ComboBox1中選擇ChoiceB,ComboBox2是clear()ed,但是沒有選擇要添加到ComboBox2,所以它應該保持爲空。問題是,在選擇了ChoiceB之後,在ComboBox2中有一個大的白盒子,有三個空插槽。因此,基本上,無論多少項都被清除N,這就是選擇ChoiceB後出現多少個空插槽。Windows Form Combobox.Items.Clear()留下空插槽
這可能有點混亂,我希望我解釋得不錯。
- 編輯添加代碼,希望它有助於清理事情。順便說一句,mainItemInfo是另一個「viewmodel」類型的類。它重新接入表單進行更新。
private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownItem item = (DropDownItem)cmbType.SelectedItem;
if (!String.IsNullOrWhiteSpace(item.Text))
{
cmbBrand.Enabled = true;
btnAddBrand.Enabled = true;
mainItemInfo.FillBrands(new Dictionary<string, string> { { "Type", item.Text } });
mainItemInfo.SyncBrands(this);
}
}
public void FillBrands(Dictionary<string, string> columnsWhere)
{
// Clear list
Brands.Clear();
// Get data
StorageData storage = new StorageData(File.ReadAllLines(ItemsFilePath));
// Fill Brands
foreach (string type in storage.GetDistinctWhere(columnsWhere, "Brand"))
{
Brands.Add(type, new DropDownItem(type, type));
}
}
public void SyncBrands(IPopupItemInfo form)
{
form.ClearcmbBrand();
var brands = from brand in Brands.Keys
orderby Brands[brand].Text ascending
select brand;
foreach (var brand in brands)
{
form.AddTocmbBrand(Brands[brand]);
}
}
public void AddTocmbBrand(DropDownItem brand)
{
cmbBrand.Items.Add(brand);
}
public void ClearcmbBrand()
{
cmbBrand.Items.Clear();
}
我承認我被迷惑,在事件處理程序添加代碼組合1對你的問題。 –