LIFO STACK (Windows application)

998 Views Asked by At

I need to do an excercise with windows form but I'm beginner programer on c#, and I want help..

First... I need to list this numbers on a windows Form: -20, 45, -10, 20, 1 but I dont know if I only need to use label of toolbox and change value to do that.

Second... I need to create button, that when I clic, It just disappear negative values (-20,-10)

Three... I need another button that order numbers in LIFO

Can any one help me uploading example or program please? I don't have any idea how to do that

1

There are 1 best solutions below

7
On

This might be a solution. Please copy the following code to your form :

        List<int> numbers = new List<int>();
        private void positiveButton_Click(object sender, EventArgs e)
        {
            RefreshList(numbers.Where(x => x > 0).ToList());
        }
        private void RefreshList(List<int> list)
        {
            listBox1.Items.Clear();
            foreach (int item in list)
                listBox1.Items.Add(item);
        }
        private void addButton_Click(object sender, EventArgs e)
        {
            int newValue;
            if (int.TryParse(textBox1.Text, out newValue))
            {
                numbers.Add(newValue);
                listBox1.Items.Add(textBox1.Text);
            }
            else
                MessageBox.Show("Please enter a number.");
        }
        private void ShowAllButton_Click(object sender, EventArgs e)
        {
            RefreshList(numbers);
        }
        private void lifoButton_Click(object sender, EventArgs e)
        {
            numbers.Reverse();
            RefreshList(numbers);
        }

you should design your form like this :

enter image description here

I used a listbox for showing the numbers and a list in background to keep them all. EDIT : add this button to solve the problem

   private void lifoPositiveButton_Click(object sender, EventArgs e)
        {
            RefreshList(numbers.Where(x => x > 0).Reverse().ToList());
        }

Design the form and assign the appropriate events. I hope this helps.