gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 13:54:42 26 4
gpt4 key购买 nike

我有一个带有“名称”,“期望”,“总计”列的ListView,我想在末尾添加另一列“重新计数”。理想情况下,仅当“期望”值大于“总计”值时,“重新计数”列才会有一个复选框。

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

有人有什么想法我还能做什么?

最佳答案

如果您愿意忍受P/Invoke的苦恼以访问内置于 native Windows控件中的功能,但.NET FW不公开此功能,则这实际上相对容易实现。

我向in my answer here演示了如何使用TreeView控件完成此完全相同的操作,并考虑到ListView与TreeView有多么相似,因此可以非常相似地使用ListView来完成此操作也就不足为奇了。

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

' 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

制作看起来像这样的东西(单击番茄和 cucumber 项目的“隐藏检查”按钮之后):

关于.net - 如何从ListView控件的单个项目中删除复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5934422/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com