2010-03-31 29 views
14

是否可以取消WinForms應用程序上的列表框的SelectedIndexChange事件?這似乎是合乎邏輯的事情,我必須忽略一些簡單的功能。基本上,我一直在彈出一個消息框,詢問用戶是否真的想要移動到另一個項目,因爲這會改變用戶界面,我不希望他們的更改丟失。如果用戶沒有保存他們正在處理的內容,我希望能夠取消該事件。有沒有更好的方法來做到這一點?取消列表框SelectedIndexChange事件

+0

我與nawfal同意有關詳細信息,請查看以下鏈接.... http://www.mindstick.com/Articles/176c6d68-ceca-4072-a319-7389f4e5b9dd/?ListBox%20events %20in%20C%20#.Net – 2011-09-10 14:17:03

+0

[如何防止/取消c#中的組合框值更改]可能的重複?(http://stackoverflow.com/questions/314503/how-to-prevent-cancel-a- comboboxs值改變,在-C) – 2016-07-05 14:26:33

回答

16

您不能取消它。

我也只是幾天的什麼以前是有最新選擇的指數的變量。然後,當事件觸發時,您詢問用戶是否想要保存,這是在事件處理程序中完成的。如果用戶選擇「取消」,則再次更改ID。

問題是這會使事件再次發生。所以我用的是一個布爾只是說「抑制」。而在該事件處理程序的頂部,我有:

if(Inhibit) 
    return; 

然後低於這個,你問的問題,你做這樣的事:

DialogResult result = MessageBox.Show("yadadadad", yadada cancel etc); 
if(result == DialogResult.Cancel){ 
    Inhibit = true; //Make sure that the event does not fire again 
    list.SelectedIndex = LastSelectedIndex; //your variable 
    Inhibit = false; //Enable the event again 
} 
LastSelectedIndex = list.SelectedIndex; // Save latest index. 
+1

這一細微變化,我讀這個答案後發現 - 你可以刪除事件處理程序,而不是使用禁止標誌: 'list.SelectedIndexChanged - = list_SelectedIndexChanged; list.SelectedIndex = LastSelectedIndex; list.SelectedIndexChanged + = list_SelectedIndexChanged;' 可能有一些原因,這是一種低劣的方法,但工作得非常好,我的目的。 – TwainJ 2011-04-30 01:30:51

+0

@J Jones如果你減去兩次會發生什麼?不知道這是否會拋出異常? – 2011-05-01 12:07:00

+0

@OskarKjellin你不會做兩次,因爲這是在函數內。直到您稍後將其添加回來之後,才能再次調用該函數。 – Grungondola 2016-04-29 19:26:19

2

中的SelectedIndexChanged無法取消。所以你只有一個真正的選擇:

private int? currentIndex; 
public void ListBox_SelectedIndexChanged(sender, EventArgs args) { 
    if (currentIndex.HasValue && currentIndex.Value != listBox1.SelectedIndex) { 
     var res = MessageBox.Show("Do you want to cancel edits?", "Cancel Edits", MessageBoxButtons.YesNo); 
     if (res == DialogResult.Yes) { 
      currentIndex = (listBox1.SelectedIndex == -1 ? null : (int?) listBox1.SelectedIndex); 
     } else { 
      listBox1.SelectedIndex = currentIndex.Value; 
     } 
    } 
} 
5

我剛碰到這個確切的問題。我所做的是當用戶進行更改時,我設置了ListBox.Enabled = false;這不允許他們選擇不同的索引。一旦他們保存或放棄他們的更改,我設置ListBox.Enabled = true;可能不像提示那樣光滑,但它完成了工作。

+0

這太棒了。任何時候我開始不必擔心跟蹤當前索引的私有成員變量,或者跟蹤一些抑制布爾值,我開始思考:如何避免添加這個額外的邏輯層?禁用ListBox會照顧到這一點。 – JustLooking 2015-05-11 19:46:47

9

這正是@Oskar Kjellin的方法,但與一捻。也就是說,一個變量減少並且具有選定索引的變化事件的行爲就像選定索引變化事件。我經常想知道爲什麼即使點擊完全相同的選定項目,選定的索引更改事件也會被解僱。這裏沒有。是的,這是一個偏差,所以要加倍確定你是否希望它在那裏。

int _selIndex = -1; 
    private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (listBox1.SelectedIndex == _selIndex) 
      return; 

     if (MessageBox.Show("") == DialogResult.Cancel) 
     { 
      listBox1.SelectedIndex = _selIndex; 
      return; 
     } 

     _selIndex = listBox1.SelectedIndex; 
     // and the remaining part of the code, what needs to happen when selected index changed happens 
    } 
0

更優雅,使用Tag屬性:

 if ((int)comboBox.Tag == comboBox.SelectedIndex) 
     { 
      // Do Nothing 
     } 
     else 
     { 
      if (MessageBox.Show("") == DialogResult.Cancel) 
      { 
       comboBox.SelectedIndex = (int)comboBox.Tag; 
      } 
      else 
      { 
       // Reset the Tag 
       comboBox.Tag = comboBox.SelectedIndex; 

       // Do what you have to 
      } 
     } 
0

這是我的方式來取消SelectionChange的組合框。我認爲它也適合ListBox。

private bool comboBox_CancelSelection = false; 
private int comboBox_LastSelectedIndex = -1; 

private void comboBox_SelectedIndexChanged(object sender, EventArgs e) { 
    if (comboBox_CancelSelection) { 
     comboBox_CancelSelection = false; 
     return ; 
    } 

    // Handle Event 

    if (!comoBox_CancelSelection) { 
     comboBox_LastSelectedIndex = comboBox.SelectedIndex; 
    } else { 
     comboBox.SelectedIndex = comboBox_LastSelectedIndex; 
    } 
}