.net – 如何从ListView控件中的各个项目中删除复选框?

前端之家收集整理的这篇文章主要介绍了.net – 如何从ListView控件中的各个项目中删除复选框?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个列表’Name’,’Expected’,’Total’的ListView,我想在最后添加另一个列’Recount’.仅当“预期”值大于“总计”值时,“重新计数”列才会有一个复选框.

到目前为止,我已经获得了带有列的ListView,并且可以在左侧添加一个复选框,但是该复选框不在列标题下(尽管我可以放置另一个没有值的列来解决这个问题)和这是所有的记录.

任何人都有什么想法我还能做些什么?

解决方法

这实际上相对简单,只要您愿意忍受P / Invoke的苦差事来访问本机 Windows控件中内置的功能,但.NET FW不会公开.

我演示了in my answer here如何使用TreeView控件完成同样的事情,并考虑到ListView与TreeView的相似之处,使用ListView以非常相同的方式完成这一点并不特别令人惊讶.

这是所有必需的代码(确保您已为System.Runtime.InteropServices命名空间添加了Imports声明):

' P/Invoke declarations
Private Const LVIF_STATE As Integer = &H8
Private Const LVIS_STATEIMAGEMASK As Integer = &HF000
Private Const LVM_FIRST As Integer = &H1000
Private Const LVM_SETITEM As Integer = LVM_FIRST + 76

<StructLayout(LayoutKind.Sequential,Pack:=8,CharSet:=CharSet.Auto)> _
Private Structure LVITEM
   Public mask As Integer
   Public iItem As Integer
   Public iSubItem As Integer
   Public state As Integer
   Public stateMask As Integer
   <MarshalAs(UnmanagedType.LPTStr)> _
   Public lpszText As String
   Public cchTextMax As Integer
   Public iImage As Integer
   Public lParam As IntPtr
End Structure

<DllImport("user32.dll",CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr,ByVal Msg As Integer,ByVal wParam As IntPtr,ByRef lParam As LVITEM) As IntPtr
End Function

''' <summary>
''' Hides the checkBox for the specified item in a ListView control.
''' </summary>
Private Sub HideCheckBox(ByVal lvw As ListView,ByVal item As ListViewItem)
   Dim lvi As LVITEM = New LVITEM()
   lvi.iItem = item.Index
   lvi.mask = LVIF_STATE
   lvi.stateMask = LVIS_STATEIMAGEMASK
   lvi.state = 0
   SendMessage(lvw.Handle,LVM_SETITEM,IntPtr.Zero,lvi)
End Sub

然后你可以简单地调用上面的方法

Private Sub btnHideCheckForSelected_Click(ByVal sender As Object,ByVal e As EventArgs)
   ' Hide the checkBox next to the currently selected ListViewItem
   HideCheckBox(myListView,myListView.SelectedItems(0))
End Sub

生成看起来有点像这样的东西(点击番茄和黄瓜项目的“隐藏检查”按钮后):

猜你在找的HTML相关文章