Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove the checkboxes from individual items in a ListView control?

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?

like image 561
Harold Avatar asked May 09 '11 08:05

Harold


People also ask

What are the 4 main modes in list view control?

The control has four view modes: LargeIcon, SmallIcon, List, and Details.

How do you control list view?

The ListView control is used to display a list of items. Along with the TreeView control, it allows you to create a Windows Explorer like interface. Let's click on a ListView control from the Toolbox and place it on the form. The ListView control displays a list of items along with icons.

What is a ListView control vb net?

The ListView Controls are used to display a collection of items in the Windows Forms. It uses one of the view lists, such as LargeIcon, SmallIcon, Details, List, and Tile. Furthermore, the ListView allows the user to add or remove icons from the ListView Control.

What is ListView control in C#?

C# ListView control provides an interface to display a list of items using different views including text, small images, and large images.


1 Answers

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

     Sample ListView, with checkboxes removed from individual items

like image 69
Cody Gray Avatar answered Sep 28 '22 05:09

Cody Gray