サブアイテムがクリックされたときに行全体を選択したい場合は、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();
}