如何删除ListView控件从单个项目的复选框

如何删除ListView控件从单个项目的复选框

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

问题描述

我有列姓名一个ListView,预期,总,我想在末尾加上另一列说:选票风波。在叙事列将最好有一个复选框只有当预期值比总计值。

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.

到目前为止,我还得到了与列ListView,并且可以在左侧添加一个复选框,但复选框不是列标题下(虽然我也许可以把另一列在那里没有值来解决这一点),它是在所有的记录。

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?

推荐答案

这其实实现起来比较简单,只要你愿意忍受的P / Invoke的苦差事访问内置的原生Windows控制功能,但不是由.NET FW暴露出来。

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.

我演示了in在这里我的答案的是如何同样的事情可以用一个TreeView控件来实现,并考虑如何类似一个ListView是一个TreeView,它不应该是特别令人惊讶的是这可以在很多的相同的方式完成了ListView控件。

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.

下面是所有所需的code(请确保您已经添加了一个进口声明为了System.Runtime.InteropServices 命名空间):

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):

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;

     

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

07-23 16:37