如果要在單擊子項目時選擇整行,請嘗試使用FullRowSelect
屬性ListView
。 爲了處理一個子項雙擊,試試這個:
private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo hit = listView1.HitTest(e.Location);
// Use hit.Item
// Use hit.SubItem
}
如果你想允許最終用戶在列表視圖編輯子項的文字,恐怕最簡單的方法是使用Grid控件。另一種方法是嘗試這樣的事情:
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();
}
這是什麼平臺,wpf,winform,其他? – har07
平臺是winform –
雖然您可以使用ListView控件執行此操作,但設置起來很麻煩。你有沒有想過使用Grid控件呢? –