I'm trying to find a way to determine which item was selected last in a listbox with a MultiSimple selectionmode. I'm currently doing it using a mouse point in the mousedown event, but I'd like to find a way to do it in either the listbox's click event or the listbox's selectedIndexChanged event. I'm trying to make a listbox select either "ALL" OR some. Any suggestions on how to do this or another way to do it will be apprecitated.
My CODE:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Me.ListBox1.Items.Clear() Me.ListBox1.Items.Add("ALL") Me.ListBox1.Items.Add("111") Me.ListBox1.Items.Add("222") Me.ListBox1.Items.Add("333") Me.ListBox1.Items.Add("444") Me.ListBox1.Items.Add("555") Me.ListBox1.Items.Add("666") Me.ListBox1.Items.Add("777") Me.ListBox1.Items.Add("888") Me.ListBox1.Items.Add("999") Me.ListBox1.SelectionMode = SelectionMode.MultiSimple Me.ListBox1.SelectedIndex = 0 End SubPrivate Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown Try Dim sAllString As String = "ALL" If Me.ListBox1.SelectedItems.Count = 0 Then Me.ListBox1.ClearSelected() Me.ListBox1.SelectedItems.Add(sAllString) Else Dim pt As Point = New Point(e.X, e.Y) Dim index As Integer
index =
CType(sender, ListBox).IndexFromPoint(pt) If index = Me.ListBox1.Items.IndexOf(sAllString) Then Me.ListBox1.ClearSelected() Me.ListBox1.SelectedItems.Add(sAllString) Else If Me.ListBox1.SelectedItems.Count > 1 AndAlso _ Me.ListBox1.SelectedItems.Contains(sAllString) Then Me.ListBox1.SelectedItems.Remove(Me.ListBox1.Items.Item(Me.ListBox1.Items.IndexOf(sAllString))) End If End If End If Catch ex As Exception Throw ex End Try End Sub

Find the last item selected in a MultiSimple listbox...
SARay
Works very well. The only thing I would add would be a "Last Clicked Item" property, since the last selected item is not always the last clicked item.
Private _LastSelectedItem As New Object
Public ReadOnly Property LastSelectedItem() As Object
Get
If IsNothing(_LastSelectedItem) Then
Return Nothing
Else
Return _LastSelectedItem
End If
End Get
End Property
Private _LastClickedItem As New Object
Public ReadOnly Property LastClickedItem() As Object
Get
If IsNothing(_LastClickedItem) Then
Return Nothing
Else
Return _LastClickedItem
End If
End Get
End Property
Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
Dim posX As Integer = mylistbox.MousePosition.X
Dim posY As Integer = mylistbox.MousePosition.Y
Dim lastitem As Object = Me.Items(Me.IndexFromPoint(Me.PointToClient(New Drawing.Point(posX, posY))))
_LastClickedItem = lastitem
If Me.SelectedItems.Contains(lastitem) Then
_LastSelectedItem = lastitem
End If
MyBase.OnClick(e)
End Sub
Kenneth Alexander
Darkwings
To find the last Item selected in a multi select
Dim s as string = Me.ListBox1.SelectedItems(Me.ListBox1.SelectedItems.Count - 1).ToString
which is the last item added to the collection
RichardS71
latestIndex always comes back as the last item in the selecteditems collection. I'm trying to find out which item in the selecteditems collection was clicked. For examle if they click "ALL", I want to get an index of 0. If they click "555", I want to get an index of 5, etc.
In this example, latestIndex is always the highest integer index of all the selected items. So, if you click "ALL", it is 0. Then you click "555", it is 5. Then, you click "333" it is 5 again. and so on until you click an item that has a higher index than the current highest index.
Pricerage
Hi,
Did you try this (code is in C#-sorry)
private void ListBox1_MouseDown(object sender, MouseEventArgs e){
System.Windows.Forms.
ListBox.SelectedIndexCollection coll= this.ListBox1.SelectedIndices; int latestIndex = coll[coll.Count - 1];//Get latest selected indexwhich may replace
index = CType(sender, ListBox).IndexFromPoint(pt)
}
Thuy MSFT
Maybe I confused the question. I'm not looking for the last item added to the selecteditems collection. I'm looking for the last (or current) item (text in this case) that was clicked on.
Johan Johansson
This custom list box adds a property that shows the last item that was selected.....
Public Class mylistbox
Inherits ListBox
Private _LastSelectedItem As New Object
Public ReadOnly Property LastSelectedItem() As Object
Get
If IsNothing(_LastSelectedItem) Then
Return nothing
Else
Return _LastSelectedItem
End If
End Get
End Property
Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
Dim posX As Integer = mylistbox.MousePosition.X
Dim posY As Integer = mylistbox.MousePosition.Y
Dim lastitem As Object = Me.Items(Me.IndexFromPoint(Me.PointToClient(New Drawing.Point(posX, posY))))
If Me.SelectedItems.Contains(lastitem) Then
_LastSelectedItem = lastitem
End If
MyBase.OnClick(e)
End Sub
End Class
Rochak Agrawal
tralala