1. 程式人生 > >CheckedListBox 實現RadioButton功能

CheckedListBox 實現RadioButton功能

使用CheckedListBox 實現RadioButton 的功能
花費了一些時間,並且期間有各種Bug ,其中最頭痛的是當對CheckedListBox 狀態進行改變時,會再次出發函式本身,夠成列傳遞函式,甚至死迴圈.

後來探索下面方法可以有效的解決問題

  private void checkedListBoxResult_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            checkedListBoxResult.ItemCheck -= checkedListBoxResult_ItemCheck;
            if (e.CurrentValue == CheckState.Checked)
            {
                e.NewValue = CheckState.Checked; 
            }                
            else
            {
                for (int i = 0; i < checkedListBoxResult.Items.Count; i++)
                {
                    
                    checkedListBoxResult.SetItemChecked(i, false);
                    
                }
                e.NewValue = CheckState.Checked;
            }
            checkedListBoxResult.ItemCheck += checkedListBoxResult_ItemCheck;
        }