Quentin DELIL
Quentin DELIL

Reputation: 13

Adding and displaying items in list C#

I'm currently creating a Windows Phone 8.1 App, for a friend who owns a Bar. I've a string array which contains 30 shooter names, with different flavours. The goal of this app is to select randomly an item from this array, by clicking on a button and displaying the selected item in a TextBlock.

This part works.

Now what I want to do is to add the name of the selected shot in a List, displayed in a ListBox, for each click on the button, and sort the list alphabetically.

It will be easier when ordering (in case of large group of friends).

Now my code with shorthened array (from 30 to 4 items) :

public string[] Shooters = new string[4] {"B52", "Baby Guinness", "Bizu", "Jedi"};


    public List<string> Commande = new List<string>();
    public void ChoixShot_Click(object sender, RoutedEventArgs e)
    {
        Random ran = new Random();

        string name = null;
        int ind = 0;
        ind = ran.Next(0, Shooters.Length);
        name = Shooters[ind];
        Sanction.DataContext = name;
        Commande.Add(name);
        Commande.Sort();
        ListeCommande.ItemsSource = Commande;            
    }

It displays only the name selected with the first click.

Thanks for considering my question, and spending time for me :)

Upvotes: 0

Views: 78

Answers (1)

Fluff
Fluff

Reputation: 89

This works for me:

class Program
    {
        public string[] Shooters = new string[4] { "B52", "Baby Guinness", "Bizu", "Jedi" };
        public List<string> Commande = new List<string>();

        static void Main(string[] args)
        {

            Random ran = new Random();
            Program shots = new Program();

            string name = null;


            name = Convert.ToString(ran.Next(0, shots.Shooters.Length));
            Sanction.DataContext = name;
            Commande.Add(name);
            Commande.Sort();
            ListeCommande.ItemsSource = Commande;    

        }
    }

Upvotes: 1

Related Questions