2014-05-01 18 views
0

我有一個複選框,在檢查時將進度條增加20%,未選中時減少20%(或者至少這是我想要它)。它的作用是增加,但是當我取消選中該框時,VS尖叫着我,並告訴我「'-20'的值對'Value'無效,'Value'應該在'minimum'和'maximum'之間。任何幫助表示讚賞。我需要從進度條中減去設置的金額並繼續收到超出範圍的消息

這裏是我的代碼:

private void chkAlpha_Click(object sender, EventArgs e) 
{ 
    if (chkAlpha.Checked == true) 
    { 
     pbWaitMessage.Value = (intPBValue + intIncrementValue); 
    } 
    if (chkAlpha.Checked == false) 
    { 
     pbWaitMessage.Value = (intPBValue - intIncrementValue); 
    } 
} 

我以前聲明以下變量:

int intPBValue = 0; 
int intIncrementValue = 20; 
+0

不應該有範圍檢查。例如。 Inc只有當該值小於100時,纔會減少20,並且只有當該值大於0時才減少20. – srsyogesh

回答

2

你是不是曾經設定intPBValue的價值。

你可以使用:

if (chkAlpha.Checked == true) 
{ 
    pbWaitMessage.Value += intIncrementValue; 
} 
if (chkAlpha.Checked == false) 
{ 
    pbWaitMessage.Value -= intIncrementValue; 
} 

[編輯]

或者重構按照評論:)

if (chkAlpha.Checked) 
{ 
    pbWaitMessage.Value += intIncrementValue; 
} 
else 
{ 
    pbWaitMessage.Value -= intIncrementValue; 
} 
+1

如果布爾值不爲true,它將爲false;)你明白我的意思了嗎? –

+0

值是一個整數。我只是在原始問題中複製了結構,而不是重構它。這不是代碼出現問題的地方。 –

+1

夠公平的,但教育OP不會傷害你是我的感受。 –

0

試試這個:

int intIncrementValue = 20; 


private void chkAlpha_Click(object sender, EventArgs e) 
{ 
    if (chkAlpha.Checked) 
    { 
    if(pbWaitMessage.Value + intIncrementValue <= pbWaitMessage.Maximum) 
     pbWaitMessage.Value = pbWaitMessage.Value + intIncrementValue ; 
    } 
    else 
    { 
    if(pbWaitMessage.Value - intIncrementValue >= pbWaitMessage.Minimum) 
     pbWaitMessage.Value = pbWaitMessage.Value - intIncrementValue ; 
    } 
} 
1

您需要修改intPBValue一個d確保數值不會超出範圍...

private void chkAlpha_Click(object sender, EventArgs e) 
{ 
    if (chkAlpha.Checked) 
    { 
     intPBValue += intIncrementValue; 
    } 
    else 
    { 
     intPBValue -= intIncrementValue; 
    } 

    if (intPBValue < 0) 
    { 
     intPBValue = 0; 
    } 
    else if (intPBValue > 100) // assuming 100 is the max 
    { 
     intPBValue = 100; 
    } 

    pbWaitMessage.Value = intPBValue; 
} 
相關問題