如何保持一个项目选择? - 列表显示 [英] How to keep an item selected? - ListView

查看:170
本文介绍了如何保持一个项目选择? - 列表显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保持一个项目选择,在一个ListView,当用户点击一个没有项目的空间。例如,下方空间中的项目,但仍然对在ListView组件。我已经改变ListView的财产HideSelection为假,但只能当焦点更改为另一个组件;不是当用户点击的ListView本身。谢谢!

I would like to keep an item selected, on a ListView, when the user clicks on a space which has no item. For example, the space below the items, but still on the ListView component. I've change the ListView property "HideSelection" to false, but that only works when the focus is changed to another component; not when the user clicks on the ListView itself. Thanks!

推荐答案

这是一些你通常不应该修正。用户点击某处故意,这很可能是因为她的希望的取消选择的项目。如果是无意的她就会明白发生了什么,并知道如何纠正它。给标准控件不规范的行为往往只是迷惑用户。

This is something you normally shouldn't fix. The user clicked somewhere intentionally, that might well be because she wanted to deselect an item. If it was unintentional then she'll understand what happened and know how to correct it. Giving standard controls non-standard behavior tends to only confuse the user.

但你可以解决它。你需要prevent从看到的点击本地ListView控件。这需要重写的WndProc()方法,并检查发生点击。添加一个新类到您的项目并粘贴如下所示的code。编译。从工具箱的上方新的控制窗体上。

But you can fix it. You'll need to prevent the native ListView control from seeing the click. That requires overriding the WndProc() method and checking where the click occurred. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto the form.

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

class MyListView : ListView {
  protected override void WndProc(ref Message m) {
    if (m.Msg == 0x201 || m.Msg == 0x203) {  // Trap WM_LBUTTONDOWN + double click
      var pos = new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);
      var loc = this.HitTest(pos);
      switch (loc.Location) {
        case ListViewHitTestLocations.None:
        case ListViewHitTestLocations.AboveClientArea:
        case ListViewHitTestLocations.BelowClientArea:
        case ListViewHitTestLocations.LeftOfClientArea:
        case ListViewHitTestLocations.RightOfClientArea:
          return;  // Don't let the native control see it
      }
    }
    base.WndProc(ref m);
  }
}

这篇关于如何保持一个项目选择? - 列表显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