[点晴永久免费OA]C# 双击ListView出现编辑框可编辑,回车确认
当前位置:点晴教程→点晴OA办公管理信息系统
→『 经验分享&问题答疑 』
1.
2.
[DllImport("user32")] 3.
public static extern int GetScrollPos(int hwnd, int nBar); 4.
5.
private TextBox txtInput; 6.
7.
//获取点击项的位置 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坐标 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;
//得到长度和ListView的列的长度相同 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.
//回车保存内容 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.
//释放文本框内容 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 编辑过 |
关键字查询
相关文章
正在查询... |