Using each item in a list box - vb.net

I am working on a new application and am trying to work with a Listbox. What I am trying to do is run a task with each item in the Listbox one at a time.
Example:
Items in Listbox:
Dog
Cat
Fish
Cow
For this example, I would like the program to start at the top at 'Dog', display the result, then go to 'Cat', and so on until there are no more items.
Side note: I would also like it if there was a way to highlight what item it was currently on.
Thanks in advance guys, I am working on learning some of the other features of the ListBox with this new application.

The ListBox has some properties you may be interested in.
You want to highlight the item. Therefore, you'll need the SelectedIndex
Since you want to select it, a standard For loop will be needed.
To access the items in the list, you use the Items collection.
The Items collection has a Count property.
Here is an example based on what you provided. AnimalList is a ListBox, and ShowNames is a Button:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AnimalList.Items.AddRange({"Dog", "Cat", "Fish", "Cow"})
End Sub
Private Sub ShowNames_Click(sender As Object, e As EventArgs) Handles ShowNames.Click
For i As Integer = 0 To AnimalList.Items.Count - 1
AnimalList.SelectedIndex = i
MessageBox.Show(AnimalList.Items(i).ToString())
Next
End Sub
If you set the SelectedIndex, you can also access the SelectedItem property.
When you have a multi-select list, there are also SelectedIndices and SelectedItems properties.

Related

How do I change the options in one ComboBox depending on another ComboBox?

I am creating a windows form, in this form I have two Combo Boxes that I want to be linked together, meaning when I select one item in the first combo box (Category), it will change the items in the other combo box (Item) accordingly. I have no code behind it at the moment because I am unsure of how to begin. The image is off the form, if it helps in any way for you understanding what I need to happen then its there. I've been on different sites trying to read about how to do it but I am just completely lost. If anyone can point me in the right direction then that would be much appreciated I now get this error whenever I run it and i'm not sure how to fix it, I am not sure what to put in these brackets 'If cmbCat.SelectedItem() Then'
No matter what I seem to put nothing works.
Public Class frmRestaurantOrd
Public Shared cmbCatDrinks As String = "Drinks"
Public Shared cmbCatMain As String = "Main"
Private Sub cmbCat_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbCat.SelectedIndexChanged
If cmbCat.SelectedItem() Then
cmbItem.Items.Add("Water")
cmbItem.Items.Add("Orange Juice")
cmbItem.Items.Add("Coca Cola")
cmbItem.Items.Add("Beer")
ElseIf cmbCat.SelectedItem("Main") Then
cmbItem.Items.Add("Piza 1")
Else cmbCat.SelectedItem.Equals("")
cmbItem.Items.Clear()
End If
End Sub
Private Sub frmRestaurantOrd_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cmbCat.Items.Add(cmbCatDrinks)
cmbCat.Items.Add(cmbCatMain)
End Sub
I would encourage you separate your data from the logic. What I mean by that is create a separate class for each selection category so that you will end up with three lists:
List(Of Category) 'This list will contain the two category of choices: Drink, MainDish
List(Of Drink) 'This list will contains the choices of drinks
List(Of MainDish) 'This list will contain the choices of main dishes
I would avoid using "Main" as the name of a class or List
Then, based on the choice a user makes in your combo box cmbCat, set the cmbItem DataSource property to one list or the other.
Pseudo code:
cmbItem.DataSource = IIf(cmbCat.SelectedIndex == 0, Drinks, MainDish)

Inserting data to dropdown comboboxes

I am a fairly new to visual studio. I have 2 forms with dropdown cb_CBOX1 & dropdown list cb_CBOX2.
I want user to add data to cb_CBOX1 and have the data be inserted in alphabetical order in cb_CBOX1 dropdown and be inserted in alphabetical order in cb_CBOX2 dropdown list also.
I'm trying to use the below statement. TIA
cb_CBOX2.Items.Add(cb_CBOX1.Text)
Update 2017-11-07:
I have 2 forms both have combobox with drop down list. I want user to insert data to cb_CBOX1 and have the data added alphabetically to the cb_CBOX1 drop down and added alphabetically to cb_CBOX2 drop down. When the user types in data in cb_CBOX1, they will click button1 to call the Add function.
My code:
Private Sub button1.Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
cb_CBOX2.Items.Add (cb_CBOX1.Text)
End Sub
Extract the items, add the new item, sort, then apply the items back. It's fairly simple with Linq.
Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
addToAndSortComboBox(cb_CBOX1, cb_CBOX1.Text)
addToAndSortComboBox(cb_CBOX2, cb_CBOX1.Text)
cb_CBOX1.Text = "" ' optional
End Sub
Private Sub addToAndSortComboBox(cb As ComboBox, value As String)
Dim items = cb.Items.Cast(Of String).Concat({value}).OrderBy(Function(v) v).ToList()
cb.Items.Clear()
cb.Items.AddRange(items.ToArray())
End Sub
Just a comment on the design: it doesn't seem very intuitive. I feel like the input should be done in a TextBox with the ComboBoxes having ComboBox.DropDownStyle = DropDownList.
It sounds like you want to be able to add data to a list, Whatever the user types into combobox1, will appear on combobox2 on a different window?
I'd suggest binding your combo boxes to a list, and then setting the item-sources of both combo-boxes to this list.
Create the list, and reference it in a class so you can access it from a different window...
Dim comboboxitemlist As New List(Of String)
Bind your list to CB2...
cb_CBOX2.itemssource = comboboxitemlist
In a button or however you are adding...
comboboxitemlist.add(cb_cbox1.text)

