Changing a Combobox selected Item - vb.net

Here's what I need to do:
I have a ComboBox that has a whole list of items, for this example lets just call them 1, 2, 3.
If someone selects 3 I want to reset the ComboBox.
So if I was to select 3, the ComboBox would then return to its default blank state.
I just want to make this clear that I don't want the actual ComboBox to reset, that is, I don't want to remove the items I have listed in it. I just want the selection to go blank again.

As LarsTech said, set the SelectedIndex = -1. Here is a complete example with just a ComboBox on the form.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.DataSource = Enumerable.Range(1, 10).ToList()
ComboBox1.SelectedIndex = -1
End Sub
Private Sub ComboBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedValueChanged
If CInt(Me.ComboBox1.SelectedValue) = 3 Then
ComboBox1.SelectedIndex = -1
End If
End Sub

Related

How to transfer data from listbox to textbox in Visual Basic

I did some activity but I cant figure out how to retrieve data from listbox1 to textbox1. Example in the listbox1 there are 4 names: John, Jorge, Joe. Then I want to transfer Joe from listbox1 to textbox1. I did an arraylist where I adding those 3 names in the listbox1 but I didn't know how to retrieve the name "Jorge" from listbox1 to textbox1. Send help.
Here's the code where I try to retrieve one of the name from listbox1 to textbox1
Private Sub Retrievebtn_Click(sender As Object, e As EventArgs) Handles Retrievebtn.Click
If textbox1.Text = ListBox1.Items.Count Then
textbox1.Text = ArrayofNames(x)
End If
End Sub
Here's the whole code
Public Class Form1
Dim ArrayofNames() As String
Dim x As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Retrievebtn_Click(sender As Object, e As EventArgs) Handles Retrievebtn.Click
If textbox1.Text = ListBox1.Items.Count Then
textbox1.Text = ArrayofNames(x)
End If
End Sub
Private Sub Addbtn_Click(sender As Object, e As EventArgs) Handles Addbtn.Click
Dim x As Integer = 0
ReDim ArrayofNames(x)
For x = 0 To ArrayofNames.Length - 1
ArrayofNames(x) = Addtextbox.Text
ListBox1.Items.Add(ArrayofNames(x))
Next
End Sub
Private Sub removeBtn_Click(sender As Object, e As EventArgs) Handles removeBtn.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
End Sub
End Class
Here's the Image of the interface i try to retrieve the name Joe but it wasn'tshowing
Let's go over the code you posted.
In the retrieve button click event you are comparing and Integer to a String with textbox1.Text = ListBox1.Items.Count This won't compile with Option Strict On. You do have Option Strict On, don't you? You should always have this on.
On the next line, you assign the value of ArrayofNames(x), where x refers to your form level variable, to a text box's Text property. Since the value of x is never changed anywhere in the code this will always return the 0 index in the array. (The first element) I can't imagine why it should matter that the Textbox.Text should equal the count of the ListBox items.
In the add button click event you first declare a local variable x. Any code in the method will use this x, not the form level x. You ReDim (without the Preserve keyword) the array to an array with a single element. Your For loop is silly because the length of ArrayofNames is 1, so it is 0 To 0.
The ArrayofNames will never have more than a single element and it will be overwritten each time the add button is clicked.
Fortunately the Listbox maintains its own collection of items.
We can use this collection to simplify your code. Just add the contents of the text box to the the list items.
To retrieve a value from the list box you must first determine if the user has entered a valid index. First, is the entry a valid Integer and is it an index present in the list box.
Remember that .net collections start at index 0.
I use default names in my test program but you should continue to use meaningful names for your controls.
'Your add button
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim index As Integer
If Integer.TryParse(TextBox1.Text, index) AndAlso index >= 0 AndAlso ListBox1.Items.Count - 1 >= index Then
TextBox2.Text = ListBox1.Items(index).ToString
End If
End Sub
'Your add button
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox1.Items.Add(TextBox3.Text)
End Sub

Combo box functionality

