如果要在单击子项时选择整行,请尝试使用FullRowSelect
. ListView
要处理双击子项,请尝试以下操作:
private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo hit = listView1.HitTest(e.Location);
// Use hit.Item
// Use hit.SubItem
}
如果您想让最终用户在列表视图中编辑子项的文本,恐怕最简单的方法是使用网格控件。另一种方法是尝试这样的事情:
private readonly TextBox txt = new TextBox { BorderStyle = BorderStyle.FixedSingle, Visible = false };
public Form1()
{
InitializeComponent();
listView1.Controls.Add(txt);
listView1.FullRowSelect = true;
txt.Leave += (o, e) => txt.Visible = false;
}
private void listView1_MouseDoubleClick(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;
txt.Bounds = new Rectangle(rowBounds.Left + leftMargin, rowBounds.Top, rowBounds.Width - leftMargin - 1, rowBounds.Height);
txt.Text = hit.SubItem.Text;
txt.SelectAll();
txt.Visible = true;
txt.Focus();
}