C # ListView checks if ListViewItem is checked

I am creating a listview with collections of ListViewItems, all with checkboxes. I want to check that the item is checked. I know how to fire the ItemChecked event, but the event fires every time a ListViewItem is added to the ListView. How can I prevent this?


To help you understand what I want to do, here is some information about the application.

I am creating an application for Red Cross dispatchers. This will help them track units in the field. The application is used, in particular, to register transfers. When priority transmission arrives during transmission, the current unit will be held on hold. This will be done by checking the box that belongs to the ListViewItem blocks.

By checking the box, an object (of type Unit) sets the objUnit.onHold property to true. If unchecked, the property will again be set to false. Every 3 minutes, the application will go through all units to see if everyone is on hold. If so, a pop-up window will appear with a message reminding the device manager on hold.

So, you see, I have to be sure that the dispatcher actually checked or unchecked the ListViewItem.

Hope someone can point me in the right direction.

+3
source share
3 answers

You can set a flag indicating that you are inserting an element and ignoring the event if the check box is selected.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;

class Form1 : Form
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    ListView listView;
    List<Unit> units;
    bool insertingItem = false;

    public Form1()
    {
        Controls.Add(listView = new ListView
        {
            Dock = DockStyle.Fill,
            View = View.Details,
            CheckBoxes = true,
            Columns = { "Name" },
        });

        Controls.Add(new Button { Text = "Add", Dock = DockStyle.Top });
        Controls[1].Click += (s, e) => AddNewItem();

        listView.ItemChecked += (s, e) =>
            {
                Unit unit = e.Item.Tag as Unit;
                Debug.Write(String.Format("Item '{0}' checked = {1}", unit.Name, unit.OnHold));
                if (insertingItem)
                {
                    Debug.Write(" [Ignored]");
                }
                else
                {
                    Debug.Write(String.Format(", setting checked = {0}", e.Item.Checked));
                    unit.OnHold = e.Item.Checked;
                }
                Debug.WriteLine("");
            };

        units = new List<Unit> { };
    }

    Random Rand = new Random();
    int NameIndex = 0;
    readonly string[] Names = { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" };
    void AddNewItem()
    {
        if (NameIndex < Names.Length)
        {
            Unit newUnit = new Unit { Name = Names[NameIndex++], OnHold = Rand.NextDouble() < 0.6 };
            units.Add(newUnit);
            insertingItem = true;
            try
            {
                listView.Items.Add(new ListViewItem { Text = newUnit.Name, Checked = newUnit.OnHold, Tag = newUnit });
            }
            finally
            {
                insertingItem = false;
            }
        }
    }
}

class Unit
{
    public string Name { get; set; }
    public bool OnHold { get; set; }
}
+1
source

ItemChecked ItemChecked, , .

    //Counter to differentiate list items
    private int counter = 1;

    private void button1_Click(object sender, EventArgs e)
    {
        //Add a new item to the list
        listView1.Items.Add(new ListViewItem("List item " + counter++.ToString()));
    }

    private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        //Get the item that was checked / unchecked
        ListViewItem l = listView1.Items[e.Index];

        //Display message
        if (e.NewValue == CheckState.Checked)
            MessageBox.Show(l.ToString() + " was just checked.");

        else if (e.NewValue == CheckState.Unchecked)
            MessageBox.Show(l.ToString() + " was just unchecked.");
    }
+1

Source: https://habr.com/ru/post/1791671/


All Articles