473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

7
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 :
Expand|Select|Wrap|Line Numbers
  1. private void btnDeleteLoan_Click(object sender, RoutedEventArgs e)
  2.         {
  3.             Loan SelectedLoan = (Loan) lstLoans.SelectedItem;
  4.             loans.Remove(SelectedLoan);
  5.             lstLoans.Items.Remove(SelectedLoan);
  6.  
  7.             lstLoans.ItemsSource = loans;
  8.  
  9.             lstLoans.Items.Remove(lstLoans.SelectedItem);
  10.         }
  11.  
Thanks in advance.
Apr 28 '11 #1

✓ answered by GaryTexmo

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.

Expand|Select|Wrap|Line Numbers
  1. public partial class Form1 : Form
  2. {
  3.     private ListBox listBox1 = new ListBox();
  4.     private Button button1 = new Button();
  5.  
  6.     public Form1()
  7.     {
  8.         InitializeComponent();
  9.  
  10.         button1.Location = new Point(12, 12);
  11.         button1.Text = "Remove";
  12.         button1.Click += new EventHandler(button1_Click);
  13.  
  14.         listBox1.Location = new Point(12, button1.Bottom + 12);
  15.         listBox1.Size = new Size(this.Width - (3 * 12), this.Height - (5 * 12) - button1.Height);
  16.         listBox1.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
  17.  
  18.         this.Controls.Add(button1);
  19.         this.Controls.Add(listBox1);
  20.  
  21.         listBox1.Items.Add(new ListBoxItem("one", 1));
  22.         listBox1.Items.Add(new ListBoxItem("two", 2));
  23.         listBox1.Items.Add(new ListBoxItem("three", 3));
  24.         listBox1.Items.Add(new ListBoxItem("four", 4));
  25.         listBox1.Items.Add(new ListBoxItem("five", 5));
  26.         listBox1.Items.Add(new ListBoxItem("six", 6));
  27.         listBox1.Items.Add(new ListBoxItem("seven", 7));
  28.         listBox1.Items.Add(new ListBoxItem("eight", 8));
  29.         listBox1.Items.Add(new ListBoxItem("nine", 9));
  30.         listBox1.Items.Add(new ListBoxItem("ten", 10));
  31.     }
  32.  
  33.     private void button1_Click(object sender, EventArgs e)
  34.     {
  35.         if (listBox1.SelectedItem != null)
  36.         {
  37.             ListBoxItem item = listBox1.SelectedItem as ListBoxItem;
  38.             if (item != null)
  39.             {
  40.                 DialogResult dr = MessageBox.Show(string.Format("Remove item, {0}, with a value of {1}?", item.Text, item.Value), "Remove?", MessageBoxButtons.YesNo);
  41.                 if (dr == DialogResult.Yes)
  42.                 {
  43.                     listBox1.Items.Remove(item);
  44.                 }
  45.             }
  46.         }
  47.     }
  48. }
  49.  
  50. public class ListBoxItem
  51. {
  52.     public string Text { get; set; }
  53.     public int Value { get; set; }
  54.  
  55.     public ListBoxItem() { }
  56.  
  57.     public ListBoxItem(string text, int value)
  58.         : this()
  59.     {
  60.         Text = text;
  61.         Value = value;
  62.     }
  63.  
  64.     public override string ToString()
  65.     {
  66.         return this.Text;
  67.     }
  68. }

2 1749
GaryTexmo
1,501 Expert 1GB
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.