Set Length of ComboBox

I'm trying to set the length of a Combo Box control in the most efficient way possible. By length, I mean the number of items in the items collection of the Combo Box control.
This is the best attempt I have:
Dim cboNew As New ComboBox
For i As Integer = 0 To cboSelection.Items.Count - 1
cboNew.Items.Add(cboSelection.Items(i))
Next
cboSelection is another Combo Box control I have, I am pretty much trying to set the length of cboSelection to cboNew with one line of code (If cboSelection has 5 items, then set cboNew to have 5 items). I feel as if I have done this before, but have forgotten how.
If you are willing to use the DataSource property, you could do something like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.DataSource = New String() {"tony", "bruce", "clark"}
ComboBox2.DataSource = ComboBox1.DataSource
End Sub

Basic solution for adding list item to combo box via code on load

What I need:
I was a basic equivalent of a select box i.e. a combobox of dropdown list style (that preferably doesn't allow text input).
I need to add the list items by code rather than the property box.
What I have:
Private Sub Form_Load ()
ComboStaffMember.AddItem "John Murphy"
End Sub
...produces "...add item is not a member of system.windows.forms.comboxbox".
Private Sub Form_Load ()
ComboStaffMember.Items.Add("John Murphy")
End Sub
...produces no result.
My question:
Why is the item not adding? The form name is FrmStaffLogIn and it's in Form1.vb. Should Form_Load correspond to either of these or is my code incorrect elsewhere?
Try to put combo add statement in following format in form load event :
Private Sub Form_Load ()
Me.ComboStaffMember.Items.Add(New DictionaryEntry("Text to be displayed", 1))
End Sub
Are you sure your code line ComboStaffMember.Items.Add("John Murphy") doesn't work? it should work just fine.
The Add() method On Item collection expects object parameter and string as well can be passed as argument to it. Like below [C# code sample]:
this.comboBox1.Items.AddRange(
new string[] {"SomeText","SomeOtherText","LastText"});
Also, you probably don't see any item cause you haven't set a default selected item. Just expand the dropdown and you will see the items. To set the default selected item
this.comboBox1.SelectedIndex = 0;
Working code:
Private Sub FrmIdentCust_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboStaffMember.Items.Add("John Murphy")
End Sub
I was missing (sender As Object, e As EventArgs) Handles MyBase.Load.

How to adding checked item from checkedlistbox to combobox

I want to adding checked item from checkedlistbox to my combobox, but i have a little problem here. Combobox only show 1 item last checked.
This is my sample code.
If CheckedListBox1.CheckedItems.Count <> 0 Then
For i As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
cbCheckedItem.Text = CheckedListBox1.CheckedItems(i).ToString
Next i
End If
anyone can help me show all checked item??
thank's for your help...
You aren't adding the items to the combo box, you're only setting its Text property. That's only changing the text currently displayed in the combo box, and only one item can be displayed at a time.
If you want the items to be permanent and selectable, you need to add them to the combo box control's Items collection.
Sample code:
If CheckedListBox1.CheckedItems.Count > 0 Then
For Each checkedItem In CheckedListBox1.CheckedItems
cbCheckedItem.Items.Add(checkedItem.ToString())
Next
End If
Or, better yet, use the AddRange method:
If CheckedListBox1.CheckedItems.Count > 0 Then
Dim checkedItems() As String = CheckedListBox1.CheckedItems.Cast(Of String).ToArray()
cbCheckedItems.Items.AddRange(checkedItems)
End If
Oddly enough the CheckedListBox has a CheckedItems property, which is a collection. As such you can loop through it like you can any other collection, using a For or For Each loop.
then, Each item needs to be added to the Items collection of the ComboBox.
like this sample:
Public Class frmCheckedListBox
Private Sub frmCheckedListBox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CheckedListBox1.Items.Clear()
Me.CheckedListBox1.BeginUpdate()
Me.CheckedListBox1.Items.Add("One")
Me.CheckedListBox1.Items.Add("Two")
Me.CheckedListBox1.Items.Add("Three")
Me.CheckedListBox1.Items.Add("Four")
Me.CheckedListBox1.Items.Add("Five")
Me.CheckedListBox1.EndUpdate()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each Item As String In Me.CheckedListBox1.CheckedItems
Me.ComboBox1.Items.Add(Item)
Me.ComboBox1.SelectedIndex = 0
Next
End Sub
End Class
As sample code shows, the CheckedItems collection contains the items that are checked, just as the name suggests. It doesn't contain a Boolean value for each an every item to indicate whether it is checked or not. If an item is checked then that item is in the CheckedItems, and if it isn't then it isn't. You simply need to loop through the collection and get every item in it, because it contains all the items that are checked and none that aren't.
in the end you can put :
Me.Combobox1.items.clear()
because when it would click with sample code it would have the one that clicked then on the next click would return the previous one it had clicked and then the new one all compiled in the combobox selection menu
perhaps my answer can help you solve your problems
Combobox doesn't have a multiselect option. so only one item at a time could be selected.