2016-03-25 70 views
1

我有這個事件來改變組合框的索引;如何在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; 
} 

現在如果假設這些代碼沒有按鈕的工作,因爲它選擇的指數和現在的代碼再次運行和相關組合框的指數迴歸到默認值:(

文本框的值,我讀了很多它成爲另一個問題關於我的問題的主題,但我無法弄清楚。

回答

2

我來自the previous question你問,好像你是很新的Windows Form。但是,既然你自己嘗試過,它會讓那些回答更好的人感覺更好。

  1. 添加一個全局變量LastIndex存儲上次驗證的索引。

  2. 添加全局變量InhibitIndexChange來檢查用戶是否觸發了索引更改事件。有關更多信息,請參見Cancelling ListBox SelectedIndexChange Event

  3. 檢查InhibitIndexChange處理您希望IndexChangedEvent

  4. 檢查消息框結果期間執行的代碼之前,如果它是No,你應該回滾選定的指數,否則,將其更新爲最新的索引。

全碼

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

是我新的Windows窗體,但我學得很快,並請在這裏親愛的朋友的幫助下,我希望從你那裏學到很多東西。 所以如果我的問題有點簡單,我們的不專業人士會原諒我,但在我提出一個問題之前,我會嘗試很多次來自己解決問題,最後如果我找不到問題的答案,坦克如此之多: ) –

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; 
} 
相關問題