I’m trying to create a class that inherits from ListBox and adding the property PreviousSelectedIndex to it. So far all good.
Next, I’m trying to set that property before the SelectedIndex is changed.
The problem is that I cannot find the method on which the property SelectedIndex is changed so I can capture it. I always can change it manually on the object itself but that is only a workaround. Here is what I got:
public class MyListBox : System.Windows.Forms.ListBox
{
public int PreviousSelectedIndex { get; set; }
public MyListBox() : base()
{
this.PreviousSelectedIndex = -1;
}
protected override void OnClick(EventArgs e)
{
this.PreviousSelectedIndex = base.SelectedIndex;
base.OnClick(e);
}
}
When I’m trying:
MessageBox.Show(
"Previous Index = " + listBox4.PreviousSelectedIndex +
"\nCurrent Index = " + listBox4.SelectedIndex
);
I always get the same value.
Also I tried it with OnMouseDown() and OnSelectedIndexChanged() with no luck.
For a Winform, you could do:
IF you want to change to WPF, you can get the previous item from the SelectedChanged event of the ListBox