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
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
Sorry im new :-D
i wanne do the following in a List box:
add item
add item
....
im counting the added items and display them to a label.text. and also sending that to a .txt that updates.
when all items are added i want to press a bttn "end adding Items" and then doubleclick the items to edit them(i want to count that aswell.)
So want it look like this "changed Items / total Items"
how can i count the changes done by the doubleclick ? and how to display it ?
this is the doubleclick i did ;D
Dim intIndex As Integer = lstBla.Items.IndexOf(lstBla.SelectedItem)
Dim objInputBox As Object = InputBox("Edit", lstBla.SelectedItem)
If Not objInputBox = Nothing Then
lstBla.Items.Remove(lstBla.SelectedItem)
lstBla.Items.Insert(intIndex,objInputBox)
End If
End Sub
and this is the save to the lable.txt
Total.Text = lstBla.Items.Count
My.Computer.FileSystem.WriteAllText("C:\Counter\Total.txt", Total.Text, False)
Once I select a value from a Combobox and save the form, ComboBox.SelectedText contains the value selected but ComboBox.SelectedIndex is returning 0 always for each item in the list. Below is just a sample code for reference.
If (combobox1.SelectedIndex = 0 Or combobox1.SelectedText = "")
MessageBox.Show("No value selected")
else
MessageBox.Show("Some value selected")
End If
Some code to illustrate usage
Private Sub ComboBox1_SelectedIndexChanged(sender As Object,
e As EventArgs) Handles ComboBox1.SelectedIndexChanged
'check for no item selected
If ComboBox1.SelectedIndex < 0 Then
Stop 'no item
Else
Dim idx As Integer = ComboBox1.SelectedIndex
Dim val As String = CStr(ComboBox1.SelectedItem) '<-- use SelectedItem
Stop
End If
End Sub
Small Answer
SelectedText is not the same as SelectedItem. Take a look at ComboBox.SelectedText:
Gets or sets the text that is selected in the editable portion of a ComboBox.
I think that you are confusing this with ComboBox.SelectedItem
SelectedIndex: -1 if nothing selected, otherwise the index of the selected item
SelectedValue: null if nothing selected, otherwise the selected item
SelectedText: the text that the operator marked in the editable part of the combo box.
** Room for improvement **
You use VB. My VB is a bit rusty, so I'll give my answer in C#. I guess you'll get the gist.
In Winforms, whenever you want to fill a that shows a sequence of items, like ComboBoxes, ListBoxes, DataGridViews, Charts, etc, there are usually two methods:
Fill the ComboBox one by one with the texts that you want to display
Use a DataSource: fill it with the Items that you want to be selectable, and tell the ComboBox which property of the selectable items you want to display.
Use the first method if you only want to display a constant array of strings. Fill ComboBox.Items. When an item is selected, use SelectedIndex to get the index of the selected string. Use ComboBox.Items[selectedIndex] to get the selected string.
If the string represents something more than just a string, for instance the text represents a Customer, or a Product. It is usually easier to use the DataSource method.
To do that, you use property ComboBox.DataSource to tell the ComboBox which Customersit should display. In ComboBox.ValueMember you tell the ComboBox which Customer poperty should be used to represent the Customer, for instance the name of the Customer.
Once the operator selected the name of the Customer, you get the complete Selected Customer using property ComboBox.SelectedItem:
List<Customer> availableCustomers = ...
ComboBox combo1 = new ComboBox(...);
combo1.ValueMember = nameof(Customer.Name); // the Customer property that you want to display
combo1.DataSource = availableCustomers;
After the operator selected an item, you can process the event, and fetch the selected customer immediately:
Customer selectedCustomer = (Customer)cmbo1.SelectedValue;
ProcessSelectedCustomer(selectedCustomer);
Of course you should only select a property that is unique. If you have two Customers named "Hans Brinker", operators wouldn't know which name represents which Customer.
Apart from the nice thing that you don't have to do a lookup from SelectedIndex to what this selected item represents (a Customer), you are independent of the order in which the Customers are displayed.
Another nice thing: if in future versions you want to change from ComboBox to ListBox, or maybe even to DataGridView, you won't have to change your model drastically: the control still shows a sequence of Customers, and once an operator selects something that represents this Customer (Name? Id?, DataGridView Row?), you get the complete Customer.
(1)ComboBox1.SelectedIndex starts from -1, when you are not selected, ComboBox1.SelectedIndex=-1
(2)When you click combobox1.items, SelectedText is always "".
This is because, at the time of these events(SelectedIndexChanged, SelectedValueChanged.. ), the previous SelectedText value has been cleared and the new value has not yet been set. You can use the SelectedItem property instead.
E.g:combobox1.SelectedItem
Please try the following code.
If ComboBox1.SelectedIndex = -1 Then
MessageBox.Show("No value selected")
Else
MessageBox.Show("Some value selected")
End If
I've created a Student Record, I have a Button Delete ,I want to display a MessageBox with warning When Delete Button is clicked and no Item is selected in my Listview Control.You need to choose a Student.
Here is my code
If LvSV.SelectedItems.Count < 0 Then
MessageBox.Show("You need to choose Student!")
Else
Dim items As ListViewItem
items = LvSV.SelectedItems(0)
items.Remove()
End If
Dear you have a small mistake in your code :-)
you should check for Count equal to 0 instead of < 0.
If LvSV.SelectedItems.Count = 0 Then
MessageBox.Show("You need to choose Student!")
Else
Dim items As ListViewItem
items = LvSV.SelectedItems(0)
items.Remove()
End If
Also your code assumes exactly 1 selected item. Maybe you want to generalize it to all selected items.
For each item As ListViewItem in LvSV.SelectedItems
LvSV.Items.Remove(item)
Next
Cheers
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