473,387 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,387 developers and data experts.

Putting Checkboxes in any cell in listview

!NoItAll
297 100+
I needed to put checkboxes in several cells in a listview control. The way the listview control works you can really only have an "official" checkbox in the first cell, and attempts to put them anywhere else are thwarted at just about every turn.
So I developed a simple approach using WingDings. The WingDings font has a checked and unchecked box. In this project I place the unchecked box from WingDings centered in any cell I want a checkbox in, and clicking on the cell will result in toggling between the WingDing checked and unchecked character.
Chr(168) is checked and Chr(254) is unchecked.
(float your mouse over the image below to see the whole thing....)


The trick is to set the font in specific cells to WingDings.
First we have to create a font object:
Expand|Select|Wrap|Line Numbers
  1. Dim myCheckFont As New System.Drawing.Font("Wingdings", 12, FontStyle.Regular)
  2.  
Then we need to assign it to the specific cell in which we want to have a checkbox. You set the font as you create the cells. So in the following example I am creating a table of 16 rows in a listview instantiated with 3 columns. Each time we add an item to column 1 we set the font to MyCheckFont (the WingDings font as assigned above).
Expand|Select|Wrap|Line Numbers
  1.         With ListView1
  2.             For I As Integer = 0 To 15
  3.                 .Items.Add("test" & I.ToString)
  4.                 .Items(I).UseItemStyleForSubItems = False
  5.                 .Items(I).SubItems.Add(Chr(168))
  6.                 .Items(I).SubItems.Item(1).ForeColor = Color.DarkRed
  7.                 .Items(I).SubItems.Item(1).Font = myCheckFont
  8.                 ReDim bOWCheck(I)
  9.             Next I
  10.         End With
  11.  
The trick here is to make sure we set UseItemSylteForSubItems to FALSE. If we do not do that then we can not have separate fonts for different columns. That's a real nice piece of esoterica!
Finaly the only thing you then need to manage is to catch the clicks in the particular column and row so you can then toggle between the two WingDing characters that represent the checked and unchecked boxes.
This too is steeped in some esoteric functionality. Why MS didn't give us a simple way to get the cell that was clicked is beyond me - but they didn't so we have to do it the hard way.
In the listview MouseUp event I have the following code
Expand|Select|Wrap|Line Numbers
  1.     Private Sub ListView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseUp
  2.         Dim cellLoc As MyCell
  3.         cellLoc = WhichCell(ListView1, e.X, e.Y)
  4.         If cellLoc.Col = 2 Then
  5.             If bOWCheck(cellLoc.Row) = True Then
  6.                 ListView1.Items(cellLoc.Row).SubItems(cellLoc.Col - 1).Text = Chr(168)
  7.                 bOWCheck(cellLoc.Row) = False
  8.             Else
  9.                 ListView1.Items(cellLoc.Row).SubItems(cellLoc.Col - 1).Text = Chr(254)
  10.                 bOWCheck(cellLoc.Row) = True
  11.             End If
  12.  
  13.         End If
  14. End Sub
  15.  
Notice the WhichCell function - that's doing the esoteric work...
Expand|Select|Wrap|Line Numbers
  1.     Private Function WhichCell(ByVal lvw As ListView, ByVal X As Integer, ByVal Y As Integer) As MyCell
  2.  
  3.         Dim colstart As Integer = 0
  4.         Dim colend As Integer = 0
  5.         Dim xCol As Integer
  6.  
  7.         For xCol = 0 To (ListView1.Columns.Count - 1)
  8.             colend = colend + ListView1.Columns(xCol).Width
  9.             If colstart <= X And X <= colend Then
  10.                 WhichCell.Col = xCol + 1
  11.                 Exit For
  12.             End If
  13.             colstart = colstart + ListView1.Columns(xCol).Width
  14.         Next
  15.  
  16.         WhichCell.Row = ListView1.FocusedItem.Index
  17.         Return WhichCell
  18.  
  19.     End Function
  20.  
Finally - to keep track of the checkboxes I have a boolean array: bOWCheck() that I index to the current row. If you have multiple column just add another dimension to the array. This is where we persist the state of the checkbox for each cell. Notice it is toggled in the listview MouseUp event along with toggling the WingDing checked and unchecked characters.

