Вопрос

I am working on a Windows Form that has multiple comboboxes. Depending on what is chosen in the first combobox determines what items are filled into the second combobox. The issue I am running into is if I choose ChoiceA in ComboBox1, ComboBox2 is clear()ed, then is filled with ChoiceX, ChoiceY, and ChoiceZ. I then choose ChoiceB in ComboBox1, ComboBox2 is clear()ed, but there are no choices to add to ComboBox2, so it should remain empty. The issue is, after choosing ChoiceB, there's a big white box with three empty slots in ComboBox2. So, basically, however many items are cleared N, that's how many empty slots show up after choosing ChoiceB.

This might be a tad confusing, I hope I explained it well enough.

-- EDIT Adding Code, hope it helps clear things up. BTW, mainItemInfo is another "viewmodel" type class. It interfaces back into the form to make updates.

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();
    }
Это было полезно?

Решение 3

I was able to fix the extra space. I changed the Add and Clear methods to:

public void AddTocmbModel(DropDownItem model)
    {
        cmbModel.Items.Add(model);
        cmbModel.DropDownHeight = cmbModel.ItemHeight * (cmbModel.Items.Count + 1);
    }

    public void ClearcmbModel()
    {
        cmbModel.Items.Clear();
        cmbModel.DropDownHeight = cmbModel.ItemHeight;
    }

Другие советы

Simply, you can add an item then clear the combobox again:

   cmbBrand.Items.Clear();
   cmbBrand.Items.Add(DBNull.Value);
   cmbBrand.Items.Clear();

You should able to set the datasource of listbox2 to null to clear it, then set it again with the new data.

So, in pseudo-code, something like:

ItemSelectedInListBox1()
{
    List futureListbox2Items = LoadOptionsBaseOnSelectedItem(item)

    Listbox2.Datasource = null
    Listbox2.Datasource = futureListBox2Items
}

That should refresh the list of items displayed in Listbox2 with no white spaces.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top