I have 4 combobox in my visual basic;
ComboBox1 has 3 items: Vehicle, Motorbikes, None
ComboBox2 has 4 items: sportbike, casual bikes and sportcar,casual cars
ComboBox3
ComboBox4
I KINDLY need a code that will let me do the following:
Make combobox 2,3,4 invisible until I make a selection on combobox 1 i.e. I will choose vehicle and later progress to select sportscar meanwhile combobox 3 and 4 are invisible. In short, the next combo box only appears after making a selection on the previous one.
On combobox 1, if "none" is selected the other 2,3,4 remains invincible .
On the forms Load event you need to set combo boxes 2,3 and 4 to visible=False. Then on the ComboBox1_SelectedIndexChanged event you can change combobox2 to visible=True and then do the same for each progression. The code is shown below. You will also need to decide how you want to reset previous box. In other words do you want to comboboxes 2, 3, and 4 to once again become invisible if you change from Motorbikes to none in combobox1?
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox2.Visible = False
ComboBox3.Visible = False
ComboBox4.Visible = False
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedItem="None" then
ComboBox2.Visible=False
ComboBox3.Visible=False
ComboBox4.Visible=False
Else
ComboBox2.Visible = True
End If
End Sub
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
ComboBox3.Visible = True
End Sub
Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
ComboBox4.Visible = True
End Sub
End Class
In your designer file, set the Visible property of 2, 3, and 4 to false.
Then in your code file use the following code:
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim showComboBox = Not String.IsNullOrWhitespace(ComboBox1.Text) AndAlso ComboBox1.Text <> "None"
ComboBox2.Visible = showComboBox
ComboBox3.Visible = showComboBox
ComboBox4.Visible = showComboBox
End Sub
What this does is set a Boolean variable equal to if the Text of ComboBox1 is not an empty string and not "None". It then sets the Visible property of the other ComboBox controls to the result.

Replacing listview items with another list using combo box

Pardon for being novice at vb.net. I have a combo box and a list view. What I needed is when I change the category in the combo box and pressed 'OK', the old list added before will be replaced by a new list.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As ListViewItem
If ComboBox1.Text = "Terrestrial Ecotoxicity (Freshwater)" Then
i = ListView1.Items.Add("Water")
i.SubItems.Add("2068.030567")
i.SubItems.Add("0")
i.SubItems.Add("0")
ElseIf ComboBox1.Text = "Terrestrial Ecotoxicity (Seawater)" Then
i = ListView1.Items.Add("Dimethylamine")
i.SubItems.Add("1229.539887")
i.SubItems.Add("0.000122731")
i.SubItems.Add("0.15090266")
End if
End Sub
What should I need to add?
What you are doing there is adding a different row on each case. What you ask is to fill the listview with different items depending on the item you pick at the combobox.
Basically, you need to clear the listview on each case. I'd do something like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As ListViewItem
ListView1.Items.Clear
If ComboBox1.Text = "Terrestrial Ecotoxicity (Freshwater)" Then
i = ListView1.Items.Add("Water")
i.SubItems.Add("2068.030567")
i.SubItems.Add("0")
i.SubItems.Add("0")
ElseIf ComboBox1.Text = "Terrestrial Ecotoxicity (Seawater)" Then
i = ListView1.Items.Add("Dimethylamine")
i.SubItems.Add("1229.539887")
i.SubItems.Add("0.000122731")
i.SubItems.Add("0.15090266")
End if
End Sub

Get corresponding items from a selected listbox item

