【问题标题】:C# ListView check if ListViewItem is checkedC# ListView 检查 ListViewItem 是否被选中
【发布时间】:2011-06-26 01:56:21
【问题描述】:

我正在创建一个包含 ListViewItems 集合的列表视图,所有这些都带有复选框。我想检查检查了哪个项目。我知道如何启动 ItemChecked 事件,但每次将 ListViewItem 添加到 ListView 时都会启动该事件。我怎样才能防止这种情况?


为了帮助您了解我想要做什么,这里有一些关于该应用程序的信息。

我正在为红十字调度员构建一个应用程序。它将帮助他们跟踪现场的单位。除其他外,该应用程序用于记录传输。当传输期间有优先传输进入时,当前单元将被设置为保持。这将通过选中属于单元 ListViewItem 的复选框来完成。

通过选中复选框,对象(类型为 Unit)将属性 objUnit.onHold 设置为 true。当复选框未选中时,该属性将再次设置为 false。每 3 分钟,该应用程序将循环遍历所有单元,以查看是否有人仍然处于等待状态。如果是这样,将弹出一个消息框,提醒调度员该单元处于暂停状态。

所以你看,我必须确保调度程序确实选中或取消选中 ListViewItem。

我希望有人能指出我正确的方向。

【问题讨论】:

  • 如果您没有看到有用的答案,您可以添加 cmets 以提出后续问题。如果答案有用,您可以通过投票代表提供反馈。

标签: c# winforms listview checkbox


【解决方案1】:

您可以设置一个标志,表明您正在插入一个项目,如果选中该标志,则忽略该事件。

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; }
}

【讨论】:

    【解决方案2】:

    如果您使用 ItemCheck 事件而不是 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.");
        }
    

    【讨论】:

      【解决方案3】:

      列表视图上有一个 CheckedItems 属性,您可以使用ListView.CheckedItems Property (System.Windows.Forms)

      【讨论】:

        猜你喜欢
        • 2015-10-02
        • 1970-01-01
        • 1970-01-01
        • 2014-07-02
        • 2021-07-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多