I'm not really sure how to accomplish this task and would like some help with ideas or, if someone knows how to do this, some advice would be greatly appreciated!

I am working on a custom usercontrol. The usercontrol has a listbox (or rather it IS a listbox, just docked to fill the control surface), anyway, I need to catch/intercept when a user changes the selected index of the listbox in the control. The listbox is bindable (it has the DataSource, DisplayMember, SelectedValue and ValueMember properties exposed) and the binding works great! However, I want to give my control two custom events, one that executes when the selected index is about to change but before it has, and one after the selected index has changed.

I know how to implement custom events, that's not the problem, the problem is that since the listbox control does not have an event for selectedindexchanging and only one for selectedindexchanged, I can't seem to figure out the best way to "intercept" the index change before it actually occurs.

I don't know if that actually makes much sense, I hope it does.

Also, since the control I am creating has a base of UserControl and not ListBox the listbox events and properties are not exposed when the usercontrol is added to the design surface, and they are not editable in code. I would need to implement them on my own to make them public, which is exactly what I want, but I need some of the listbox events to be accessable, so they can be subscribed to.

To get to where I am just create a custom usercontrol, and when the design surface appears for that control, throw a listbox on it, and now imagine you want to find out when the selected index on that listbox is about to change and when it has changed. That's about the extent of it. I'm not sure where to start. The selectedindexchanged event already exists, so that's an easy one, but knowing before the change occurs is the show stopper for me.

Any help would be greatly appreciated!

The event fires as the first consequence, so you can consider SelectedIndexChanged as the "Changing" event.

How are you supposed to know what's going to change and to what value before it happens? At best you'd be able to use the click event but even at that point, presumably you've clicked on the list box and changed the index already...

I'm pretty sure that the only way to reliably do this is to record the most recent state before the change and the do your comparisons against that. That way if any of your validation fails you can fall back onto your previous state.

Failing that you could always override WNDPROC to try and intercept the event before it even gets fired in .NET but I can't say that this way is elegant.


Some controls do support events like I am describing, but since this is a custom control, I'm having difficulties on two levels, one, the listbox properties and events aren't accessable directly to the user of the control, two, the listbox doesn't have an event for SelectedIndexChanging.

I don't really care what the value of the index is going to be before it changes, I just need to know that it is about to change, but hasn't yet. This is something that can be done when you create custom events, it's not complicated.

I guess what I really need to know is, given a control, any control, and you derive from it, can you override events and replace them with your own?

Nevermind, I think I figured it out.

Instead of inheriting from UserControl (which provides the benefit of hiding some events and properties that I didn't want visible) I just inherited from ListBox instead and used an override on the SelectedIndex property, and called my code in the set accessor before calling base.SelectedIndex = value;

Here's a simple snippet of what I did:

namespace X.XxX.UX.Controls
    public partial class ServicesRenderedList : ListBox
        public ServicesRenderedList()

        protected override void OnPaint(PaintEventArgs pe)

        public override int SelectedIndex
                return base.SelectedIndex;
                // my code for SelectedIndexChanging goes here

                base.SelectedIndex = value;

Just thought I would share in case someone was interested in doing the same.

Although you've solved your own question, I'm curious as to why you need to know that something is about to change before it's changed. You have no way to stop the change, so I'm not sure what the difference would be having foreknowledge of the change.

I'm not criticising, I'm just curious why you need this kind of solution.