2013-12-19 85 views
1

我想創建一個if語句來提示我是否確定要檢查複選框。如果該複選框已被選中,則取消選中該複選框。以下是我似乎無法工作的內容。謝謝!如果有人有這樣做的簡單方法,請告知。如果複選框選中其他操作取消選中

if (checkBox15.Checked == false) 
{ 
    MessageBox.Show("Are you sure you want to check this?", "Prompt", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) 
    { 
     Updatelist(); 
    } 
    else 
    { 
     checkBox15.Checked = false; 
     return; 
    } 
} 
else if (checkBox15.Checked == true) 
{ 
    checkBox15.Checked = false; 
    return; 
} 
+0

在什麼情況下是你的代碼?你使用的是什麼UI?窗體形式? – RononDex

+0

這是MouseClick事件。 –

回答

4

你已經錯過了if(

if(MessageBox.Show("Are you sure you want to check this?", 
    "Prompt", 
    MessageBoxButtons.YesNo, 
    MessageBoxIcon.Question) == DialogResult.Yes) 
{ 
    Updatelist(); 
} 
else 
{ 
    checkBox15.Checked = false; 
    return; 
} 
+2

這也取決於*哪裏*這個方法被調用。如果裏面檢查改變了事件處理程序,這將創建無限遞歸 - > stackoverflow。在這種情況下,您可能需要使用一些布爾開關。 – Tigran

+0

@Tigran它正在創建一個遞歸。任何關於交換機看起來如何的建議? –

+0

@hexc:創建一個實例布爾值,它在* checkBox15.Checked = false;之前切換值(比如變爲true)*。並且在事件處理程序中檢查該布爾值是否爲真,將其設置爲false(將其重置爲原始值)並返回,因此不要處理任何事情,因爲checkBox15.Checked = false;碼。 – Tigran

0

這是你的答案

if (checkBox15.IsEnabled == false) 
{ 
MessageBox.Show("Are you sure you want to check this?", "Prompt", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) 
{ 
    Updatelist(); 
} 
else 
{ 
    checkBox15.IsEnabled = false; 
    return; 
} 
}`enter code here` 
else if (checkBox15.IsEnabled == true) 
{ 
checkBox15.Checked = false; 
return; 
相關問題