# .Net listbox ? - 'Latest' item selected in multi-mode

**URL:** <https://boards.straightdope.com/t/net-listbox-latest-item-selected-in-multi-mode/507415>\
**Category:** Factual Questions\
**Created:** [August 23, 2009, 8:18pm UTC](https://boards.straightdope.com/t/net-listbox-latest-item-selected-in-multi-mode/507415 "2009-08-23T20:18:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SirRay](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@SirRay](https://boards.straightdope.com/u/SirRay)\
**Post date:** [August 23, 2009, 8:18pm UTC](https://boards.straightdope.com/t/net-listbox-latest-item-selected-in-multi-mode/507415/1 "2009-08-23T20:18:08Z")

</div>

My googling on the usual programmer’s forums is weak, as I found very little in regard to this (age-old) questions - perhaps someone can help?

Translating a program of mine that I use at home from VB6 to C#:  
Listbox in multiextended selection node, user (well, me) selects a number of items one-at-a-time using ctrl-click - now, the problem is the (.Net) SelectedItem remains set at the first (lowest # index) item in the selected items collection, but I’d like to preview the last/current item selected - meaning, for example, if I select (one at a time) L2 then L7 then L4, I’d like to preview (again one at a time) L2 then L7 then L4, which using VB6 is easy as the ‘latest’ item selected is correctly returned, but in C# the selecteditem stays at L2 (why, I can’t figure out) and so that doesn’t work.  
I found one solution which involved comparing arrays (snapshots) of the selected collection whenever SelectedIndexChanged is called, but I wonder (hope) that a more elegant and simple solution is out there.  
Any ideas?

(oh, to prevent confusion like I saw in at least 3 responses on the various developer forums, I know I can get the LAST item in the SelectedItem collection using SelectedItems.Count - 1, but I need the LATEST item just selected (via Ctrl+Click, or Shift+Click etc), which can be before the last item selected (again, L2-\>L7-\>L4), so selection one at a time I need L2, then L7, then finally L4 - the selected collection would have 3 items, ordered as L2, L4, L7, and SelectedItem remains at L2 throughout this exercise - this is useless to me :mad:

---

<div class="post-metadata">

**Author:** ![Khadaji](https://avatars.discourse-cdn.com/v4/letter/k/9e8a1a/32.png) [@Khadaji](https://boards.straightdope.com/u/Khadaji)\
**Post date:** [August 23, 2009, 9:28pm UTC](https://boards.straightdope.com/t/net-listbox-latest-item-selected-in-multi-mode/507415/2 "2009-08-23T21:28:00Z")

</div>

You might try this (I’ve not tested it much, so there may be a problem I haven’t seen.)

Put in a listbox mouse up event. Call the listbox’s _IndexFromPoint_, using the and y parts of the _e_ argument. Check to see if that item is in the selected array. If it is, display it.

private void listBox1\_MouseUp(object sender, MouseEventArgs e)  
{  
int jj = listBox1.IndexFromPoint(e.X, e.Y);  
object Test = listBox1.Items[jj];  
if(listBox1.SelectedItems.Contains(Test))  
label1.Text = listBox1.Items[jj].ToString();  
}

---

<div class="post-metadata">

**Author:** ![ZipperJJ](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/zipperjj/32/211_2.png) [@ZipperJJ](https://boards.straightdope.com/u/ZipperJJ)\
**Post date:** [August 24, 2009, 1:34am UTC](https://boards.straightdope.com/t/net-listbox-latest-item-selected-in-multi-mode/507415/3 "2009-08-24T01:34:55Z")

</div>

You can use Javascript with [ASP.NET](http://ASP.NET) too. Add a Javascript function call to your listbox (I think OnChange maybe?) and then a JS function on your page that every time the listbox is clicked, add the value of the selected item to a string and set the string as the value of a hidden field.

I do believe that OnChange will fire every time something is clicked, whether it’s being selected or de-selected, so you’ll want to remove the string if it exists in the hidden value.

When the form is submitted, you can get the value of the hidden field and parse each item as an array value “in order.”

I think my idea is exactly the same as **Khadaji** ’s but using JS instead of C#.

Also, just in case you don’t know, [ASP.NET](http://ASP.NET) web apps change your object IDs when they get to the browser, so you’ll need to address them as getItemById(‘\<%=TheIDYouUsed.ClientID%\>’).

---

<div class="post-metadata">

**Author:** ![SirRay](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@SirRay](https://boards.straightdope.com/u/SirRay)\
**Post date:** [August 24, 2009, 3:17am UTC](https://boards.straightdope.com/t/net-listbox-latest-item-selected-in-multi-mode/507415/4 "2009-08-24T03:17:15Z")

</div>

Just saw these two posts, Khadaji’s idea looks interesting and cleaner than the before & after array comparisons I saw - I will definitely try it.  
ZipperJJ, I am programming in a pure C# environment (not web), so I’m not using Javascript - however, that would be a good idea if I go to a web environment.  
Thanks

---

<div class="post-metadata">

**Author:** ![Khadaji](https://avatars.discourse-cdn.com/v4/letter/k/9e8a1a/32.png) [@Khadaji](https://boards.straightdope.com/u/Khadaji)\
**Post date:** [August 24, 2009, 12:19pm UTC](https://boards.straightdope.com/t/net-listbox-latest-item-selected-in-multi-mode/507415/5 "2009-08-24T12:19:29Z")

</div>

> [@SirRay](#):
>
> Just saw these two posts, Khadaji’s idea looks interesting and cleaner than the before & after array comparisons I saw - I will definitely try it.  
> ZipperJJ, I am programming in a pure C# environment (not web), so I’m not using Javascript - however, that would be a good idea if I go to a web environment.  
> Thanks

Be sure to give me credit if you post it anywhere! 😉
