禁止ListView使所选项目为零

时间:2010-07-15 11:22:56

标签: c# winforms listview

我的项目是.NET / WinForms。

我有一个列表视图,总是用项目填充。我希望它总是有选择。但是,如果单击列表视图项下方的空白区域,则会丢失选择。

该列表有多个selection = true且hide selection = false。

6 个答案:

答案 0 :(得分:25)

您需要阻止本机控件看到鼠标单击,以便它不会取消选择项目。在项目中添加一个新类并粘贴下面显示的代码。编译。将其从工具箱顶部拖放到表单上,替换现有表单。

using System;
using System.Drawing;
using System.Windows.Forms;

class MyListView : ListView {
    protected override void WndProc(ref Message m) {
        // Swallow mouse messages that are not in the client area
        if (m.Msg >= 0x201 && m.Msg <= 0x209) {
            Point pos = new Point(m.LParam.ToInt32());
            var hit = this.HitTest(pos);
            switch (hit.Location) {
                case ListViewHitTestLocations.AboveClientArea :
                case ListViewHitTestLocations.BelowClientArea :
                case ListViewHitTestLocations.LeftOfClientArea :
                case ListViewHitTestLocations.RightOfClientArea :
                case ListViewHitTestLocations.None :
                    return;
            }
        }
        base.WndProc(ref m);
    }
}

答案 1 :(得分:2)

就像其他人所说的那样,SelectedIndexChanged事件是你应该看到的,但你应该与ItemSelectionChanged事件合作使用它。这是我刚刚编写的一些代码:

// Holds the last selected index
private int _previousIndex = -1;

// Restores the previous selection if there are no selections
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (listView1.SelectedIndices.Count == 0)
    {
        if (_previousIndex >= 0)
        {
            listView1.SelectedIndices.Add(_previousIndex);
        }
    }
}

// Records the last selected index
private void listView1_ItemSelectionChanged(object sender, 
               ListViewItemSelectionChangedEventArgs e)
{
    if (e.IsSelected)
    {
        _previousIndex = e.ItemIndex;
    }
}

对于纯代码重用目的,将此代码放入新的UserControl并且具有确定是否允许丢失最后一个选择的属性可能是值得的:

public class CustomListView : ListView
{

    protected CustomListView()
    {
        this.SelectedIndexChanged += new EventHandler(CustomListView_SelectedIndexChanged);
        this.ItemSelectionChanged += new ListViewItemSelectionChangedEventHandler(CustomListView_ItemSelectionChanged);
    }

    void CustomListView_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.MaintainLastSelection && this.SelectedIndices.Count == 0)
        {
            if (_previousIndex >= 0)
            {
                this.SelectedIndices.Add(_previousIndex);
            }
        }
    }

    void CustomListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
    {
        if (e.IsSelected)
        {
            _previousIndex = e.ItemIndex;
        }
    }

    private int _previousIndex = -1;

    public bool MaintainLastSelection
    {
        get { return _maintainLastSelection; }
        set { _maintainLastSelection = value; }
    }
    private bool _maintainLastSelection;

}

答案 2 :(得分:2)

如果您不需要多选,则以下内容有效。它的优点是无需跟踪上次选择的项目。

private void ListViewAny_SelectedIndexChanged(object sender, EventArgs e)
{
    if ((sender as ListView).FocusedItem != null)
    {
        (sender as ListView).FocusedItem.Selected = true;
    }
}

答案 3 :(得分:1)

    private void LV_MouseUp(object sender, MouseEventArgs e)
    {
        if (LV.FocusedItem != null)
        {
            if (LV.SelectedItems.Count == 0)
                LV.FocusedItem.Selected = true;
        }
    }

(没有多选)

答案 4 :(得分:0)

您需要处理ListView的SelectedIndexChanged事件,如果在此事件发生后没有选择任何项目,请以编程方式重新选择上一个已知选择。

答案 5 :(得分:0)

ListViewSelectedIndexChanged个事件。听取这个,然后查询listView.SelectedItems.Count属性 - 如果它为零,选择第一个/最后一个选定的项目。