How to put checkbox in listview header? Use of checkbox is for select all and vice versa... Thank You...

Hi,

You can't add a Checkbox into the listview header, but you can add a checkbox into the listview header and use this ccde to use it.

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

  For Each lvitem As ListViewItem In Me.ListView1.Items
   If lvitem.Checked = False Then
      lvitem.Checked = True
   Else
      lvitem.Checked = False
   End If
  Next

End Sub

Hi,

You can't add a Checkbox into the listview header, but you can add a checkbox into the listview header and use this ccde to use it.

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

  For Each lvitem As ListViewItem In Me.ListView1.Items
   If lvitem.Checked = False Then
      lvitem.Checked = True
   Else
      lvitem.Checked = False
   End If
  Next

End Sub

that's what i did.. if i put checkbox in the header the headertext cannot be seen... i may say the design is not soothing to the users eye...thank u.

Hi,

You can give the checkboxtext the same text as the listviewheader.

What you can do is the use a Datagridview instead and there you can add checkboxes.

my datagridview is readonly... means i cannot edit the datagridview... soh how can i do that?? thanz

Let me know if this helps.

Public Class Form1
    Private WithEvents cbCheckUncheckALL As New CheckBox With {.Text = "", .AutoSize = True} '// Declare New CheckBox.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '// Place CheckBox above ListView, in the location of ListView's Column 1.
        cbCheckUncheckALL.Location = New Point(ListView1.Location.X + 6, ListView1.Location.Y + 6) '// Set Location to work for your ListView.
        Me.Controls.Add(cbCheckUncheckALL) '// Add CheckBox to Form.
        cbCheckUncheckALL.BringToFront() '// Set CheckBox as TopMost control.
    End Sub

    Private Sub cbCheckUncheckALL_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbCheckUncheckALL.CheckedChanged
        For Each itm As ListViewItem In ListView1.Items
            If cbCheckUncheckALL.Checked Then itm.Checked = True Else itm.Checked = False '// Toggle Check/UnCheck ALL.
        Next
    End Sub
End Class

thanx for your help... i want the design to be smooth... and not that on the picture i attach... thank you...

Option 1. Add a blank titled column just for the CheckBoxes and start adding your ListView Items from the first SubItem and not as the Item.Text. (image 1)

With ListView1
            .Columns.Add("", 23) '// No Titled Column and sized for CheckBoxes.
            .Columns.Add("Column 1", 100)
            .View = View.Details : .CheckBoxes = True : .FullRowSelect = True
            Dim lvItm As New ListViewItem(",subitem".Split(","c)) : .Items.Add(lvItm)
            lvItm = New ListViewItem(",subitem".Split(","c)) : .Items.Add(lvItm)
            lvItm = New ListViewItem(",subitem".Split(","c)) : .Items.Add(lvItm)
        End With

Option 2. If your first Column is titled "Column 1", add a few empty spaces just prior to the title for accommodating the CheckBox.(image 2)

With ListView1
            .Columns.Add("     Column 1", 100)
            .Columns.Add("Column 2", 100)
            .View = View.Details : .CheckBoxes = True : .FullRowSelect = True
            Dim lvItm As New ListViewItem("item,subitem".Split(","c)) : .Items.Add(lvItm)
            lvItm = New ListViewItem("item 2,subitem".Split(","c)) : .Items.Add(lvItm)
            lvItm = New ListViewItem("item 3,subitem".Split(","c)) : .Items.Add(lvItm)
        End With
commented: + +2

i hav many columns... and when i move the horizontal bar i want my checkbox hidden... if column1(where the checkbox is) hides.. thank u

Tricky little CheckBox.:D

Ok, I have a solution.
Add a Panel, set the Panel's "AutoScroll=True" in the Panel's Properties.
Add your ListView within this panel, size the ListView to where all Columns are/should be visible without the horizontal scrollbar, and make the Panel smaller than the ListView.
This should add the horizontal scrollbar to the Panel and not the ListView.

When adding the CheckBox, instead of adding it to the Form Me.Controls.Add(cbCheckUncheckALL) '// Add CheckBox to Form. , add it to the Panel instead.

Panel1.Controls.Add(cbCheckUncheckALL) '// Add CheckBox to Form.

Now another issue arises, and that is the issue of the vertical scrollbar for the ListView.
This issue can be solved by sizing the ListView by the items.Count and causing the Panel to have the vertical scrollbar instead, but it is nothing like having the ListView's vertical scrollbar to use. :(

I will try and find a possible solution for this and will reply back to this thread if/when I do. Otherwise, I hope this quick fix helps.

i can't get it... the cbCheckUncheckALL did not appear... i will try other means... thank u for all ur help...

i got it... thank u :) Next thing to do the horizontal bar of the listview... thank u so much...

The only reasonable solution that is probably the best solution to use, is not to add a CheckBox control on top of the ListView, but to add images in the ColumnHeader of the ListView.

For this next piece of code to work properly, you will need to have 2 images of the CheckBox not Checked and Checked, and specify their location if different from "C:\".
1 ListView, 2 images(attached)

Public Class Form1

    Private Sub designListView()
        With ListView1
            .Size = New Size(125, 125)
            .Columns.Add("", 25) '// No Titled Column and sized for image of CheckBoxes.
            .Columns.Add("Column 1", 100)
            .Columns.Add("Column 2", 100, HorizontalAlignment.Center)
            .View = View.Details : .CheckBoxes = True : .FullRowSelect = True
            '// When adding Items, do not add it as the Item's.Text, Start adding at the first .SubItem of the Item.
            Dim x As String = ",item1 subitem1,subitem2" : Dim lvItm As New ListViewItem(x.Split(","c)) : .Items.Add(lvItm)
            x = ",item2 subitem1,subitem2" : lvItm = New ListViewItem(x.Split(","c)) : .Items.Add(lvItm)
            x = ",item3 subitem1,subitem2" : lvItm = New ListViewItem(x.Split(","c)) : .Items.Add(lvItm)
        End With
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        designListView() '// For TESTING.
        Dim imgList As New ImageList With {.ImageSize = New Size(13, 13), .ColorDepth = ColorDepth.Depth32Bit} '// New ImageList.
        Dim imgCheckedNot As Image = Image.FromFile("C:\checkedNot.png") '// image for Not Checked.
        Dim imgChecked As Image = Image.FromFile("C:\checked.png") '// image for Checked.
        '// Add 2 images to the ImageList.
        imgList.Images.Add(imgCheckedNot) '// Add Not Checked Image first.
        imgList.Images.Add(imgChecked) '// Add Checked Image.
        ListView1.SmallImageList = imgList '// set the ImageList for the ListView.
        ListView1.Columns(0).ImageIndex = 0 '// Add image to Column1.
    End Sub

    Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick
        If e.Column = 0 Then '// get Column1.
            With ListView1 '// swap images and check/uncheck all items.
                If .Columns(0).ImageIndex = 0 Then : .Columns(0).ImageIndex = 1
                    For Each itm As ListViewItem In .Items : If Not itm.Checked Then : itm.Checked = True : End If : Next '// check all.
                Else : .Columns(0).ImageIndex = 0
                    For Each itm As ListViewItem In .Items : If itm.Checked Then : itm.Checked = False : End If : Next '// uncheck all.
                End If
            End With
        End If
    End Sub
End Class

how about the location of the checkbox(header)? thank u so much...

Attached image has a CheckBoxImage.Size of (13, 13) and is not as off-centered as yours.
As for fixing every little issue "every time" one occurs, that is not for me to do.

Glad I could help otherwise and that I could provide a few suggestions and/or solutions.

Thank You :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.