0

3 つのチェックボックスがあり、そのうちの 1 つがチェックされていれば 2 つをオフにできます。しかし、3 つ目がチェックされている場合は、最初の 2 つをオフにする必要があります。どうやってやるの?

これが私が試みていることです。

private void chkResFoodVeg_CheckedChanged(object sender, EventArgs e)
{
    //disables the other checkbox if one is checked
    this.chkResFoodNveg.Enabled = !this.chkResFoodVeg.Checked;
}

private void chkResFoodNveg_CheckedChanged(object sender, EventArgs e)
{
    this.chkResFoodVeg.Enabled = !this.chkResFoodNveg.Checked;
}

private void chkResFoodBoth_CheckedChanged(object sender, EventArgs e)
{
    this.chkResFoodBoth.Enabled = !this.chkResFoodNveg.Checked &&
   !this.chkResFoodVeg.Checked;
}

コードの最後の部分が機能しません。最初の 2 つのチェックボックスをオフにする必要があります。

ありがとう

4

2 に答える 2

1

あなたのロジックは逆です。入力を結合するのではなく、2 つの割り当てが必要です。

private void chkResFoodBoth_CheckedChanged(object sender, EventArgs e)
{
    this.chkResFoodNveg.Enabled = !this.chkResFoodBot.Checked;
    this.chkResFoodVeg.Enabled = !this.chkResFoodBot.Checked;
}

ただし、ボタンのアクションをどのように説明しているかからすると、RadioButtonの動作をエミュレートしようとしているように思えます。代わりにそれを使用する必要があります。一度に 3 つのオプションから 1 つしか選択できません。

ここに画像の説明を入力

于 2013-09-15T23:54:27.027 に答える