[點(diǎn)晴永久免費(fèi)OA]C# 雙擊ListView出現(xiàn)編輯框可編輯,回車(chē)確認(rèn)
1.
2.
[DllImport("user32")] 3.
public static extern int GetScrollPos(int hwnd, int nBar); 4.
5.
private TextBox txtInput; 6.
7.
//獲取點(diǎn)擊項(xiàng)的位置 8.
private void lViewPersonWork_MouseDoubleClick(object sender, MouseEventArgs e) 9.
{ 10. try 11. { 12. ListViewItem item = this.lViewPersonWork.GetItemAt(e.X, e.Y); 13. 14. //找到文本框 15. Rectangle rect = item.GetBounds(ItemBoundsPortion.Entire); 16. int StartX = rect.Left; //獲取文本框位置的X坐標(biāo) 17. int ColumnIndex = 0;
//文本框的索引 18. 19. //獲取列的索引 20. //得到滑塊的位置 21. int pos = GetScrollPos(this.lViewPersonWork.Handle.ToInt32(), 0); 22. foreach (ColumnHeader Column in lViewPersonWork.Columns) 23. { 24. if (e.X + pos >= StartX + Column.Width) 25. { 26. StartX += Column.Width; 27. ColumnIndex += 1; 28. } 29. } 30. 31. if (ColumnIndex < this.lViewPersonWork.Columns.Count - 1) 32. { 33. return; 34. } 35. 36. this.txtInput = new TextBox(); 37. 38.
//locate the txtinput and hide it.
txtInput為TextBox 39. this.txtInput.Parent = this.lViewPersonWork; 40. 41. //begin edit 42. if (item != null) 43. { 44. rect.X = StartX; 45. rect.Width = this.lViewPersonWork.Columns[ColumnIndex].Width;
//得到長(zhǎng)度和ListView的列的長(zhǎng)度相同 46. this.txtInput.Bounds = rect; 47. this.txtInput.Multiline = true; 48. //顯示文本框 49. this.txtInput.Text = item.SubItems[ColumnIndex].Text; 50. this.txtInput.Tag =
item.SubItems[ColumnIndex]; 51. this.txtInput.KeyPress += new
KeyPressEventHandler(txtInput_KeyPress); 52. this.txtInput.Focus(); 53. } 54. } 55. catch (Exception ex) 56. { 57. 58. } 59.
} 60. 61.
//回車(chē)保存內(nèi)容 62.
private void txtInput_KeyPress(object sender, KeyPressEventArgs e) 63.
{ 64. try 65. { 66. if ((int)e.KeyChar == 13) 67. { 68. if (this.txtInput != null) 69. { 70.
ListViewItem.ListViewSubItem lvst = (ListViewItem.ListViewSubItem)this.txtInput.Tag; 71. 72. lvst.Text = this.txtInput.Text; 73. 74. this.txtInput.Dispose(); 75. } 76. } 77. } 78. catch (Exception ex) 79. { 80. 81. } 82.
} 83. 84.
//釋放文本框內(nèi)容 85. private void lViewPersonWork_selectedIndexChanged(object sender, EventArgs e) 86.
{ 87. try 88. { 89. if (this.txtInput != null) 90. { 91. if (this.txtInput.Text.Length > 0) 92. { 93.
ListViewItem.ListViewSubItem lvst = (ListViewItem.ListViewSubItem)this.txtInput.Tag; 94. 95. lvst.Text = this.txtInput.Text; 96. } 97. 98. this.txtInput.Dispose(); 99. } 100.
} 101.
catch (Exception ex) 102.
{ 103.
104.
} 105.
} 該文章在 2022/12/22 20:41:39 編輯過(guò) |
關(guān)鍵字查詢(xún)
相關(guān)文章
|