C# 双击 ListView 中的一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12872740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DoubleClick on a row in ListView
提问by whoah
Is there any possibility to get a value of doubleclicked row in ListView? I registered an event:
是否有可能在 ListView 中获得双击行的值?我注册了一个事件:
private void lvLista_DoubleClick(object sender, EventArgs e)
{
MessageBox.Show(lvLista.SelectedItems.ToString());
}
But on message, when i doubleclick some row in listview i get:
但是在消息中,当我双击列表视图中的某行时,我得到:
System.Windows.Forms.ListView+SelectedListViewItemCollection
System.Windows.Forms.ListView+SelectedListViewItemCollection
What is more, I have got 2 columns in listview:
更重要的是,我在列表视图中有 2 列:
lvLista.Columns.Add("ID");
lvLista.Columns.Add("Tilte");
And i want to show in messagebox the "ID" of doubleclicked row.
How to do it? How to get a values from this event?
我想在消息框中显示双击行的“ID”。
怎么做?如何从此事件中获取值?
采纳答案by Daniil Grankin
Try this:
尝试这个:
private void lvLista_DoubleClick(object sender, EventArgs e)
{
MessageBox.Show(lvLista.SelectedItems[0].SubItems[0].Text);
}
回答by XIVSolutions
If you handle the MouseDownand/or MouseDoubleClickevents of the ListView control, and use the HitTestmethod to determine the target of the mouse action, you will know which item has been double clicked. This is also a good means to determine if NO item was clicked (for example, clicking on the empty area in a partially filled list.
如果你处理ListView控件的MouseDown和/或MouseDoubleClick事件,并使用该HitTest方法来确定鼠标动作的目标,你就会知道哪个项目被双击了。这也是确定是否单击了 NO 项的好方法(例如,单击部分填充列表中的空白区域。
The following code will display the clicked item in a textbox if a single click occurs, and will pop up a message box with the name of the double-clicked item if a double click occurs.
下面的代码将在发生单击时在文本框中显示被单击的项目,如果发生双击,将弹出一个带有双击项目名称的消息框。
If the click or double click occur in an area of the list view not populated by an item, the text box or message box inform yopu of that fact.
如果单击或双击发生在列表视图的未由项目填充的区域中,则文本框或消息框会通知您该事实。
This is a trivial example, and depending on your needs, you will have to mess with it a little.
这是一个微不足道的例子,根据您的需要,您将不得不稍微处理一下。
UPDATE: I added some code which clears the SelectedItems property of the Listview control when an empty area of the list is clicked or double-clicked.
更新:我添加了一些代码,当单击或双击列表的空白区域时,这些代码会清除 Listview 控件的 SelectedItems 属性。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listView1.MouseDown += new MouseEventHandler(listView1_MouseDown);
listView1.MouseDoubleClick += new MouseEventHandler(listView1_MouseDoubleClick);
this.Load += new EventHandler(Form1_Load);
}
void Form1_Load(object sender, EventArgs e)
{
this.SetupListview();
}
private void SetupListview()
{
ListView lv = this.listView1;
lv.View = View.List;
lv.Items.Add("John Lennon");
lv.Items.Add("Paul McCartney");
lv.Items.Add("George Harrison");
lv.Items.Add("Richard Starkey");
}
void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo info = listView1.HitTest(e.X, e.Y);
ListViewItem item = info.Item;
if (item != null)
{
MessageBox.Show("The selected Item Name is: " + item.Text);
}
else
{
this.listView1.SelectedItems.Clear();
MessageBox.Show("No Item is selected");
}
}
void listView1_MouseDown(object sender, MouseEventArgs e)
{
ListViewHitTestInfo info = listView1.HitTest(e.X, e.Y);
ListViewItem item = info.Item;
if (item != null)
{
this.textBox1.Text = item.Text;
}
else
{
this.listView1.SelectedItems.Clear();
this.textBox1.Text = "No Item is Selected";
}
}
}
回答by Gary Huckabone
Thanks; this is what I needed. I thought I'd also mention one could set up the local info variable more generally as:
谢谢; 这就是我需要的。我想我还会提到可以更一般地将本地信息变量设置为:
ListViewHitTestInfo info = ((ListView)sender).HitTest(e.X, e.Y);
回答by Shawn
Try this
尝试这个
private void listView1_MouseClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo hit = listView1.HitTest(e.Location);
Rectangle rowBounds = hit.SubItem.Bounds;
Rectangle labelBounds = hit.Item.GetBounds(ItemBoundsPortion.Label);
int leftMargin = labelBounds.Left - 1;
string x = hit.Item.Text;
}
回答by Martin Riddar
Since the accepted answer didn't help me i thought that I would share my solution to the same problem: getting data from a specific column in a listview in the double click event.
由于接受的答案对我没有帮助,我认为我会分享我对同一问题的解决方案:从双击事件中列表视图中的特定列获取数据。
The following line returns the data of the second column in the row that I've double clicked on as a string:
以下行返回我双击的行中第二列的数据作为字符串:
private void listViewOutput_DoubleClick(object sender, EventArgs e)
{
string content = listViewOutput.Items[listViewOutput.SelectedIndices[0]].SubItems[1].Text
}
回答by hgjdfhjfhjj
I know this thread is old but nobody here answered the question properly in my opinion. For those in the future, try this, from MSDN:
我知道这个帖子很旧,但在我看来,这里没有人正确回答了这个问题。对于将来的人,请从MSDN尝试此操作:
this.myListView.Activation = System.Windows.Forms.ItemActivation.TwoClick;
this.myListView.ItemActivate += new
System.EventHandler(this.myListView_ItemClick);

