Removing the last item in a listbox? vb 2008 - vb.net

I have a simple question. I have a listbox called listbox1. I want to add a button that removes the last item on the bottom whatever it maybe, when the button is clicked.
But I don't know how to finish it. something simple like listbox1.removeitem(some number?)
What could I add to remove whatever item that is on the bottom on the list?

This will remove the last item in your ListBox. Using the RemoveAt method.
ListBox1.Items.RemoveAt(ListBox1.Items.Count - 1)
Using the RemoveAt method, we remove the item at the Index provided. Since we get the full length of items in your current ListBox (and subtract 1 because it is zero based) we get the last item.

Use the RemoveAt method and get the last element by it's count - 1 since it's zero based array.
Listbox1.Items.RemoveAt(Listbox1.Items.Count - 1)

Related

When removing items from a combobox the last item is removed but the text remains... how do I make the combobox blank after last item is removed?

Really simple question but just can't find an answer anywhere.
I've created a command button that removes items from a list in a combobox based on which item they have selected. When the last item is removed, it "technically" is removed but the text for the last item remains in the combobox field. This would confuse the user; in this instance they would believe an item is still attached (the list of items are file pathways.)
I've tried convoluted routes e.g. setting the first index field as "No attachments" and building a solution around this. e.g. when the last field is removed, set it to "no attachments." but this requires me to initialise the combobox this way and change other areas of my code which just unnecessarily complicates it all.
Just a brief description of code, it takes a boolean which the user prompts by choosing either yes to removing all or pressing a remove selected option
Private Sub rmvFile(removeAll As Boolean)
If removeAll = False Then
'trying to work out how to remove selected item
fileListComboBox.RemoveItem (fileListComboBox.ListIndex)
Else
Do While fileListComboBox.ListCount > 0
fileListComboBox.RemoveItem (0)
Loop
End If
End Sub
The result I would expect is that when I remove the last item in the combobox it would be blank, but this doesn't happen. The last items text remains in the combobox, confusing the user.

Comparing two list boxes Vb.net

Listbox 1 contains names of hairdressers and Listbox 2 contains services provided by all of them then in Listbox 3 both the selected hairdresser(only 1 allowed) and and selected services are contained. There is a remove button which removes items from Listbox 3. I want a code for the button that if a hairdresser is removed all services also get removed otherwise only services are removed.
For Each str As String In Hairdresser.lstHairdresser.Items
If Not lstHairdresserAndServices.Items.Contains(str) Then
lstHairdresserAndServices.Items.Clear()
Else
'more code here but above statement never gets true
End If
Next
If the hairdresser is always the first item in the 3rd listbox, all you have to do is check if the selectedindex equals 0. If so, clear the listbox. Otherwise just remove the selected item.
Alternatively, you could search the the 1st listbox for the selected string. If found clear the listbox otherwise remove the selected item.
It is also possible to mark the listbox items when you add then to the listbox. Set the tag property to something like "hairdresser" or "service" and when the remove button is clicked all you have to do is check the tag property.
Finally I would recommend a different approach: Instead of adding the items to the listbox, fill a data structure with the hairdresser's name and services, display this structure in the listbox and when the remove button is clicked compare the selected item to the structure to find out what was selected.

VB Getting the selected item from a List View

I have a list view with two columns and I'd like to be able to save the value of the leftmost column for the selected row, or even better make it so that once the user clicks on either the right or left column of any given row, the entire row selects and not only the field that was clicked.
However I'm struggling to get the field saved which is more crucial than the row highlighting.
In a list box it would be
string = listbox1.selecteditem.tostring
However this doesn't seem to work for the list view. It won't even let me put "Selecteditem" and instead requires I put selecteditems, however this doesn't seem to do what I want either.
When I use the code:
string = ListView1.SelectedItems.ToString
I get the result of
string = "System.Windows.Forms.ListView+SelectedListViewItemCollection"
Despite the selected field actually being "EGG".
I need to have two columns so can't switch to using a listbox, although that seems like it would be the easier solution.
When I tried googling this question I could only find things for C#
Set FullRowSelect on to get the entire row to select.
SelectedItems.ToString refers to the collection of selected items.
SelectedItems(0).Text refers to the first selected item's text property.

MS Access 2003 - Automatically show last records in list box on a form rather than first

So I have a form that has a listbox that shows like a ledger. My question is how can I make it display the last records (or have the scroll bar default to the bottom instead of the top), instead of the first few as the default.
Now I don't mean reversing the order from bottom to top instead of top to bottom (though that would be a cool thing to learn how to do), just simply having the bottom of the list (in terms of the scroll bar) shown and the default, so that it is always showing the last 10 or so records (based on the size that I made the list box).
So i think this is simple, but then again, I obviously do not know?!?!
Thanks!
In a suitable event, such as the current event:
Me.ListX.Selected(Me.ListX.ListCount - 1) = True
You could add some code to the form load event so that it will do this:
YourListBox.SetFocus
YourListBox.ListIndex = YourListBox.ListCount - 1
YourListBox.Selected(YourListBox.ListCount - 1) = False
It basically selects the last item in the list box so it will scroll down to it, and then unselects it.
I know this is later but maybe this will help someone in the future who comes upon this thread. This is the code I used to go to the last record then unselect the last record.
YourListBox.SetFocus
YourListBox.Selected(YourListBox.ListCount - 1) = True
YourListBox.Selected(YourListBox.ListCount - 1) = False
How did you set the listbox items? Are they from a database? If yes, then you need to update the SQL statement with an "order by columnName".

Trying To Add Element To Array

Using 3.5 VB1 framework.net
I'm trying to add an element to an array
i would like to clear the listbox and display the array contents in listbox.
then add another button, then add an element to the array, from the textbox.
Ive created this painstakingly for the past 6 hours
Call clearout() ''===== Clears listbox
Dim MyNumbers(4) As Integer
Dim i As Integer
MyNumbers(0) = 1
MyNumbers(1) = 2
MyNumbers(2) = 3
MyNumbers(3) = 4
MyNumbers(4) = 5
For i = 0 To 4
ListBox1.Items.Add(MyNumbers(i))
Next i
That part works great!
It plops it right into listbox and deletes any previous entries into listbox
What Ive studied so far after all these hours to make the next button is to use UBound function to find the highest element then add one to it and ReDim it with that value
Problem is I'm not sure how to write that correctly under the second button
Any help?
I'm not quite clear what you are trying to achieve, but this is my interpretation:
Button1: clears the listboxpopulates the array with numbers 1 to 5populates the listbox with these numbers
Button 2: adds a number to the array from a textboxadds this new number from the array to the listboxORsimply increments the values in the array by 1 and append this new value to the listbox?
In any case, you need to share the array between the buttons so it must be declared with module level scope. That is, outside the button click routines.Also, in order to preserve the values already in the array, you need to use ReDim Preserve MyNumbers(newUBound)Hopefully the above tips will help!PS. Does your clearout() method simply call ListBox1.Items.Clear() ? If it does, it's best just to call this inline rather than create a new method to do this.