ListBox (Multisimple) - Click Event: Way to handle unclicked items?
Hi all
List boxes have a click, selecteditem and selectedvalue events (among others). I was wondering... is there a way to handle unselected items?
For example, if you click one item in list box 1, several items are added to listbox 2... then, if you unselect the item in list box 1, those related items in list box 2 are deleted.
I'm just wondering how 'unselect' events are handled for list boxes.
Any help appreciated - thanks in advance.
GTD
Re: ListBox (Multisimple) - Click Event: Way to handle unclicked items?
What you're able to do is create a Dictionary that would store the index of the item clicked in ListBox1 as the key and an array of indices of the items added in ListBox2 as the Value. In the SelectedIndexChanged event for ListBox1, you would check if the SelectedItems count is more or less than the Dictionary count. If it's more then you know that the user is wanting to add values to ListBox2. If it's less then you know that the user is removing values from ListBox2. Here is a quick (free-typed) example:
Code:
Private indices As Dictionary(Of Integer, Integer()) = New Dictionary(Of Integer, Integer())
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ListBox1.SelectedIndexChanged
If indicies.Count < ListBox1.SelectedItems.Count Then
'Add values to ListBox2
Dim fooValues() As String = {"Foo1", "Foo2", "Foo3"}
indicies.Add(ListBox1.SelectedIndex, Enumerable.Range(ListBox2.Items.Count, ListBox2.Items.Count + fooValues.Length).ToArray())
ListBox2.Items.AddRange(fooValues)
Else
'Remove values from ListBox2
For Each index As Integer In indices.Values(ListBox1.SelectedIndex)
ListBox2.Items.RemoveAt(index)
Next
indices.Remove(ListBox1.SelectedIndex)
End If
End Sub
Re: ListBox (Multisimple) - Click Event: Way to handle unclicked items?
The event is always the SelectedIndexChanged event. If you select an item, you can see the new selected index. If you are interested in the previous selected index, then the thing to do is to store the current selected index (which probably starts out as -1) in some form level variable, then on the next SelectedIndexChanged event you have the current SelectedIndex, and the last selected index will be held in that form level variable. Just be sure to update that variable in the SelectedIndexChanged event handler.
Re: ListBox (Multisimple) - Click Event: Way to handle unclicked items?
Thanks for the input -- i'll have a play around and see if i can get this working.
Re: ListBox (Multisimple) - Click Event: Way to handle unclicked items?
This task seem like it should be simple... but its baking my noodle. Dont get me wrong... i get what you're saying... but cant get the result i need.
Shaggy Hiker: Can you give me example code of what you mean?
Re: ListBox (Multisimple) - Click Event: Way to handle unclicked items?
I amended a version of this... it kind of works... but not quite. It adds values to listbox2 but doesnt remove them. It also throws an error if you select and deselect one item.
Re: ListBox (Multisimple) - Click Event: Way to handle unclicked items?
I'm not at a computer where I can test out the code that I provided you with, but if I do get the chance tonight I'll work up an example for you.
Re: ListBox (Multisimple) - Click Event: Way to handle unclicked items?
Ok -- thanks again dday9 :)