Getting the old selected index in the Winform Combo field

I have a combo box (winform). This combo box has some elements (e.g. 1,2,3,4).

Now, when I change the selection inside this combo, I want to know the old index and the new index .

How to get it?

Possible approaches that I want to AVOID .

  • Add an enter event , cache the current index, and then when changing the index of the selection, get a new index.

  • Using the selected text property / selected item received by the event sender.

What I ideally want:

  • In the case of getting the arguments, I want something like:

    e.OldIndex; e.newIndex;

    At the moment, the event arguments received in the SelectionIndex Change event are completely useless.

  • I do not want to use more than one event.

  • If C # doesn't offer this, can I pass in my event, which passes the old index and the new index as event arguments?

+5
source share
3 answers

This seems like a possible duplicate

ComboBox SelectedIndexChanged event: how to get a previously selected index?

There is nothing built-in, you will need to listen to this event and track it in a class variable.

But this answer seems to offer a reasonable way to extend combobox to track the previous index fooobar.com/questions/1124585 / ...

+6
source

1- 2-Bind a Button ( "prevB" )
3 - ComboBox

//initilize List and put current selected index in it

List<int> previousScreen = new List<int>();
previousScreen.Add(RegionComboBox.SelectedIndex);    

//Button Event
 private void prevB_Click(object sender, EventArgs e)
    {
        if (previousScreen.Count >= 2)
        {
            RegionComboBox.SelectedIndex = previousScreen[previousScreen.Count - 2];
        }
    }
0

You will need to replace the ComboBox with the following control:

public class AdvancedComboBox : ComboBox
{
    private int myPreviouslySelectedIndex = -1;
    private int myLocalSelectedIndex = -1;

    public int PreviouslySelectedIndex { get { return myPreviouslySelectedIndex; } }

    protected override void OnSelectedIndexChanged(EventArgs e)
    {
        myPreviouslySelectedIndex = myLocalSelectedIndex;
        myLocalSelectedIndex = SelectedIndex;
        base.OnSelectedIndexChanged(e);
    }
}

Now you can get the property PreviouslySelectedIndex.

0
source

All Articles