如何从ListView控件中删除单个项目的复选框? [英] How can I remove the checkboxes from individual items in a ListView control?

查看:184
本文介绍了如何从ListView控件中删除单个项目的复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView与列'名称','期望','总',我想添加另一个列Recount结束。只要Expected值大于Total值,Recount列就会有一个复选框。



到目前为止,并且可以在左侧添加一个复选框,但是该复选框不在列标题下(虽然我可以放置另一个没有值的列来解决这个问题),并且它在所有记录上。 / p>

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

解决方案

实际上是相对简单的实现,只要你愿意忍受P / Invoke的苦恼来访问本地Windows控件内置的功能,但不是由.NET FW暴露。



我在此演示我的回答如何这个完全相同的事情可以用一个TreeView控件,并考虑ListView是如何类似于一个TreeView,应该不是特别令人惊讶,这可以以非常相同的方式与一个ListView。



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

 'P / Invoke声明
Private Const LVIF_STATE As整数=& H8
私人字段LVIS_STATEIMAGEMASK As Integer =& HF000
私人字段LVM_FIRST As Integer =& H1000
私人字段LVM_SETITEM As Integer = LVM_FIRST + 76

< StructLayout(LayoutKind.Sequential,Pack:= 8,CharSet:= CharSet.Auto)> _
私有结构LVITEM
公共掩码作为整数
公共iItem作为整数
公共iSubItem作为整数
公共状态作为整数
公共stateMask作为整数
< MarshalAs(UnmanagedType.LPTStr)> _
Public lpszText As String
public cchTextMax As Integer
Public iImage As Integer
Public lParam As IntPtr
结构结构

< DllImport (user32.dll,CharSet:= CharSet.Auto)> _
私有共享函数SendMessage(ByVal hWnd As IntPtr,ByVal Msg As Integer,ByVal wParam As IntPtr,ByRef lParam As LVITEM)As IntPtr
End Function

''' ; summary>
'''隐藏ListView控件中指定项目的复选框。
'''< / 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)
'隐藏当前选中的ListViewItem旁边的复选框
HideCheckBox(myListView,myListView.SelectedItems(0) )
End Sub

生成看起来有点像这样的东西检查按钮):



     


I have a ListView with the columns 'Name', 'Expected', 'Total', and I want to add another column saying 'Recount' at the end. The 'Recount' column will ideally have a checkbox only if the 'Expected' value is larger than the 'Total' value.

So far I have got the ListView with columns and can add a check box on the left hand side, but that check box is not under a column heading (though I can probably put another column with no values in there to work around that) and it is on all of the records.

Anyone have any ideas what else I can do?

解决方案

This is actually relatively simple to implement, provided that you're willing to endure the drudgery of P/Invoke to access functionality built into the native Windows control, but not exposed by the .NET FW.

I demonstrate in my answer here how this exact same thing can be done with a TreeView control, and considering how similar a ListView is to a TreeView, it should not be particularly surprising that this can be done in very much the same way with a ListView.

Here's all the code that is required (make sure that you've added an Imports declaration for the System.Runtime.InteropServices namespace):

' 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

And then you can simply call the above method like this:

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

Producing something that looks a bit like this (after clicking the "Hide Check" button for both the tomato and the cucumber items):

     

这篇关于如何从ListView控件中删除单个项目的复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