c# - 不允许 ListView 具有零个选定项

标签 c# winforms listview

我的项目是 .NET/WinForms。

我有一个总是充满项目的 ListView 。我希望它总是有选择。但是,如果我单击 ListView 项下方的空白区域,则会失去选择。

列表有多个选择 = true 和隐藏选择 = false。

最佳答案

您需要防止 native 控件看到鼠标单击,这样它就不会取消选择项目。向您的项目添加一个新类并粘贴如下所示的代码。编译。将它从工具箱的顶部拖放到您的表单上,替换现有的表单。

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

关于c# - 不允许 ListView 具有零个选定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3255046/

相关文章:

c# - 即使最小化也将 key 发送到表单

c# - 如何获取 ListView 数据模板项目双击WPF MVVM

java - Android:如何为每个 ListView 列表项分配单独的图标?

c# - 使用 c# 和 asp.net 将 HTML 内容转换为 Pdf

c# - 我们可以在 ServicePointManager.SecurityProtocol 中添加四个协议(protocol)吗?

c# - 数据库恢复后, Entity Framework 有时无法识别过去的迁移

c# winform绘制闪烁图像

android - ListView 内部 fragment 错误

c# - 本地化 ObjectListView OLVColumn,由于 Empty Name 属性而无法实现

c# - 当我没有实例化 "Response.Redirect"时,如何从 Razor 网页使用 "Response"?