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:
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.
You can use Javascript with 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 web apps change your object IDs when they get to the browser, so you’ll need to address them as getItemById(‘<%=TheIDYouUsed.ClientID%>’).
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