Receiving an error on the following line, any suggestions?
If lstbxCharacters.ListCount = 0 Then
Else
End if
Error: Listcount is not a member of Windows.Forms.Listbox
Any method that I can call to check if the contents of a listbox is NOT empty?
Iv'e tried this instead now but it gives me a boolean error when the listbox gets populated with different methods
If lstboxCharacters.Datasource = "" Then
Else
<bunch of methods>
End if
Fixed
If lstbxCharacters.Items.Count > 0 Then
Else
End if
Thanks anyway :)
If lstbxCharacters.Items.Count > 0 Then
'data exists
Else
'NO data exists
End if
the list box has an Items collection that you can check. Google the control and msdn and it will show you all the members.
Related
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
I have a dropdown that displays nothing. If I hit the save button and nothing is displayed I want an error message stating something has to be selected
I've tried if ddl.POApprover.text = "" then display the message but that doesn't display
If ddlPOApprover.Text = "" And (Session("UserGroup") = 13) Then
MsgBox("Error: An Approver must be provided before you can save")
blnValidation = False
End If
I want the error to come up when I hit save but it doesn't
It's If ddlPOApprover.SelectedValue not .Text
A Combobox has an array from 0 to numberOfItems -1.
whenever there is nothing selected, it's index is -1
so you can check if ddlPOApprover.selectedindex < 0 then no item has been chosen
Edit: i also want you to try replacing And with AndAlso like:
If ddlPOApprover.Text = "" AndAlso (Session("UserGroup") = 13) Then
would it still give you an error? if not then you can choose either my main answer or leave it and just change And to AndAlso
I'm not able to get the value of a checkbox..
I've googled a lot but I really can't find the error.
Anyway this is my code:
If printa.Checked = True Then
objDoc.PrintOut
Else
bjDoc.Close
End If
The chechbox's name in the Userform is "printa".
I get the error that it can't find the method or the object.
Any advice?
Thanks
Dennis
The property you want to use is Value:
If printa.Value = True Then
objDoc.PrintOut
Else
objDoc.Close
End If
(There's also a typo in your objDoc.Close)
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