Preventing a listbox SelectedIndexChanged event

  • Thread starter Thread starter Fred Iannon
  • Start date Start date
F

Fred Iannon

All,
Please consider the following scenario:

I have a listbox with a number of employee names as part
of a larger form which contains data about those employees.

If the user selects a new employee from the listbox, I
would like to prompt them to save/delete/"cancel the
operation" for any data changes which were made to the
previously edited employee. IF they press "cancel" I want
to PREVENT the listbox index change from occuring.

Is there a good way to do this? I need to be able to
cancel the event, however once I have received the
SelectedIndexChanged event it is to late, and I canNOT
find another event that gives me a chance to cancel the
index change before it happens (like a Validating event
PER listbox entry....)

I know this sounds like schoolwork, but it is not <LOL> I
really liked .NET forms at first (as compared to MFC),
however as I started using them for real-life applications
IMHO there are still a number of "common" problems (like
this one) that do not seem to be readily solvable without
trickery....(previously I posted about some shortcomings
with the Validating event and, IMHO, the keypress event
also has some definite room for improvement since you
canNOT necessarily tell what the "new" text will be until
AFTER it has already been populated...consider
cut/paste/highlighted selections, etc....)

Thanks for any help,
Fred Iannon
 
Fred Iannon said:
All,
Please consider the following scenario:

I have a listbox with a number of employee names as part
of a larger form which contains data about those employees.

If the user selects a new employee from the listbox, I
would like to prompt them to save/delete/"cancel the
operation" for any data changes which were made to the
previously edited employee. IF they press "cancel" I want
to PREVENT the listbox index change from occuring.

Is there a good way to do this? I need to be able to
cancel the event, however once I have received the
SelectedIndexChanged event it is to late, and I canNOT
find another event that gives me a chance to cancel the
index change before it happens (like a Validating event
PER listbox entry....)


private m_SelectedIndex as integer
'...
sub SelectedIndexChanged (..)

if me.listbox.selectedindex = m_selectedindex then return

if All-Checks-Are-OK then
'....
m_SelectedIndex = me.listbox.selectedindex
else
me.listbox.selectedindex = m_SelectedIndex
end if

end sub
 
Unfortunately, the listbox index visually "changes" to
the "new" item and then visually changes back to the
previous item if this method is used...

I was trying to find a way to intercept (and hopefully be
able to cancel) the action BEFORE it occurred....
 
Just an idea, but couldn't you do something with the 'MouseDown' event on
the listbox?
 
Back
Top