As the title says, Once I select an item from lstordpizza, the corresponding item from lstordserving and lstordqty will be shown from the comboboxes shown in the image. Any help would be appreciated. :)
EDIT: Sorry for the lack of info to my post and I can't think of any way to do this.
When I select 'Hawaiian Supreme', the corresponding item which are 'Family' should appear in the combobox as well as the quantity '9'.
First you need to load the listbox items in the combobox when you open the form with a for each loop.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each item As String In ServinSizeLB.Items
ServinSizeCB.Items.Add(item)
Next
For Each item As Integer In PizzaQtyLB.Items
PizzaQtyCB.Items.Add(item)
Next
End Sub
Then you add a SelectedIndexChanged event to your pizza listbox.
So as soon you select a pizza in the listbox you select the values of the other list-boxes in the combo-boxes.
But first you need to check if there is already a value selected in a listbox.
Otherwise the selection is gone when you select a different pizza.
"maybe i already know the size but i did change my mind about what pizza i want"
Private Sub PizzaLB_SelectedIndexChanged(sender As Object, e As EventArgs) Handles PizzaLB.SelectedIndexChanged
If ServinSizeLB.SelectedItem = Nothing Then
ServinSizeCB.SelectedIndex = 0
End If
If PizzaQtyLB.SelectedItem = Nothing Then
PizzaQtyCB.SelectedIndex = 0
End If
End Sub
You should not forget that the first item is at location 0
I don't know if you want them to select the serving size or pizza qty from the listbox or the combobox ...
but here also the code if its the listbox.
So make a SelectedIndexChanged even for serving size listbox and one for pizza qty listbox.
Becaus the items are in the same order in the listbox and combobox you link the combobox selecteditem with the listbox selected item.
Private Sub ServinSizeLB_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ServinSizeLB.SelectedIndexChanged
ServinSizeCB.SelectedIndex = ServinSizeLB.SelectedIndex
End Sub
Private Sub PizzaQtyLB_SelectedIndexChanged(sender As Object, e As EventArgs) Handles PizzaQtyLB.SelectedIndexChanged
PizzaQtyCB.SelectedIndex = PizzaQtyLB.SelectedIndex
End Sub
And the same you do with the listbox if you select a value in the combobox.
Private Sub ServinSizeCB_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ServinSizeCB.SelectedIndexChanged
ServinSizeLB.SelectedIndex = ServinSizeCB.SelectedIndex
End Sub
Private Sub PizzaQtyCB_SelectedIndexChanged(sender As Object, e As EventArgs) Handles PizzaQtyCB.SelectedIndexChanged
PizzaQtyLB.SelectedIndex = PizzaQtyCB.SelectedIndex
End Sub

Synchronizing three comboboxes

I have, 3 comboboxes loaded from database but not binded, with different data but same indexes.
All of them are setup like this:
ComboBox1.AutoCompleteMode = AutoCompleteMode.Suggest
ComboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
ComboBox1.AutoCompleteCustomSource = mycolumn1
ComboBox1.DropDownStyle = DropDownList
I would like to get functionality that when I choose an item in one combo that other two selects item with same index.
Foe start I am very surprised in that event _SelectedIndexChanged is never triggered while I expected to get index from there.
Why is this so and how to get desired functionality?
I am not sure your issue partially because you have no posted code for me to help you in your situation. Here is an example I done up for you. This is a quick one, but works; you can actually accomplish this in one procedure, but did this so you could understand the functionality of how this works.
Public Class Form1
'Always give variable a default value'
Private selectedIndex As Integer = 0
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim myArray() As String = {"1", "2", "3"}
ComboBox1.Items.AddRange(myArray)
ComboBox2.Items.AddRange(myArray)
ComboBox3.Items.AddRange(myArray)
End Sub
'Handles one of your comboboxes'
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
'Cast this as Integer for selected index and set your variable'
selectedIndex = CType(ComboBox1.SelectedIndex.ToString, Integer)
'Next lets make sure that we set the other comboboxes to this index'
ComboBox2.SelectedIndex = selectedIndex
ComboBox3.SelectedIndex = selectedIndex
End Sub
'Another one of your comboboxes'
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
'Cast this as Integer for selected index and set your variable'
selectedIndex = CType(ComboBox2.SelectedIndex.ToString, Integer)
'Next lets make sure that we set the other comboboxes to this index'
ComboBox1.SelectedIndex = selectedIndex
ComboBox3.SelectedIndex = selectedIndex
End Sub
'Your last combobox'
Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
'Cast this as Integer for selected index and set your variable'
selectedIndex = CType(ComboBox3.SelectedIndex.ToString, Integer)
'Next lets make sure that we set the other comboboxes to this index'
ComboBox1.SelectedIndex = selectedIndex
ComboBox2.SelectedIndex = selectedIndex
End Sub
End Class
* You must add the global variable to the top so it can be used to hold your current comboboxes selected index. You can also ignore the load event as I used this as a reference.
Thanks!