VB.NET Deleting last item from 2 listboxes at once won't work - vb.net

I'm working on a program called "Smart Search". In the "customize search" form,
I made it possible to delete items. Well, the problem here is that it's two listboxes. One for what you should search for and one for what happens when you search: https://i.imgur.com/aGZRoeX.png
Oh, and btw. When you select an item in one of the listbox, the same selectedindex will be selected in the other.
But when I delete the last item(s) in the list I get a error.
I think it's something wrong with my code but I can't find anything wrong with it.
The code: http://pastebin.com/LRBfx5DL

Try something like this:
Private Sub rembtn1_Click(sender As Object, e As EventArgs) Handles rembtn1.Click
Dim index As Integer = ListBox1.SelectedIndex
If index <> -1 Then
ListBox1.Items.RemoveAt(index)
If index < ListBox2.Items.Count Then
ListBox2.Items.RemoveAt(index)
End If
save()
End If
End Sub

Related

Visual Basic, Listbox, is it possible to check if an item gets unselected

I want when the user using the program selects something in the list box, and then selects something else, between that action i want the program to do something, I hope it's clear what I mean.
Thanks in advance.
You would have to handle the SelectedIndexChanged event or the like and then remember the previous item yourself, e.g.
Dim previousSelectedIndex As Integer = -1
Private Sub ListBox1_SelectedIndexChanged(...) Handles ListBox1.SelectedIndexChanged
If previousSelectedIndex <> -1 Then
'Use previousSelectedIndex here.
End If
previousSelectedIndex = ListBox1.SelectedIndex
End Sub
If you wanted to, you could create your own derived class inheriting ListBox and build that functionality into it.

How can I filter a ListView using TextBox and ComboBox?

Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
ListView2.Items.Clear()
Dim curSelectionCombobox As String = ComboBox2.SelectedItem.ToString()
ListView2.Items.Add(listitm)
End Sub
Well basically this is what ive come up with the filtering thing in combobox which is obviously wont work
and in the combobox and button i didnt get to try coding those but im quite sure it wont work either im new in this language and im struggling to catch up giving the fact that this pandemic really gets me more and more stupider as the day passed by
Well my main problem is that the filtering in the groupBox_bookShelf is when i choose a genre in the combobox the static listview will filter leaving only the exact items its genre selected in the combobox
the second is the combobox and button im aiming to link the action of both property when filing in the groupBox_bookInformation then once the filter button is clicked i want to filter the lower listview leaving only the selected genre and its items
Here is the sample form ive been working on.
enter image description here
I am guessing that what is selected in the combo box is a value that appears in some of your list view items.
Start by calling .BeginUpdate() This will prevent the user interface from repainting on each update which would really slow things down.
I loop through the items and test one of the sub items to see if it matches the combo selection. If it does not match is is removed.
Be sure to call .EndUpdate or the changes will not show up.
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
Dim curSelectionCombobox As String = ComboBox2.SelectedItem.ToString()
ListView2.BeginUpdate()
For Each lvi As ListViewItem In ListView2.Items
If lvi.SubItems(6).Text <> curSelectionCombobox Then
lvi.Remove()
End If
Next
ListView2.EndUpdate()
End Sub

VB Studio ListBox scrollbar position

Perhaps there's a better solution, but...
I am using a listbox to select multiple items, but the list contains 800+ items in alphabetical order. If I am selecting three items, for example, "Apples", "Oranges" and "Zucchini", I have to pull the scrollbar down or use the mouse wheel to scroll through the list.
Is there any way to "jump" to a location in the list to reduce the scrolling needed? For example, if I had a button for "D", the scroll list would jump down to the items starting with "D".
Any suggestions would be appreciated.
This should work as you need.
The second line searches through the listbox to find the first item that begins with the key you pressed. If no Items begin with that letter, the FindString function returns -1 or the same as ListBox.NoMatches ( which is a constant already defined in Visual Studio to make code more readable)
I've put the code into a separate sub so that it can be resused to apply to any listbox.
Private Sub ListBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ListBox1.KeyDown
Dim key As String = Chr(e.KeyValue).ToString
SelectlistBoxItemFromKey(ListBox1, key)
End Sub
Private Sub SelectlistBoxItemFromKey(ByRef lBox As ListBox, k As String)
Dim index As Integer = lBox.FindString(k)
If index > ListBox.NoMatches Then
lBox.SelectedIndex = index - 1
End If
End Sub

Visual Basic.Net stopping user selecting listbox items

How do I make it so that the user can't select the first two items in a list box. I'm trying to make a program that runs when the user changes their selected index, but the first two items are going to be headings for a table so I do not want the program to run if the user tries to select them.
You could just unselect them again:
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedIndex < 2 Then
If ListBox1.Items.Count < 2 Then
ListBox1.SelectedIndex = -1
Else
ListBox1.SelectedIndex = 2
End If
End If
End Sub
It does have -5 elegance points, the kind you can get back by using a Label above the ListBox or by using a ListView instead.
As with Hans' answer, it's not very clean but might I suggest:
ListBox1.RemoveAt(0)
ListBox1.RemoveAt(1)
It won't work if the list is databound, but if that's the case then you might like to try
ListBox1.DataSource = linqQuery.Skip(2).ToList()

Adding a default number of items to list box in Visual Basic

I'm working on a project for my Visual Basic class and need a nudge in the right direction. I'm not going to go into exactly what the program is supposed to do in the end because I'm not really at that point yet. The step I'm stuck on now is this.
We are supposed to have two list boxes, one for Ingredients (which I've named lstIngredients) and the second for the Recipe (lstRecipe), we are also supposed to have a text box labeled Quantity for the user to enter how many of the selected item to add to the Recipe list. For example, if the user selects "eggs" and types 3 for the quantity, "eggs" should appear 3 times in the Recipe list box. Also, if nothing is put into the quantity box, it is supposed to default to adding one of the selected item to the Recipe list box.
With the code I have written, I am able to add items to the Recipe list as long as I type something into the quantity text box, but I cannot get the program to just add one when nothing is typed into the text box. Here is the code I have so far.
Public Class Form1
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim i As Integer = lstIngredients.SelectedIndex
If txtQuantity.text= "" Then
lstRecipe.Items.Add(1)
End If
Dim intCount As Integer = 0
While intCount < txtQuantity.Text
lstRecipe.Items.Add(lstIngredients.Items(i))
intCount += 1
End While
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
lstRecipe.Items.Clear()
txtQuantity.Clear()
End Sub
Any help on this step would be greatly appreciated. I'm sure I'll probably have more questions as I get farther into the coding, but I will edit this question to include those when the time comes.
First, you'll need to convert the value in your Quantity text box to an integer. For that, you can use either Integer.Parse or Integer.TryParse. For instance:
Dim value As Integer = Integer.Parse(Quantity.Text)
Then you can use a For loop to add the same item that many times, for instance:
For x As Integer = 1 to value
lstRecipe.Items.Add(lstIngredients.Items(i))
Next