There are possibly several better ways to do this. Improvements are certainly welcome.
I've attached a zip file with a VS2008 VB.NET project you can play with.
Attached Files
File Type: zip lstViewChecks.zip (69.0 KB, 3891 views)
Jan 21 '10 #1
7 39167
This is a really neat trick. Well done for working it out and sharing.
Jan 29 '15 #2
Just came across this, and was exactly what I was looking for. Very clever and easy to implement. Thanks for posting this!
Feb 10 '19 #3
IronRazer
83 64KB
Kind of handy if you really want to use a ListView but, it is much easier to just use a DataGridView which can be made to look just like a ListView and can easily have a checkbox column on any column(s) you want. 8)
Feb 11 '19 #4
!NoItAll
297 100+
It's been a long time since I wrote this post. There was a reason I didn't use the dataGridView at the time. It might not have been a good reason - but it was a reason... ;-)
Feb 11 '19 #5
Hey guys - I am aware that a DataGridView could be used, and in fact use that quite a bit. The reason this came in handy is that I had an existing ListView and needed to add checkbox columns to it "after the fact". Rather than rewriting that part of the interface from scratch with the DGV, it was quite handy to add these checkbox columns to the existing LV.
Feb 11 '19 #6
amin grf
2 2Bits
Thanks for sharing, I've added an option to select only one item at a time, in Mouse_Up event.
Expand|Select|Wrap|Line Numbers
  1.  Private Sub ListView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseUp
  2.         Dim cellLoc As MyCell
  3.         cellLoc = WhichCell(ListView1, e.X, e.Y)
  4.  
  5.         If cellLoc.Col = 2 Then
  6.             'check once
  7.             ListView1.Items(check1_indx).SubItems(1).Text = Chr(168)
  8.             bOWCheck(cellLoc.Row) = False
  9.             If bOWCheck(cellLoc.Row) = False Then
  10.                 ListView1.Items(cellLoc.Row).SubItems(cellLoc.Col - 1).Text = Chr(254)
  11.                 bOWCheck(cellLoc.Row) = True
  12.                 check1_indx = ListView1.Items(cellLoc.Row).Index
  13.                 Label1.Text = "checked: " & check1_indx
  14.             End If
  15.  
  16.  
  17.             ''check multiple
  18.             'If bOWCheck(cellLoc.Row) = True Then
  19.             '    ListView1.Items(cellLoc.Row).SubItems(cellLoc.Col - 1).Text = Chr(168)
  20.             '    bOWCheck(cellLoc.Row) = False
  21.             'Else
  22.             '    'uncheck
  23.             '    ListView1.Items(cellLoc.Row).SubItems(cellLoc.Col - 1).Text = Chr(254)
  24.             '    bOWCheck(cellLoc.Row) = True
  25.             '    check1_indx = ListView1.Items(cellLoc.Row).Index
  26.             '    Label1.Text = "checked: " & check1_indx
  27.             'End If
  28.  
  29.         End If
  30.  
  31.  
  32.         Label1.Focus()
  33.         ListView1.Select()
  34.     End Sub
