checkbox in listview

albertkhor

Well-known member
Joined
Jan 12, 2006
Messages
150
Programming Experience
Beginner
i use listview to display my data, each row have a checkbox.

how to make my listview header have a checkbox, so that when user click on the checkbox, all other checkbox in listvew will checked too. Is that possible?
 
No, but you can utilize the ListView.ColumnClick event to get the behaviour.
 
kulrom said:
Why not using of separated checkBox control by placing over listView header (background = transparent) and make some code to check all the items below. at least it is how i would go for it ;)

what i do is add a CheckBox at the ListView header. Below is my code:
VB.NET:
[SIZE=1][COLOR=#0000ff]For [/COLOR][/SIZE][SIZE=1][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=1] item [/SIZE][SIZE=1][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=1] ListViewItem [/SIZE][SIZE=1][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=1][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=1].ListView1.Items
[/SIZE][SIZE=1][COLOR=#0000ff]If [/COLOR][/SIZE][SIZE=1][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=1].CheckBox.Checked [/SIZE][SIZE=1][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=1][COLOR=#0000ff]If [/COLOR][/SIZE][SIZE=1][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=1] item.Checked [/SIZE][SIZE=1][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=1]item.Checked = [/SIZE][SIZE=1][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=1][COLOR=#0000ff][/COLOR][/SIZE][SIZE=1][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=1][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=1][COLOR=#0000ff][/COLOR][/SIZE][SIZE=1][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=1][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=1][COLOR=#0000ff]Next
[/COLOR][COLOR=black]
[/COLOR]

when i checked the checkbox only the first row checkbox in listview have been checked. The secord row checkbox will not be checked, if i checked again the checkbox then second row in listview will be checked. may i know what wrong with my code? [/SIZE]
 
Sorry if my words were kinda Understimated but i wanted to point out that this is fair simple code and we would spend only time for nothing. I offer my appologize for the inconviniences if any :)

However this is the code you need to accomplish your task:
VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] CheckBox1_CheckedChanged([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, _
[/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] CheckBox1.CheckedChanged[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]  For [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] lvitem [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] ListViewItem [/SIZE][SIZE=2][COLOR=#0000ff]In [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ListView1.Items
[/SIZE][SIZE=2][COLOR=#0000ff]   If[/COLOR][/SIZE][SIZE=2] lvitem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]      lvitem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]   Else
[/COLOR][/SIZE][SIZE=2]      lvitem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]   End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]  Next[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

HTH
Regards ;)

P.S. Take a look at the attached demo project below
 

Attachments

  • ListViewCheckBox.zip
    23.4 KB · Views: 385
Regarding the code, if you want to toggle the checked state why not just:
VB.NET:
[COLOR=#0000ff]For [SIZE=2]Each[/SIZE][/COLOR][SIZE=2] lvitem [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] ListViewItem [/SIZE][SIZE=2][COLOR=#0000ff]In [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ListView1.Items
[/SIZE][SIZE=2]  lvitem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] lvitem.Checked [/SIZE]
[SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]
 
kulrom said:
Also notice that as i said you can control this behaviour through the 1st item of listview control by checking it's checked state.

HTH
Regards :)

kulrom thanks for you help! how about if i want to check whether the checkbox in the listview all have been checked or not, if yes then the CheckAll checkbox will set to true!
 
Loop through its checkedItems collection like:
VB.NET:
[SIZE=2][COLOR=#0000ff]If [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ListView1.CheckedItems.Count = 15 [/SIZE][SIZE=2][COLOR=#0000ff]Then [COLOR=darkgreen]'i assumed that you have 15 items only[/COLOR]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]   Me[/COLOR][/SIZE][SIZE=2].CheckBox1.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]

However, this is not that simple as you may think of ... maybe you should consider remove and add handler statements cuz it may not give wanted and expected result ... maybe if you try this (1st remove handler od checkBox1 in order to avoid its event which will select or unselect all items ..and then just return the handler through addhandler statement):

VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ListView1_ItemCheck([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], _[/SIZE]
[SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.ItemCheckEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] ListView1.ItemCheck[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]RemoveHandler[/COLOR][/SIZE][SIZE=2] CheckBox1.CheckedChanged, [/SIZE][SIZE=2][COLOR=#0000ff]AddressOf[/COLOR][/SIZE][SIZE=2] CheckBox1_CheckedChanged
[/SIZE][SIZE=2][COLOR=#0000ff]  If [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ListView1.CheckedItems.Count = 15 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]   Me[/COLOR][/SIZE][SIZE=2].CheckBox1.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]  End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]AddHandler[/COLOR][/SIZE][SIZE=2] CheckBox1.CheckedChanged, [/SIZE][SIZE=2][COLOR=#0000ff]AddressOf[/COLOR][/SIZE][SIZE=2] CheckBox1_CheckedChanged[/SIZE]
 
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

HTH
Regards ;)

P.s. as i said this is not trivial at all as now checkBox1's procedure may throw an unexpected result ... anyway feel free to ask if you have more questions :)
 
Back
Top