简体   繁体   中英

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. 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

我想要点击的listview项

Try this Code.

Handle the ListView_MouseUp event and write down the following code.

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. 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.

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 . We can get the Item directly from location by using GetItemAt(x,y) method. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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