0

I am trying to get the index of a listview item that has just being checked and update a database based on the item that was just checked not considering other items that were checked before I am trying to use the checkbox to indicate a user wants a notification or not, so when a user checks the checkbox, i want to use the index of the item and set notification for that item to true but i can only get all the checkeditems indexes at once.

Any help please.

I was able to call the itemcheck event function but it considers items checked initially as well as items checked by user. I managed to separate the items checked initially using a boolean function "Item_checked by user"

 ` private static bool checked_by_user;
    private void courseworks_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        if (checked_by_user == true)
        { //do something
        }
        else ;//do nothing

    }`

Now, I want to be able to take the bar_ref_id of only the line that was just checked, My list view is created from a database as below

foreach (var item2 in CW_query2)//for each CW
            {
                if (item.Status == true)
                {
                    ListViewItem items = new ListViewItem(new[] {//adds items into list view, per column
                    item2.Module_Code, item2.Title, item2.Due_Date.ToString("dd/MM/yy"),"Submitted",item.Bar_Ref_ID
                });
                    courseworks.Items.Add(items);
                }
                else
                {
                    ListViewItem items = new ListViewItem(new[] {//adds items into list view, per column
                    item2.Module_Code, item2.Title, item2.Due_Date.ToString("dd/MM/yy"),"Not-Submitted",item.Bar_Ref_ID
                });
                    courseworks.Items.Add(items);

                }

I hope my added info helps. Thanks in advance

Gafar
  • 13
  • 5

1 Answers1

0

If you're already getting the subscriptions from the database and setting the Checked property for each item according to the the user's subscription, wouldn't it be easiest to use the checkboxes' CheckedChanged Event? It's hard to tell what your implementation might be, but you should be able to have one function for when the box is unchecked (removing a subscription), and another function for when the box is checked (adding a subscription).

If you are able to provide some code, I might be able to be more specific.

More Specific

In your ItemChecked event, .NET exposes the object sender and ItemCheckEventArgs e as the parameters of the event. Inside that function, you can look at the sender to get the ListViewItem that has been checked/unchecked, and you can look in e to retrieve the index of that item in the ListView (in case you can use the index to easily change data in your database). Here's a brief example I'm going to almost steal straight from Microsoft:

private void ListView1_ItemCheck1(object sender, ItemCheckEventArgs e)
{
    ListViewItem item = (ListViewItem)sender

    if (e.CurrentValue==CheckState.Unchecked)
    {
        Unsubscribe(e.Index, currentUserID);
          /*You can pass the Index of the ListViewItem that caused the event
          to a method that will update your database (I would find it easier
          to program a function that takes the current user's ID as a parameter)*/

        Unsubscribe(item.Name, currentUserID);
          /*OR this might be a better way for you to reference what subscription
          should be cancelled (again, in addition to a UserID)*/
    }
    else if((e.CurrentValue==CheckState.Checked))
    {
        Subscribe(e.Index, currentUserID);
    }
}

private void Unsubscribe(int index, int userID)
{
    //unsubscribe the referenced userID from the subscription at index
}

private void Unsubscribe(string subscriptionName, int userID)
{
    //unsubscribe the referenced userID from the subscription called subscriptionName
}

I am unable to provide a more specific example for your second bit of code, because I'm not quite certain what it's doing. It looks like you might be doing something more complicated than the code example above can handle, but perhaps the code will assist you.

Oran D. Lord
  • 697
  • 1
  • 9
  • 23
  • Thanks, I unimplemented it thou, because of another problem. but I will add part of my code – Gafar Mar 24 '14 at 04:36
  • @Gafar I updated my answer with a code example showing how you can use the ItemCheckEventArgs in the ItemChecked Event to find out which ListViewItem has been checked/unchecked, as well possibly what you can do with that information. – Oran D. Lord Apr 01 '14 at 08:15
  • thanks very much for this although I am just seeing it . – Gafar Apr 01 '14 at 09:19