2

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.

2 Answers 2

2

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;
}
1
  • 1
    Yes I'm new to Windows Form, But I'm a quick Learner and whit the help of my dear friends like you in here I hope to learn a lot from you. so if my questions are a bit simple our unprofessional forgive me, but before i ask a question I try many many time to solve it by myself and finally if I couldn't find out a way I ask my question, tanks so much :)
    – Mojtaba SH
    Mar 25, 2016 at 17:51
2

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.