HelpNeeder
HelpNeeder

Reputation: 6480

How do I create EventHandler for ItemChecked in ListView?

I am trying to delete each checked item on my ListView item in detailed list. I don't want to delete entry while checking, but pass a value to my button which will use a value to delete entry.

I have played with it a little but I came across few problems. First of all, I cannot create an event handler by doing this: lvPC.ItemChecked += new EventHandler(CheckedState); which causes error: No overload for 'CheckedState' matches delegate 'System.EventHandler'.

Second, how can I get get the ID of the item? By saying this I mean that each added entry has it's own ID, but it isn't added to the table view. Meaning how do I decide which ID should be removed?

private void CheckedState
    (object sender, System.Windows.Forms.ItemCheckEventArgs e)
{
    ListView.CheckedListViewItemCollection 
        checkedItems = lvPC.CheckedItems;

    string items;

    foreach (ListViewItem item in checkedItems)
    {
        items = item + ", "; 
        MessageBox.Show(items);
    }

    if (e.CurrentValue == CheckState.Unchecked)
    {
    }
    else if ((e.CurrentValue == CheckState.Checked))
    {
    }
}

This is how I am inserting data into 3 different ListView:

using (OleDbDataReader read = command.ExecuteReader())
{
    // Checking if there is any data in the file.
    if (read.HasRows)
    {
        // Reading information from the file.
        while (read.Read())
        {
            if (security.DecryptAES(read.GetString(1), 
                storedAuth.Password, storedAuth.UserName) 
                    == "PC Password")
            {
                PC = new ListViewItem("");
                PC.SubItems.Add(security.DecryptAES
                    (read.GetString(5), storedAuth.Password, 
                        storedAuth.UserName));
                PC.SubItems.Add(security.DecryptAES
                    (read.GetString(6), storedAuth.Password, 
                        storedAuth.UserName));

                lvPC.Items.Add(PC);
            }
            else if (security.DecryptAES(read.GetString(1), 
                storedAuth.Password, storedAuth.UserName) 
                    == "Web Site Password")
            {
                Web = new ListViewItem("");
                Web.SubItems.Add(security.DecryptAES
                    (read.GetString(2), storedAuth.Password, 
                        storedAuth.UserName));
                Web.SubItems.Add(security.DecryptAES
                    (read.GetString(5), storedAuth.Password, 
                        storedAuth.UserName));
                Web.SubItems.Add(security.DecryptAES
                    (read.GetString(6), storedAuth.Password, 
                        storedAuth.UserName));

                lvWeb.Items.Add(Web);
            }
            else if (security.DecryptAES(read.GetString(1), 
                storedAuth.Password, storedAuth.UserName) 
                    == "Serial Code")
            {
                Serial = new ListViewItem("");
                Serial.SubItems.Add(security.DecryptAES
                    (read.GetString(3), storedAuth.Password, 
                        storedAuth.UserName));
                Serial.SubItems.Add(security.DecryptAES
                    (read.GetString(4), storedAuth.Password, 
                        storedAuth.UserName));

                lvSerialCode.Items.Add(Serial);
            }
        }
    }
}

Upvotes: 1

Views: 3533

Answers (3)

MartinDotNet
MartinDotNet

Reputation: 2505

void CheckedState(object sender, System.Windows.Forms.ItemCheckedEventArgs e)

and

lvPC.ItemChecked += new ItemCheckedEventHandler(CheckedState);

are part of the solution, but on the getting of the id, I would suggest that you create a small object, add them to a BindingList and bind that to the ListView.

In Visual Studio, you can avoid problems with mismatched EventHandlers by just typing the += part and then hitting "tab" twice (once for the "new handler" part, and once for the method it should call), it should then create it all for you.

The advantage is that you should then cast the checked item from "e.Item" to the object you want and simply remove it from the binding list and, if needed, perform database removal.

Hope this helps, I know it's not the first answer, but it does answer both of your questions...

Upvotes: 1

competent_tech
competent_tech

Reputation: 44931

First, the signature for the event is incorrect. It should be:

void CheckedState(object sender, System.Windows.Forms.ItemCheckedEventArgs e)

Second, you can set the Tag property in each ListViewItem that you add:

item.Tag = 1;

Then reference this when you look at the checked items.

Update

Missed the assignment of the CheckedState event in the initial response. It should be:

lvPC.ItemChecked += new ItemCheckedEventHandler(CheckedState);

Upvotes: 1

Jeff Ogata
Jeff Ogata

Reputation: 57783

I might be misunderstanding your question, but if you simply want to remove checked items on a button click, I don't think you need to listen to the ItemChecked event to track which items are checked.

Instead, just loop through the checked items when the button is clicked and remove them from the ListView:

private void button1_Click(object sender, EventArgs e)
{
    foreach (ListViewItem item in listView1.CheckedItems)
    {
        listView1.Items.Remove(item);
    }
}

If you only want to remove certain items that are checked, then in the loop above, look at the item and determine whether you want to delete it, either using a value stored in the text or the item's tag, as suggested by competent_tech.

Upvotes: 1

Related Questions