Expand|Select|Wrap|Line Numbers
  1. public partial class Form1 : Form
  2. {
  3.     private ListBox listBox1 = new ListBox();
  4.     private Button button1 = new Button();
  5.  
  6.     public Form1()
  7.     {
  8.         InitializeComponent();
  9.  
  10.         button1.Location = new Point(12, 12);
  11.         button1.Text = "Remove";
  12.         button1.Click += new EventHandler(button1_Click);
  13.  
  14.         listBox1.Location = new Point(12, button1.Bottom + 12);
  15.         listBox1.Size = new Size(this.Width - (3 * 12), this.Height - (5 * 12) - button1.Height);
  16.         listBox1.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
  17.  
  18.         this.Controls.Add(button1);
  19.         this.Controls.Add(listBox1);
  20.  
  21.         listBox1.Items.Add(new ListBoxItem("one", 1));
  22.         listBox1.Items.Add(new ListBoxItem("two", 2));
  23.         listBox1.Items.Add(new ListBoxItem("three", 3));
  24.         listBox1.Items.Add(new ListBoxItem("four", 4));
  25.         listBox1.Items.Add(new ListBoxItem("five", 5));
  26.         listBox1.Items.Add(new ListBoxItem("six", 6));
  27.         listBox1.Items.Add(new ListBoxItem("seven", 7));
  28.         listBox1.Items.Add(new ListBoxItem("eight", 8));
  29.         listBox1.Items.Add(new ListBoxItem("nine", 9));
  30.         listBox1.Items.Add(new ListBoxItem("ten", 10));
  31.     }
  32.  
  33.     private void button1_Click(object sender, EventArgs e)
  34.     {
  35.         if (listBox1.SelectedItem != null)
  36.         {
  37.             ListBoxItem item = listBox1.SelectedItem as ListBoxItem;
  38.             if (item != null)
  39.             {
  40.                 DialogResult dr = MessageBox.Show(string.Format("Remove item, {0}, with a value of {1}?", item.Text, item.Value), "Remove?", MessageBoxButtons.YesNo);
  41.                 if (dr == DialogResult.Yes)
  42.                 {
  43.                     listBox1.Items.Remove(item);
  44.                 }
  45.             }
  46.         }
  47.     }
  48. }
  49.  
  50. public class ListBoxItem
  51. {
  52.     public string Text { get; set; }
  53.     public int Value { get; set; }
  54.  
  55.     public ListBoxItem() { }
  56.  
  57.     public ListBoxItem(string text, int value)
  58.         : this()
  59.     {
  60.         Text = text;
  61.         Value = value;
  62.     }
  63.  
  64.     public override string ToString()
  65.     {
  66.         return this.Text;
  67.     }
  68. }
Apr 28 '11 #2
bvrwoo
16
Well, you have to consider that fact that you might be losing focus on the listbox.
First,
Expand|Select|Wrap|Line Numbers
  1. if (listbox.SelectedIndex > -1)
  2. {
  3.    int index = listbox.SelectedIndex;
  4.    Loan l = (Loan)listBox.Items[index];
  5.    listbox.Items.Remove(listbox.Items[index]);
  6.    list.Remove(l);
  7. }
  8.  
Aug 29 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Peter Moscatt | last post by:
I am having trouble understanding the methods for the Listbox from Tk. If I was to select at item in the list using a mouse click (have already created the bind event) - what method returns the...
4
by: Filips Benoit | last post by:
Dear All, I have code that selects - find and select - 1 item in a large listbox. This works Ok but most of the time the selected item isn't visible without scrolling down manualy. Is this...
2
by: Alpha | last post by:
How do I change the selected item in a listbox according to a Combox's selected item on the same form? The Combox is from a different table in the same dataset as the Listbox uses. Combox's...
6
by: David De Cotis | last post by:
Hello all, I am trying to go through a ListBox and verify if am item was selected. If an item was selected, I would like to get a handle of the item and simply do a response.write on the selected...
1
by: Karen Grube | last post by:
Hi! I'm using a standard server side ASP.Net listbox control on a web form. The page is basically various shades of green. The listbox itself has a pale green background and forest green text...
4
by: juststarter | last post by:
Hello, I have an aspx file where i've put a placeholder element. On load (page_load) i create dynamically an html table which contains a checkbox and a radiobuttonlist in each tablerow . The...
1
by: acord | last post by:
Hi, I am having problem to get a value of the selected item from a dropdown listbox. Here is the JS function; function getSelectedItem(objSelect) { alert("in getSelectedItem"); alert...
3
by: peter.mosley | last post by:
I've tried googling for the answer to this problem, without any luck. I'm sure the truth must be out there somewhere! I have a multiselect listbox populated with many items (set by the RowSource...
2
by: iDesmet | last post by:
Hallo, I have 2 forms. In one form (let's call it frmClient) I have textboxes, comboboxes, etc in which I can save/update information to a database (MySQL to be more exactly). The other form...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.