How to create a button that deletes the selected item from a listbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vagos
    New Member
    • Apr 2011
    • 7

    How to create a button that deletes the selected item from a listbox

    Hello, I am using list boxs, my NewLoan button works just fine, adds the loans to the specific lstLoans area, but this code won't delete the selected item, any suggestions?

    Here is the code :
    Code:
    private void btnDeleteLoan_Click(object sender, RoutedEventArgs e)
            {
                Loan SelectedLoan = (Loan) lstLoans.SelectedItem;
                loans.Remove(SelectedLoan);
                lstLoans.Items.Remove(SelectedLoan);
                
                lstLoans.ItemsSource = loans;
    
                lstLoans.Items.Remove(lstLoans.SelectedItem);
            }
    Thanks in advance.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    What type is lstLoans? I don't think it's a ListBox because a ListBox doesn't have an ItemSource property that I can see, at least not in .NET 4. Is there more to your code than we can see from what you've posted?

    I did up a quick little example for removing items from a ListBox to confirm the functionality and it seems to be working fine for me.

    Code:
    public partial class Form1 : Form
    {
        private ListBox listBox1 = new ListBox();
        private Button button1 = new Button();
    
        public Form1()
        {
            InitializeComponent();
    
            button1.Location = new Point(12, 12);
            button1.Text = "Remove";
            button1.Click += new EventHandler(button1_Click);
    
            listBox1.Location = new Point(12, button1.Bottom + 12);
            listBox1.Size = new Size(this.Width - (3 * 12), this.Height - (5 * 12) - button1.Height);
            listBox1.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
    
            this.Controls.Add(button1);
            this.Controls.Add(listBox1);
    
            listBox1.Items.Add(new ListBoxItem("one", 1));
            listBox1.Items.Add(new ListBoxItem("two", 2));
            listBox1.Items.Add(new ListBoxItem("three", 3));
            listBox1.Items.Add(new ListBoxItem("four", 4));
            listBox1.Items.Add(new ListBoxItem("five", 5));
            listBox1.Items.Add(new ListBoxItem("six", 6));
            listBox1.Items.Add(new ListBoxItem("seven", 7));
            listBox1.Items.Add(new ListBoxItem("eight", 8));
            listBox1.Items.Add(new ListBoxItem("nine", 9));
            listBox1.Items.Add(new ListBoxItem("ten", 10));
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                ListBoxItem item = listBox1.SelectedItem as ListBoxItem;
                if (item != null)
                {
                    DialogResult dr = MessageBox.Show(string.Format("Remove item, {0}, with a value of {1}?", item.Text, item.Value), "Remove?", MessageBoxButtons.YesNo);
                    if (dr == DialogResult.Yes)
                    {
                        listBox1.Items.Remove(item);
                    }
                }
            }
        }
    }
    
    public class ListBoxItem
    {
        public string Text { get; set; }
        public int Value { get; set; }
    
        public ListBoxItem() { }
    
        public ListBoxItem(string text, int value)
            : this()
        {
            Text = text;
            Value = value;
        }
    
        public override string ToString()
        {
            return this.Text;
        }
    }

    Comment

    • bvrwoo
      New Member
      • Aug 2011
      • 16

      #3
      Well, you have to consider that fact that you might be losing focus on the listbox.
      First,
      Code:
      if (listbox.SelectedIndex > -1)
      {
         int index = listbox.SelectedIndex;
         Loan l = (Loan)listBox.Items[index];
         listbox.Items.Remove(listbox.Items[index]);
         list.Remove(l);
      }

      Comment

      Working...