Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The code I have right now worked but I want to condense it so I am not using so many lines of code.

here is what i have now:

 For Each itm As ListViewItem In ListView2.Items

            If itm.SubItems(1).Text.ToString.Contains("document") Then
                CheckBox11.Checked = CheckState.Checked
            End If

            If itm.SubItems(1).Text.ToString.Contains("picture") Then
                CheckBox13.CheckState = CheckState.Checked

'.....etc
            End If



        Next


I am wanting to compare the text to checkboxes texts in a group box. so that when the text match it will automatically check the checkbox it matched with.

What I have tried:

I tried to do a for each statement for the group box including the for each statement for the listview and it kept repeating the text and never checked any of the checkboxes.
Posted
Updated 4-Apr-18 20:54pm
Comments
Christiaan van Bergen 4-Apr-18 18:57pm    
Are the CheckBoxes identifiable by Name or Text like "document" or "picture"? You now have a connection only in your code between the magic string "document" and a CheckBox11. Making it hard to not use this kind of elaborate coding.
Member 11856456 4-Apr-18 22:41pm    
They are identifiable by text, visual studio will not allow me to use white spaces in the design name. I was hoping to do something close to a for each statement, that would compare all the texts.
Maciej Los 5-Apr-18 2:34am    
WinForms or WebForms?
Member 11856456 5-Apr-18 2:42am    
Winforms

1 solution

As per official documentation a CheckBox.Checked Property (System.Windows.Forms)[^] gets or set a boolean value.

Your code might be simplified to:
VB.NET
CheckBox11.Checked = itm.SubItems(1).Text.ToString.Contains("document")


Note: when you go trough the collection of ListView2.Items a Checked state of checkboxes is changing in every single step of For Each ... Next loop!

If you would like to change the state of checkbox on ListView item selection change, use onClick or onSelectedIndexChange event. See: ListView Events (System.Windows.Forms)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900