May 17 '23 #7
amin grf
2 2Bits
Previous code when listview sort order change also change it's index, so some items will not be checked or unchecked. In this code I used Selected item. and added a list to store checked items.
Expand|Select|Wrap|Line Numbers
  1.     Dim checklist As New List(Of String)()
  2.     Private Sub lvwBooks_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvwBooks.MouseUp
  3.         Try
  4.             If Not lvwBooks.Items.Count = 0 Then
  5.                 Dim cellLoc As MyCell
  6.                 Dim check_chr As String = Chr(254)
  7.                 Dim uncheck_chr As String = Chr(168)
  8.                 Dim CheckdItems As String
  9.                 Dim select_once As Boolean = True
  10.                 cellLoc = WhichCell(lvwBooks, e.X, e.Y)
  11.                 If cellLoc.Col = 1 Or cellLoc.Col = 2 Or cellLoc.Col = 3 Then
  12.                     If select_once = True Then
  13.                         'check_uncheck once
  14.                         lvwBooks.MultiSelect = False
  15.                         'uncheck 
  16.                         Dim unCheckItem As ListViewItem = lvwBooks.FindItemWithText(check_chr, True, 0, False)
  17.                         If Not unCheckItem Is Nothing Then
  18.                             unCheckItem.SubItems(2).Text = uncheck_chr
  19.                             checklist.Remove(unCheckItem.SubItems(1).Text)
  20.                         End If
  21.  
  22.                         For Each item In lvwBooks.SelectedItems
  23.                             If item.SubItems(2).Text = check_chr Then
  24.                                 'uncheck
  25.                                 item.SubItems(2).Text = uncheck_chr
  26.                                 checklist.Remove(item.SubItems(1).Text)
  27.                             Else
  28.                                 'check
  29.                                 item.SubItems(2).Text = check_chr
  30.                                 checklist.Add(item.SubItems(1).Text)
  31.                             End If
  32.  
  33.                             Exit For
  34.                         Next
  35.                     Else
  36.                         'check_uncheck multiple
  37.                         lvwBooks.MultiSelect = True
  38.                         For Each item In lvwBooks.SelectedItems
  39.                             If item.SubItems(2).Text = check_chr Then
  40.                                 item.SubItems(2).Text = uncheck_chr
  41.                                 checklist.Remove(item.SubItems(1).Text)
  42.                             Else
  43.                                 item.SubItems(2).Text = check_chr
  44.                                 checklist.Add(item.SubItems(1).Text)
  45.                             End If
  46.                         Next
  47.  
  48.                     End If
  49.  
  50.                 End If
  51.                 CheckdItems = ""
  52.                 For Each c In checklist
  53.                     CheckdItems += c & " "
  54.                 Next
  55.                 If checklist.Count = 0 Then
  56.                     Label1.Text = "No Item Checked"
  57.                 Else
  58.                     Label1.Text = "Checked: " & CheckdItems
  59.                 End If
  60.  
  61.                 'lvwBooks.Select()
  62.             End If
  63.         Catch ex As Exception
  64.  
  65.              MsgBox(ex.Message)
  66.  
  67.         End Try
  68.     End Sub
May 19 '23 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: andrewcw | last post by:
I have a simple winform with the following code. But although I can read back the info, the display fails to provide the text or the cell background color changes. private void ListViewBroke()...
1
by: Alberto | last post by:
Does anybody know if in the next version of visual studio will be a ListView who allows CheckBoxes in all the columns? Thank you
1
by: redneon | last post by:
Is it possible to get the Handle property for a particular cell in a ListView? I'm wanting to turn a cell into a progress bar rather than just displaying text. If it's possible to get the Handle I...
0
by: Yaron | last post by:
Hi, I have a ListView that is shown in Details mode, and the Checkboxes property is set to true. My problem is that I dont want the checkboxes being activated when items in the ListView are...
0
by: Yaron | last post by:
Hi, I have a ListView that is shown in Details mode, and the Checkboxes property is set to true. My problem is that I dont want the checkboxes being activated when items in the ListView are...
2
by: K. Wilder | last post by:
I'm trying to add checkboxes to EVERY COLUMN in the ListView control, not just the first column. (ListView control is in Details view.) I've seen it done in commercial applications. Does anyone...
1
by: Lonewolf | last post by:
hi all, just want to find out if it is possible to set the gridline of individual cell in a listview using C#. Reason for this is I need to have a listview which has fixed number of columns and...
5
by: Klaus Jensen | last post by:
Hi I am not sure, what is the best way to do this, and I hope somebody can offer some advice. :) I have 30 brands (from one table) out of one axis and 30 product categories (from another...
1
by: Carl Johansson | last post by:
I have a virtual ListView (VirtualMode set to true). To make it more convenient for the users of the ListView to select its items, I've set the ListView's CheckBoxes property to true. However, in...
1
Frinavale
by: Frinavale | last post by:
I'm working on an ASP.NET application using VB.NET for server side code. I have a GridView listing a bunch of stuff. Above the GridView I have a checkbox named "ChkBx_SelectAll". If this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.