0

I have searched around but could not find any references.

How do I delete an item in a generic list that relates to items in a listbox?

I currently have a public static List<Employees> and a listbox named lstRecords, I can remove the item in the listbox just fine, but either everything is removed from the list or nothing at all.

This was my first set of code I was working with:

private void DeleteRecord()
{
        if (lstRecords.Items.Count > 0)
        {
            for (int i = 0; i < lstRecords.Items.Count; i++)
            {
                if (lstRecords.GetSelected(i) == true)
                {
                    Employees employeeRecord = lstRecords.SelectedItem as Employees;
                    employee.Remove(employeeRecord);
                }
            }



            lstRecords.Items.Remove(lstRecords.SelectedItem);
        }
    }  
}

This is my 2nd set of code I was working with, I have my List right under partial class, but this is all contained in a method.

private void DeleteRecord()
{
        ListBox lstRecords = new ListBox();
        List<object> employee = new List<object>();
        employee.RemoveAt(lstRecords.SelectedIndex);
        lstRecords.Items.RemoveAt(lstRecords.SelectedIndex);
}  

So far I haven't gotten either set of code to work the way I would like it to, I'm obviously doing something wrong.

I have a few other blocks of code I played around with but these seemed to be headed in the right direction.

Eventually I'll need to be able to double click an item in the list to pull up the properties menu.

Prix
  • 19,417
  • 15
  • 73
  • 132
user2391210
  • 53
  • 1
  • 5
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jul 19 '13 at 21:51
  • So do you get any error ? can you post your portion of code for inserting items to the list ? – Prix Jul 19 '13 at 21:52
  • your second code is so ridiculous to me :). You created totally empty `List<>` and `ListBox` and remove selected item from them. :)) It's funny. Where are the actual `List<>` and `ListBox` of yours? – King King Jul 19 '13 at 21:54
  • Thank you for helping edit my title. I'll try to watch for that next time. – user2391210 Jul 19 '13 at 21:58
  • Did you try to step through your code and see how and when you entered the second if-statement? I find it confusing that you use two methods of selecting: lstRecords.GetSelected() and lstRecords.SelectedItem. – Richard Jul 19 '13 at 22:00
  • and yes, it is ridiculous code, some may say, redonkulous. One day I may break the internet...on accident. Thank you for your comments, I appreciate it. I started getting crazy with my code for no reason. – user2391210 Jul 19 '13 at 22:02
  • I'm stepping through code now. – user2391210 Jul 19 '13 at 22:12

2 Answers2

0

What you want to do is bind your ListBox to you List of employees. This post shows the binding and the comments shows the removing code as well. The idea is that when you remove an item from the DataSource, then you won't see it in the ListBox.

Binding Listbox to List<object>

The problem with the DeleteRecord() method is that the lstRecords object you just created isn't the ListBox that is on the form.

Community
  • 1
  • 1
Glenn Cuevas
  • 166
  • 5
  • Okay, I've went the route of what you have posted here. I've put in `lstRecords.DataSource = employee; employee.Remove((Employees)lstRecords.SelectedValue);` However, it's still deleting both objects in my list and gives a value in the list EmployeeForm.Employees in all records spots. – user2391210 Jul 19 '13 at 22:07
  • It almost works, I'm getting some bumps, it's reversing some of my records now, for example: Smith, John turns to John, Smith. I'm gonna go play with this for a bit. Thanks everyone for giving me something to work with. I appreciate it very much. – user2391210 Jul 19 '13 at 22:16
  • If it's deleting both objects, it sounds like you may have added the same object into the list twice. – Glenn Cuevas Jul 19 '13 at 22:21
0

Your code runs fine you just have to make some small changes. The first code block is Ok however I dont know where your lstRecords are. But have a look at this just copy the code and run it after you have some records in your employee object.

It's createing a listbox in code then adds it to the form(Winforms) and having the lstRecords globaly.

ListBox lstRecords;
    private void IntializeDemoListbox()
    {
        lstRecords = new ListBox();
        this.Controls.Add(lstRecords);

        foreach (var item in employee)
        {
            lstRecords.Items.Add(item);
        }
    }

And then you will be able to use your first set of code the other set will be like this.

private void DeleteRecord()
{
        employee.RemoveAt(lstRecords.SelectedIndex);
        lstRecords.Items.RemoveAt(lstRecords.SelectedIndex);
}  
Labrinths
  • 120
  • 1
  • 11
  • Yes! You are awesome. It didn't work at first, but I changed lstRecords to lstRecords1 and that worked. Why would that be the case? Is it because the other list wasn't global?(I'm just guessing here). – user2391210 Jul 19 '13 at 23:45
  • I think I've got it, either way this is what I needed. Thank you again. – user2391210 Jul 20 '13 at 00:15
  • Well the first issue is the name you probobly hade ambigius names. And yes you need the list to be globaly so that other methods can use it and every time you calling the = new you are actually creating a new instance of that object and all your previus data will be lost.. You can now mark my answer as "Answered" thank you :) – Labrinths Jul 20 '13 at 00:18