ListView在C#.net中单击listview的空白时失去焦点 [英] ListView Losing focus while clicking on blank space of listview in C#.net
问题描述
大家好,
我有一个列表视图.显示其项目,并单击每个项目都没有问题.但是,当该列表视图停靠(填充)到面板中时.当我单击列表视图的空白区域时(我相信),我的意思是该列表视图在该面板中的空白行.我当前的selectedItem将更改为false.我不知道单击列表视图的空白区域的事件处理.那我可以禁用它...
预先感谢.
Hi all,
I have a listview. Its items are displayed and click on each item is not a problem. But while that listview is docked (fill) into a panel. When I click on an empty area of a listview (I believe), I means an empty row of that listview in that panel. My current selectedItem will be changed to false. I dont know what event handle that click at an empty area of a listview. Then I can disable it...
Thanks in advance.
推荐答案
嗨阿普尔瓦,
这是一种可能的解决方案:忽略在ListView的客户端区域中单击鼠标的操作.
Hi Apurva,
This is one possible solution for this: Ignore the mouse-click in the client area of the ListView.
using System;
using System.Windows.Forms;
using System.Drawing;
namespace ListViewFocusDemo
{
static class Program
{
class MyListView : ListView
{
protected override void WndProc(ref Message msg)
{
// Ignore mouse messages not in the client area
if (msg.Msg >= 0x201 && msg.Msg <= 0x209)
{
Point pointMousePos = new Point(msg.LParam.ToInt32() & 0xffff, msg.LParam.ToInt32() >> 16);
ListViewHitTestInfo lvhti = this.HitTest(pointMousePos);
switch (lvhti.Location)
{
case ListViewHitTestLocations.AboveClientArea:
case ListViewHitTestLocations.BelowClientArea:
case ListViewHitTestLocations.LeftOfClientArea:
case ListViewHitTestLocations.RightOfClientArea:
case ListViewHitTestLocations.None:
return;
}
}
base.WndProc(ref msg);
}
}
static void Main()
{
// Create a ListView
MyListView listview = new MyListView();
listview.Dock = DockStyle.Fill;
listview.View = View.Details;
listview.CausesValidation = false;
// ... add some columns
listview.Columns.Add("Name");
listview.Columns.Add("Value");
// ... and dummy items
listview.Items.Add(new ListViewItem(new string[] { "IsListView", "Yes" }));
listview.Items.Add(new ListViewItem(new string[] { "IsDoingWhatIWant", "No"}));
Label label = new Label();
label.Dock = DockStyle.Bottom;
listview.SelectedIndexChanged += delegate(object obj, EventArgs ea)
{
label.Text = String.Format("Selected Index = {0}",
listview.SelectedIndices.Count > 0 ? listview.SelectedIndices[0] : -1);
};
// Create a Test-Form
Form form = new Form();
form.Controls.Add(listview);
form.Controls.Add(label);
Application.Run(form);
}
}
}
另一个解决方案是,如果发生取消选择(SelectedIndexChanged,ItemSelectionChanged),则重新创建选择.
Another solution would be to recreate the selection if the unselection occurs (SelectedIndexChanged, ItemSelectionChanged).
listView1_SelectedIndeChaned(object sender, EventArgs e)
{
try
{
// your code here
}
catch
{
// leave empty to catch error
}
}
这篇关于ListView在C#.net中单击listview的空白时失去焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!