简体   繁体   中英

how to get the previously selected index of combo box in c#

I have this event for changing index of combo box;

DialogResult Result = MessageBox.Show("something", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

if (Result == DialogResult.No)
{
    // the code write follow
}

if (Result == DialogResult.Yes)
{
    NotGrazingradioButton.Checked = true;

    if (CowTypeSelect.SelectedIndex == 0)
    {
        CowTypeDefaults.LactatingCow(this);
        CowTypeVarlbl.Text = "گاو شیری";
    }
    else if (CowTypeSelect.SelectedIndex == 1)
    {
        CowTypeDefaults.DryCow(this);
        CowTypeVarlbl.Text = "گاو خشک";
    }

the problem is when user click on no button still the new index shows on combobox however it not selected,

I define a variable for previous selected index in an another event like this:

public int CowTypeSelect_SelectionChangeCommitted(object sender, EventArgs e)
{
    int PrevIndex = CowTypeSelect.SelectedIndex;
    return PrevIndex;
}

now I want to use this variable for changing combobox index to it's preview index like this but this code doesn't work and I don't know why

if (Result == DialogResult.No)
{
    CowTypeSelect.SelectedIndex = CowTypeSelect_SelectionChangeCommitted.PrevIndex;
}

now if assume that these code for no button work it become another problem because it select an Index and now codes runs again and values of textbox that related to combobox's index return to default value :(

I read many topics about my problem but I couldn't figure it out.

I guess that's what you want here:

private int prev_index = -1;

private void CowTypeSelect_SelectedIndexChanged(object sender, EventArgs e)
{
      DialogResult Result = MessageBox.Show
      ("somthing",MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
      if (Result == DialogResult.No)
      {
            CowTypeSelect.SelectedIndex = prev_index;
            return;
      }    
      else if (Result == DialogResult.Yes)
      {
            NotGrazingradioButton.Checked = true;

            if (CowTypeSelect.SelectedIndex == 0)
            {
                CowTypeDefaults.LactatingCow(this);
                CowTypeVarlbl.Text = "گاو شیری";
            }

            else if (CowTypeSelect.SelectedIndex == 1)
            {
                CowTypeDefaults.DryCow(this);
                CowTypeVarlbl.Text = "گاو خشک";
            }
     }
     prev_index = CowTypeSelect.SelectedIndex;
}

I come from the previous question you've asked, seems like you're quite new to Windows Form . But since you had tried it by yourself, It makes the ones who answer feel better.

  1. Add a global variable LastIndex storing the last verified index.

  2. Add a global variable InhibitIndexChange to check if the index change event is fired by the users. For more information about this, see Cancelling ListBox SelectedIndexChange Event

  3. Check InhibitIndexChange before processing the code you wish to execute during the IndexChangedEvent

  4. Check the message box result, if it's No , you should rollback the selected index, otherwise, update it to the newest index.

Full Code

int LastIndex = -1;
bool InhibitIndexChange = false;

private void CowTypeSelect_SelectedIndexChanged(object sender, EventArgs e)
{
    if(InhibitIndexChange)
        return;
    DialogResult Result = MessageBox.Show("Type text here", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
    if (Result == DialogResult.No)
    {
        if(LastIndex != -1)
        {
            InhibitIndexChange = true;
            CowTypeSelect.SelectedIndex = LastIndex;
            InhibitIndexChange = false;
        }
        return;
    }

    NotGrazingradioButton.Checked = true;

    if (CowTypeSelect.SelectedIndex == 0)
    {
        CowTypeDefaults.LactatingCow(this);
        CowTypeVarlbl.Text = "گاو شیری";
    }

    else if (CowTypeSelect.SelectedIndex == 1)
    {
        CowTypeDefaults.DryCow(this);
        CowTypeVarlbl.Text = "گاو خشک";
    }
    LastIndex = CowTypeSelect.SelectedIndex;
}

And for it to work on the previous question you asked

if (FirstRun == true)
{
    // Codes here execute at the first time only.
    ...
    LastIndex = CowTypeSelect.SelectedIndex;
    FirstRun = false;
    return;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM