VB Studio ListBox scrollbar position - vb.net

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

Related

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.NET Deleting last item from 2 listboxes at once won't work

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

Deleting multiple or single items from list box

I have created a list box and at the moment I am trying to make it such that you can delete one item or multiple items using this code:
Private Sub Delbtn_Click(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Delbtn.Click
lstCountries.Items.Remove(lstCountries.SelectedItem)
lstCountries.Items.Remove(lstCountries.SelectedItems)
End Sub
However when using this I am not able to delete more than one selection at a time. What is the best way to make it so that I can delete one or more than one selection?
First, make sure there are some things selected:
If lstCountries.SelectedItems.Count > 0 Then
' MUST loop backwards thru collections when removing
' or you will remove the wrong things, miss stuff and
' run out early
For n As Integer = lstCountries.SelectedItems.Count - 1 To 0 Step -1
' remove the current selected item from items
lstCountries.Items.Remove(lstCountries.SelectedItems(n))
Next n
End if
There is also a SelectedIndicies Collection which would return a collection of integers of the items. If you iterate that, use .RemoveAt() but you still need to loop backwards.

Storing listbox item indices for persistent selection

I've had such fantastic help so far. I've got a listbox that automatically populates with the folder names of the path that the user browses to. When the browse button is clicked and the listbox populates the first folder is automatically selected:
ListBox1.SetSelected(0, True)
Next I wrote some code so that if the user tries to unselect the only folder, then it holds its selected state:
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedItems.Count = 1 Then
My.Settings.Index1 = ListBox1.SelectedItem
End If
If ListBox1.SelectedItems.Count = 0 Then
ListBox1.SetSelected(My.Settings.Index1, True)
End If
End Sub
However, ideally I need to hold the indices of all selected items in memory so that when the form reloads I can restore the selection along with the list box items. I think I would like to do this when the item count is greater than one. But I need some suggestions on how to do this. Not necessarily examples, but just the methodology or approach so I can make an attempt. Many thanks!!!

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