如何在c#中获取以前选择的组合框索引

时间:2016-03-25 13:27:15

标签: c#

我有这个事件用于更改组合框的索引;

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 = "گاو خشک";
    }

问题是,当用户点击没有按钮时,新索引仍显示在组合框上,但未选中,

我在另一个事件中为之前选择的索引定义了一个变量,如下所示:

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

现在我想使用这个变量将组合框索引更改为它的预览索引,但这段代码不起作用,我不知道为什么

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

现在如果假设这些代码没有按钮工作就会成为另一个问题,因为它选择了一个索引,现在代码再次运行,与combobox索引相关的文本框值恢复为默认值:(

我读了许多关于我的问题的话题,但我无法理解。

2 个答案:

答案 0 :(得分:2)

我想这就是你想要的:

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

答案 1 :(得分:2)

我来自the previous question您已经问过,似乎您对Windows Form还不熟悉。但既然你已经亲自尝试过,那就让那些回答的人感觉更好。

  1. 添加一个存储最后验证索引的全局变量LastIndex

  2. 添加全局变量InhibitIndexChange以检查用户是否触发了索引更改事件。有关此问题的详细信息,请参阅Cancelling ListBox SelectedIndexChange Event

  3. 在处理您希望在InhibitIndexChange

  4. 期间执行的代码之前,请先检查IndexChangedEvent
  5. 检查消息框结果,如果是No,则应回滚所选索引,否则,将其更新为最新索引。

  6. 完整代码

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

    要让它在the previous question上工作,你问了

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