locked
Add item to a listbox and remove item from a listbox RRS feed

  • Question

  • Description : User enters a number into a textbox when the user clicks on a button called 'ADD' the number from that textbox will be added to a listbox and user can then remove only the last number in the listbox, for an instance i have a 3 numbers ( 5, 6, 7 and when the user clicks the button 'REMOVE' last number which is number 7 will be removed and Displayed in a label control )

    Here is what i have done so far.

     

    XAML >>>>>>>>>>>>

    <Window x:Class="PopStack.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="300" Width="220">
      <StackPanel Orientation="Vertical">
        <StackPanel Orientation="Horizontal">
          <TextBox Width="155" Height="20" Name="txtNo"></TextBox>
          <Button Width="40" Margin="1" Content="Stack" Name="btnStack" Click="btnStack_Click"></Button>
        </StackPanel>
        <ListBox Width="200" Height="220" Name="lbxList"></ListBox>
        <StackPanel Orientation="Horizontal">
          <TextBlock Width="100" Name="txtShow"></TextBlock>
          <Button Width="100" Content="Pop" Name="btnPop" Click="btnPop_Click"></Button>
        </StackPanel>
      </StackPanel>
    </Window>

     

     

    MAINWINDOW.CS>>>>>>>>>>>>>>>>>>>>>>

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace PopStack
    {
      public partial class MainWindow : Window
      {
        private List<Stack> x;
    
        public MainWindow()
        {
          InitializeComponent();
          x = new List<Stack>();
        }
    
        private void btnStack_Click(object sender, RoutedEventArgs e)
        {
          string y = txtNo.Text;
          x.Add(new Stack { Number = Convert.ToInt32(y) });
        }
        private void btnPop_Click(object sender, RoutedEventArgs e)
        {
          txtShow.Text = "xxx";
        }
      }
    }

     

     

     

     

    Class.CS>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace PopStack
    {
      class Stack
      {
        public double Number { get; set; }
    
        public Stack() { }
    
        public Stack(int no)
        {
          Number = no;
        }
    
        public override string ToString()
        {
          return String.Format("{0}", Number);
        }
      }
    }
    
    

     

     

     

     

    Thank you.

    Thursday, March 10, 2011 5:02 PM

Answers

  • I will change a bit rt.garg`s code to:

        public Form1()
        {
          InitializeComponent();
          label1.Text = "";
        }
    
        private void buttonAdd_Click(object sender, EventArgs e)
        {
          if (textBox1.Text != String.Empty)
            listBox1.Items.Add(textBox1.Text);
          textBox1.Text = "";
        }
    
        private void buttonRemove_Click(object sender, EventArgs e)
        {
          int index = listBox1.Items.Count - 1;
          string lastItem = listBox1.Items[index].ToString();
          label1.Text = lastItem;
          listBox1.Items.RemoveAt(index);
        }
    
    Mitja

    • Proposed as answer by rt.garg1991 Thursday, March 10, 2011 7:49 PM
    • Marked as answer by C-Sharp Rabbit Friday, March 11, 2011 4:22 PM
    Thursday, March 10, 2011 7:40 PM
  • If I am understanding your problem correctly, then you don't have to use stacks. When you add an item in listbox, it adds at the min index which can be possible for it to use in listbox (i.e if you have 3 items, then newly added item will add at index 3). You just have to remove item at index 0 from the list box which will remove that item from the list which was added before all the other item present in the list.

    try the following code if it can help you achieving your target. I have a list listBox1, two buttons (button1 for Add, button2 to remove), one label (label1) and one TextBox (textBox1)

        private void button1_Click(object sender, RoutedEventArgs e)
        {
          listBox1.Items.Add(textBox1.Text);
          textBox1.Clear();
        }
    
        private void button2_Click(object sender, RoutedEventArgs e)
        {
          label1.Content = listBox1.Items[0].ToString();
          listBox1.Items.RemoveAt(0);
        }
    


    How To Articles
    Thursday, March 10, 2011 7:20 PM

All replies

  • If you want to use a Stack then use a Stack

    http://msdn.microsoft.com/en-us/library/3278tedw(v=VS.100).aspx

    Regards

    Thursday, March 10, 2011 6:05 PM
  • If I am understanding your problem correctly, then you don't have to use stacks. When you add an item in listbox, it adds at the min index which can be possible for it to use in listbox (i.e if you have 3 items, then newly added item will add at index 3). You just have to remove item at index 0 from the list box which will remove that item from the list which was added before all the other item present in the list.

    try the following code if it can help you achieving your target. I have a list listBox1, two buttons (button1 for Add, button2 to remove), one label (label1) and one TextBox (textBox1)

        private void button1_Click(object sender, RoutedEventArgs e)
        {
          listBox1.Items.Add(textBox1.Text);
          textBox1.Clear();
        }
    
        private void button2_Click(object sender, RoutedEventArgs e)
        {
          label1.Content = listBox1.Items[0].ToString();
          listBox1.Items.RemoveAt(0);
        }
    


    How To Articles
    Thursday, March 10, 2011 7:20 PM
  • I will change a bit rt.garg`s code to:

        public Form1()
        {
          InitializeComponent();
          label1.Text = "";
        }
    
        private void buttonAdd_Click(object sender, EventArgs e)
        {
          if (textBox1.Text != String.Empty)
            listBox1.Items.Add(textBox1.Text);
          textBox1.Text = "";
        }
    
        private void buttonRemove_Click(object sender, EventArgs e)
        {
          int index = listBox1.Items.Count - 1;
          string lastItem = listBox1.Items[index].ToString();
          label1.Text = lastItem;
          listBox1.Items.RemoveAt(index);
        }
    
    Mitja

    • Proposed as answer by rt.garg1991 Thursday, March 10, 2011 7:49 PM
    • Marked as answer by C-Sharp Rabbit Friday, March 11, 2011 4:22 PM
    Thursday, March 10, 2011 7:40 PM
  • Thanx Mitja and Sorry C-Sharp. I wrote code for queue which follows FIFO procedure but stack follows LIFO. So you have to remove item which is at the highest index.

    How To Articles
    Thursday, March 10, 2011 7:48 PM
  • Hello Mitja, If I just remove an item present in between other items, will that make other items to move one position up or will leave that space or that index in the list box just empty?
    Thursday, March 10, 2011 10:19 PM