Results 1 to 8 of 8

Thread: ListBox (Multisimple) - Click Event: Way to handle unclicked items?

  1. #1

    Thread Starter
    Lively Member GTDriver's Avatar
    Join Date
    Apr 2015
    Location
    Blighty
    Posts
    66

    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

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Lively Member GTDriver's Avatar
    Join Date
    Apr 2015
    Location
    Blighty
    Posts
    66

    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.

  5. #5

    Thread Starter
    Lively Member GTDriver's Avatar
    Join Date
    Apr 2015
    Location
    Blighty
    Posts
    66

    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?

  6. #6

    Thread Starter
    Lively Member GTDriver's Avatar
    Join Date
    Apr 2015
    Location
    Blighty
    Posts
    66

    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.

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  8. #8

    Thread Starter
    Lively Member GTDriver's Avatar
    Join Date
    Apr 2015
    Location
    Blighty
    Posts
    66

    Re: ListBox (Multisimple) - Click Event: Way to handle unclicked items?

    Ok -- thanks again dday9

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width