I am making a To Do program. I have a checked list box there. I want to make every checked item deleted automatically. Here's the code that I am using to check if checked:
For i = 0 To CheckedListBox1.Items.Count - 1
If CheckedListBox1.GetItemChecked(i) Then
Else
End If
Next
How can I do that? Thanks
To remove Items from your ListBox, you can save items to be removed in your loop and then to delete it for example :
Dim itemsToRemove As New List(Of Object)
For i = 0 To CheckedListBox1.Items.Count - 1
If CheckedListBox1.GetItemChecked(i) Then
itemsToRemove.Add(CheckedListBox1.Items(i))
End If
Next
For Each item As Object in itemsToRemove
CheckedListBox1.Items.Remove(item)
Next
If you loop backwards then removing an item doesn't affect the part of the list you have yet to check
For i = CheckedListBox1.Items.Count - 1 to 0 Step -1
If CheckedListBox1.GetItemChecked(i) Then
CheckedListBox1.Items.RemoveAt(i)
End If
Next
This won't prevent the cross thread exception you're getting; for that we really need to know why you're accessing this Control from a thread other than the thread that created it
Related
I have a checklistbox and the items of it came from my database (tbl_Section) so it loads all the Section Number (Primary Key). I have 5 Section Numbers, and 3 of it will be assigned to only one teacher. I'm thinking of using While-statement but i dont know how.
To make it simpler to you, this is what i need to do:
While //index(number) is checked
//do something
Else (i know it should not be ELSE, but i dont know what keyword is to be used)
//do something
End While
Thanks a lot!
What you want to do is iterate through every item in your checkbox. For each item, you check if it is checked, then you act accordingly :
'We will run through each indice
For i = 0 To CheckedListBox1.Items.Count - 1
'You can replace As Object by your object type
'ex : Dim Item As String = CType(CheckedListBox1.Items(i), String)
Dim Item As Object = CheckedListBox1.Items(i)
'We ask if this item is checked or not
If CheckedListBox1.GetItemChecked(i) Then
'Do something if Item is checked
Else
'Do something else if Item is not checked
End If
Next
I'm trying to clear items from a listbox that currently appear as child nodes in a treeview. I've come up with storing the child nodes in an array(which works) then thought I would loop through and remove the listbox item if it appears in the array (basically the treenodes are added from double clicking the treeview and are placed under a dated parent node).
When I run the code I get the following error message on the final Next
List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change.
I know this is to do with the For Each loop however I'm struggling as a novice to work it out.
Dim ndes As New ArrayList
Dim no As TreeNode
For Each no In tvProgress.Nodes
Dim CNode As TreeNode
For Each CNode In no.Nodes
ndes.Add(CNode.Text)
Next
Next
Dim ditem As Object
For Each ditem In lstPlanned.Items
If ndes.Contains(ditem) Then
lstPlanned.Items.Remove(ditem)
End If
Next
You need a diff loop:
For i As Integer = (lstPlanned.Items.Count - 1) To 0 Step -1
If ndes.Contains(lstPlanned.Items(i)) Then
lstPlanned.Items.Remove(lstPlanned.Items(i))
Exit For
End If
Next
I have a CheckedListBox control that I fill with DataGridView Column HeaderText values. If these columns are visible, I would like to set the CheckedListBox Items to "Checked". My code is as follows:
For Each col As DataGridViewColumn In frmTimingP2P.dgvOverview.Columns
If col.Visible = True Then
For Each item In clbOverviewColumnOrder.Items
Dim intItemIndex As Integer = clbOverviewColumnOrder.Items.IndexOf(item)
If col.HeaderText = item.ToString Then
clbOverviewColumnOrder.SetItemCheckState(intItemIndex, CheckState.Checked)
End If
Next
End If
Next
Whenever this code runs, I get the following error:
"List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change."
What causes this? How can I get around this issue?
Thanks
Whenever are doing a for loop through an enumerator, the enumeration can't be modified or it throws this exception.
I'm not certain exactly why the enumeration would be changing here (it could be possible some other parts of your code are reacting to the change in check state) but one way to get around this would be to instantiate an enumerator and then loop through that instead.
I don't know VB, so here's some psuedo code!
e.g.
newEnumerator = ColumnOrder.Items.GetEnumerator()
begin loop through newEnumerator
set checkbox
end loop
So even if the Items list changes, it shouldn't affect this enumerator.
Thanks for the advice. I guess the error was related the the fact that, under some circumstances, you cannot modify a set of controls during a For...Next loop.
I have revised my code and ended up with the following:
Do While intCurrentItemIndex >= 0
Dim strCurrentItem As String = clbOverviewColumnOrder.Items(intCurrentItemIndex)
For Each col As DataGridViewColumn In frmTimingP2P.dgvOverview.Columns
If col.HeaderText = strCurrentItem Then
If col.Visible = True Then
clbOverviewColumnOrder.SetItemCheckState(intCurrentItemIndex, CheckState.Checked)
Else
clbOverviewColumnOrder.SetItemCheckState(intCurrentItemIndex, CheckState.Unchecked)
End If
End If
Next
intCurrentItemIndex -= 1
Loop
Process: A node is added in a treeview control with node text = textbox1.text
I want to prevent addition of duplicate node i.e. say, if a node with text "ABC" is added then next time, a node with text "ABC" should not be added to treeview control.
I tried following methods but could not achieve desired result.
Method A)
Dim list As New ArrayList
list.Add(TextBox1.Text)
if list.Contains(Textbox1.Text) then
MsgBox("Use different name")
else
.....code to add node with text
end if
Method B)
if Treeview1.Nodes.Count > 0 then
For i = 0 to Treeview1.Nodes.Count
if Treeview1.Nodes(i).Text=Textbox1.Text then
MsgBox("Use different name")
end if
next
else
........code to add node with text
end if
I could not understand the solutions suggested for C# on this forum.
Any help will be really appreciated.
Thanks
Method A should work OK. You might have another error in your code (in the else section?). list should be declared static if it is in a function that's being called repeatedly, else it will be reset to new (cleared) each time.
Method B has a couple of errors: (1) the for statement should be For i = 0 to Treeview1.Nodes.Count - 1 (maybe use "for each"), and the else with the code to add the node should be after the msgbox statement. Also, method B only search for the root node(s) of the treeview. You would need to traverse the tree to check all the nodes.
If ListView1.Items.Count > 0 Then
For I = 0 To ListView1.Items.Count - 1
For Each LVL As ListViewItem In ListView1.Items
If ListView1.Items.Item(I).Index = LVL.Index Then
Continue For
Else
If ListView1.Items.Item(I).Text = LVL.Text Then
LVL.Remove()
End If
End If
Next
Next
End If
This is probably easy as well. But I have this listview which contains exe files I've listed up. Now, I want to execute these exe files in turn from which items are checked or not.
So, I've tried this:
For each item in listView1.CheckedItems
Msgbox item.ToString
Next
Cause I noticed that the item in checkedItems doesn't contain much. And if I convert it to a string, it ends up in the msgbox looking like this: ListViewItem: {Filename.exe}
Now, I want the filename obviously. But is there any other way of extracting the name only? Or do I have to strip the string to remove the ListViewItem: { part away?
Your first step should have been to consult the documentation of ListViewItem to find out how to retrieve the required information from the object.
The Text property is what you’re after.
For Each item in listView1.CheckedItems
MsgBox(item.Text)
Next
For each item in listView1.CheckedItems
Msgbox item.Text
Next
This should work for winforms.
Have you tried Msgbox item.Text?
Your probably lookin for more of something like this. This will have the item and the subitems. You will need a loop with in a loop to accomplish this. Hopes this helps
Dim x As Integer = 0
Dim y As Integer = 0
For Each item In listView1.CheckedItems
For Each subitem In listView1.CheckedItems(y).SubItems
MsgBox(subitem.Text)
x = x + 1
Next
y = y + 1
Next