Skip to main content
deleted 1 characters in body
Source Link
Nikita B
  • 13k
  • 1
  • 25
  • 57

You can add new abstraction, instead of using a bunch of lists. For example:

class Category
{
     public string Name { get; private set; } //or other Id. Some Enum probably?
     
     public IList<Product> Products { get; private set; }

     public Category(string name)
     {
         Name = name;
         ProducntsProducts = new List<Products>();
     }

     public override string ToString()
     {
         return Name;
     }
}

Then you can simply populate your comboBox1 with Category objects, and handle selection event like this:

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        listBox1.Items.Clear();
        var category = (Category)comboBox1.SelectedItem;
        if (category == null) return;
        //cant you AddRange or assign? I'm not very familiar with winforms
        foreach (Product item in category.Products)
        {
            listBox1.Items.Add(item.ProductName);
        }
    }

As for your first question: you can override ToString method of your Product class to return Name+Price the same way it is done for Category. Then you can simply call listBox1.Items.Add(item);

You can add new abstraction, instead of using a bunch of lists. For example:

class Category
{
     public string Name { get; private set; } //or other Id. Some Enum probably?
     
     public IList<Product> Products { get; private set; }

     public Category(string name)
     {
         Name = name;
         Producnts = new List<Products>();
     }

     public override string ToString()
     {
         return Name;
     }
}

Then you can simply populate your comboBox1 with Category objects, and handle selection event like this:

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        listBox1.Items.Clear();
        var category = (Category)comboBox1.SelectedItem;
        if (category == null) return;
        //cant you AddRange or assign? I'm not very familiar with winforms
        foreach (Product item in category.Products)
        {
            listBox1.Items.Add(item.ProductName);
        }
    }

As for your first question: you can override ToString method of your Product class to return Name+Price the same way it is done for Category. Then you can simply call listBox1.Items.Add(item);

You can add new abstraction, instead of using a bunch of lists. For example:

class Category
{
     public string Name { get; private set; } //or other Id. Some Enum probably?
     
     public IList<Product> Products { get; private set; }

     public Category(string name)
     {
         Name = name;
         Products = new List<Products>();
     }

     public override string ToString()
     {
         return Name;
     }
}

Then you can simply populate your comboBox1 with Category objects, and handle selection event like this:

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        listBox1.Items.Clear();
        var category = (Category)comboBox1.SelectedItem;
        if (category == null) return;
        //cant you AddRange or assign? I'm not very familiar with winforms
        foreach (Product item in category.Products)
        {
            listBox1.Items.Add(item.ProductName);
        }
    }

As for your first question: you can override ToString method of your Product class to return Name+Price the same way it is done for Category. Then you can simply call listBox1.Items.Add(item);

Source Link
Nikita B
  • 13k
  • 1
  • 25
  • 57

You can add new abstraction, instead of using a bunch of lists. For example:

class Category
{
     public string Name { get; private set; } //or other Id. Some Enum probably?
     
     public IList<Product> Products { get; private set; }

     public Category(string name)
     {
         Name = name;
         Producnts = new List<Products>();
     }

     public override string ToString()
     {
         return Name;
     }
}

Then you can simply populate your comboBox1 with Category objects, and handle selection event like this:

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        listBox1.Items.Clear();
        var category = (Category)comboBox1.SelectedItem;
        if (category == null) return;
        //cant you AddRange or assign? I'm not very familiar with winforms
        foreach (Product item in category.Products)
        {
            listBox1.Items.Add(item.ProductName);
        }
    }

As for your first question: you can override ToString method of your Product class to return Name+Price the same way it is done for Category. Then you can simply call listBox1.Items.Add(item);