C# Reading from Array to Listbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dan Effets
    New Member
    • Oct 2011
    • 17

    C# Reading from Array to Listbox

    I created a Menue Sheet with combobox as require by the book.Select Drink ,App,Meal,Desse rt , then display the total of the meal.
    Each Topic item is in array
    Code:
    Double[] Dessert = {5.95,5.95,6.00,5.00,6.60,6.95 };
    int Indx = DescComboBox.SelectedIndex;
    SubTotal += Dessert[Indx];
    Calculate();
    All works well, but I was wanting to add Label , show each item select (like a bill).
  • arie
    New Member
    • Sep 2011
    • 64

    #2
    You could do something like this (it's only an idea, I don't know if you like it or not):

    Code:
    public class MyMenuItem
    {
    public string Price {get; set;}
    public string Type {get; set;} // Drink, Dessert, Meal...
    public string Name {get; set;}
    public MyDessert (string type, string name, string price)
      {
       Price = price;
       Type = type;
       Name = name;
      }
    public string GetStringToShow()
      {
        return Name + " " + Price;
      }
    }
    then, in your Main (or wherever you want it) you make a list:

    Code:
    List<MyMenuItem> Menu = new List<MyMenuItem>();
    ...and add the items to it (e.g. desserts):

    Code:
    for (int i =0; i<Desserts.Length;i++)
    {
      Menu.Add(new MyMenuItem("Dessert", Desserts[i], DessertNames[i]));
    }
    then, if you want to add items to your, for example, DessertsListbox :

    Code:
    foreach (MyMenuItem item in Menu)
    {
      if(item.Type == "Dessert")DessertsListBox.Add(item.GetStringToShow());
    }
    This way you'll have one complex structure in which your items are organized, it's easier to modify this way, and you can use it in many places.

    I didn't compile/run the code so there may be some errors.

    Comment

    • Dan Effets
      New Member
      • Oct 2011
      • 17

      #3
      I am going to try this later today ...

      Thank you
      Dan

      Comment

      • chilljag
        New Member
        • Oct 2011
        • 1

        #4
        Nice one... very informative..

        Originally posted by arie
        You could do something like this (it's only an idea, I don't know if you like it or not):

        Code:
        public class MyMenuItem
        {
        public string Price {get; set;}
        public string Type {get; set;} // Drink, Dessert, Meal...
        public string Name {get; set;}
        public MyDessert (string type, string name, string price)
          {
           Price = price;
           Type = type;
           Name = name;
          }
        public string GetStringToShow()
          {
            return Name + " " + Price;
          }
        }
        then, in your Main (or wherever you want it) you make a list:

        Code:
        List<MyMenuItem> Menu = new List<MyMenuItem>();
        ...and add the items to it (e.g. desserts):

        Code:
        for (int i =0; i<Desserts.Length;i++)
        {
          Menu.Add(new MyMenuItem("Dessert", Desserts[i], DessertNames[i]));
        }
        then, if you want to add items to your, for example, DessertsListbox :

        Code:
        foreach (MyMenuItem item in Menu)
        {
          if(item.Type == "Dessert")DessertsListBox.Add(item.GetStringToShow());
        }
        This way you'll have one complex structure in which your items are organized, it's easier to modify this way, and you can use it in many places.

        I didn't compile/run the code so there may be some errors.

        Comment

        Working...