简体   繁体   English

C#.net使listview项目可单击

[英]C# .net Make listview items clickable

I am trying to click on items in the listview. 我正在尝试单击列表视图中的项目。 Is there a way to do this? 有没有办法做到这一点? I would like to click on each one individually (and from that action, write to hardware, change the on/off indication (bold font), and update the hex value for that register. 我想单独单击每个按钮 (然后从该操作中写入硬件,更改开/关指示(粗体),并更新该寄存器的十六进制值。

I've found how to click on the columns via msdn, but I cannot figure out how to click on the items. 我已经找到了如何通过msdn单击列,但是我无法弄清楚如何单击项目。 http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.oncolumnclick http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.oncolumnclick

I don't have much experience, but it seems like I should be able to interface from Control.Onclick somehow: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onclick(v=vs.110).aspx 我没有太多经验,但是似乎我应该可以通过Control.Onclick进行界面交互: http : //msdn.microsoft.com/zh-cn/library/system.windows.forms.control.onclick( v = vs.110)的.aspx

我想要点击的listview项

Try this Code. 试试这个代码。

Handle the ListView_MouseUp event and write down the following code. 处理ListView_MouseUp事件并写下以下代码。

private void listView1_MouseUp(object sender, MouseEventArgs e)
{
    if (listView1.View != View.Details)
        return; 
    int rowIndex = getRowIndex(e.Location);
    if (rowIndex == -1)
        return ;
    int columnIndex = getColumnIndex(e.Location);
    if (columnIndex > -1)
        OnCellClick(rowIndex, columnIndex);
}

Now, create the method to get the Column Index according to mouse position. 现在,创建根据鼠标位置获取“列索引”的方法。 This method will find the column index according to mouse position. 此方法将根据鼠标位置找到列索引。

private int getColumnIndex(Point p)
{
    Rectangle r = Rectangle.Empty;
    r = Rectangle.Empty;
    for (int i = 0; i < listView1.Columns.Count; i++)
    {
        r = new Rectangle(r.X + r.Width, 0, listView1.Columns[i].Width, listView1.Height);
        if (r.Contains(p))            
            return i;
    }
    return -1;
}

Create another method to get the Row Index according to mouse position. 创建另一个方法以根据鼠标位置获取行索引。 This method will find the row index according to mouse position and it will set the FocusedItem property of ListView. 此方法将根据鼠标位置找到行索引,并将设置ListView的FocusedItem属性。 So, you can get the focus on the clicked item. 因此,您可以将重点放在单击的项目上。

private int getRowIndex(Point p)
{
    Rectangle r = Rectangle.Empty;
    for (int i = 0; i < listView1.Items.Count; i++)
    {
        Rectangle r1 = listView1.GetItemRect(i);
        r = new Rectangle(0, r1.Top, listView1.Width, r1.Height);
        if (r.Contains(p))
        {
            listView1.FocusedItem = listView1.Items[i];
            return i;
        }
    }
    return -1;
}

This is the method that you need to handle. 这是您需要处理的方法。 Your code to get the value from ListView cell will be written in this method. 您将从ListView单元格获取值的代码将以这种方法编写。

private void OnCellClick(int RowIndex, int ColumnIndex)
{

    MessageBox.Show("Column : " + ColumnIndex.ToString() + ", Row: " + RowIndex.ToString());
}

I know there is no need to create method to get the RowIndex . 我知道不需要创建方法来获取RowIndex We can get the Item directly from location by using GetItemAt(x,y) method. 我们可以使用GetItemAt(x,y)方法直接从位置获取Item。 But, this method only works when you have set the FullRowSelect to true otherwise you will get null when the mouse position is on subitem. 但是,仅当您将FullRowSelect设置为true时,此方法才有效,否则当鼠标位置在子项目上时您将获得null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM