8

I have a combo box (winform). This combo box has some items (eg. 1,2,3,4).

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

How do I get this?

Possible approaches that I wish to AVOID.

  1. Add an enter event, cache the current index and then on selection index change get the new index.

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

What I ideally want:

  1. In the event args that are received, I want something like:

    e.OldIndex; e.newIndex;

    Right now the event args which are received in the SelectionIndex Change event are totally useless.

  2. I don't want to use more than one event.

  3. If C#, does not offer this, can I have my event which passes the old index and new index as event args?

John C
  • 3,052
  • 3
  • 34
  • 47
Nikhil
  • 1,279
  • 2
  • 23
  • 43
  • Hi Nikhil, you can do it using one variable and without any events, if you agree the idea tell me to Post the code as an answer. – Mahdi Tahsildari Aug 23 '12 at 08:13
  • ComboBoxEdit by devexpress has EditValueChanging event.if you don't want to do anything with index and just want text property then it is better control than standard combobox. See http://documentation.devexpress.com/#WindowsForms/DevExpressXtraEditorsBaseEdit_EditValueChangingtopic – Ravi Patel Aug 23 '12 at 08:20
  • 1
    possible duplicate of [ComboBox SelectedIndexChanged event: how to get the previously selected index?](http://stackoverflow.com/questions/3237885/combobox-selectedindexchanged-event-how-to-get-the-previously-selected-index) – Rawling Aug 23 '12 at 08:20

5 Answers5

6

Seems like this is a possible duplicate

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

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

But this answer seems to suggest a sensible way of extending the combobox to keep track of the previous index https://stackoverflow.com/a/425323/81053

Community
  • 1
  • 1
Chris Moutray
  • 18,029
  • 7
  • 45
  • 66
0

1-Make a List of integers
2-Bind a Button to switch to previous Screen (button Name "prevB")
3-change the ComboBox Index as Per described in the code

//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];
        }
    }
Abdulrehman
  • 61
  • 1
  • 3
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 PreviouslySelectedIndex property.

Kapten-N
  • 212
  • 1
  • 10
0

You can use YourComboBox.Tag (or other unused string/int property) to store old selected index...

Sergio Ross
  • 51
  • 1
  • 4
  • Thanks for your contribution! Please note however that the original author explicitly wanted to avoid caching (unless I misunderstood your answer). It is also great if you include code snippets with your answer. – mzuther Jan 29 '20 at 16:22
0

I use such pair

comboBox.SelectedItem new item

comboBox.SelectionBoxItem old item

Dmitrij
  • 11
  • 2