I’m currently working on a homework assignment where I need to set up a calculator-type program. It needs to read one or two user-input values (depending on the calculation), and then perform the calculation based on the values.
I currently have
- 2 textBoxes (
tbInput1andtbInput2), - 4 radioButtons,
- one button (
btnCalc) - a blank
labelin which the result will be displayed.
Two of the radioButtons (rbtnTrap and rbtnFak) disable the first textBox when checked; the other two need two values to be entered, and for that reason enable both textBoxes when checked. btnCalc is supposed to enable itself when the relevant number of textBoxes have value – the relevant number of textBoxes depends on which radio button is checked.
The problem is that when I check rbtnTrap or rbtnFak (disabling tbInput1) and enter an integer in tbInput2, btnCalc stays disabled.
I’ll try to explain what I have so far:
- In the
_TextChangedevent fortbInput1, I have an exact copy of the secondifblock posted below.tbInput1is only active whenrbtnPotORrbtnFibare checked, so that control only runs when that is the case. - In the
_TextChangedevent fortbInput2, I have the below, sincetbInput2is always enabled, and the control must run no matter which radio button is checked, although the control should run differently if i checkrbtnTrapORrbtnFakas opposed torbtnPotORrbtnFib.
Or that’s my understanding of it. I’m certainly open to suggestions and corrections.
private void tbInput2_TextChanged(object sender, EventArgs e)
{
//For single-field values
if ((rbtnTrap.Checked || rbtnFak.Checked) &&
!string.IsNullOrWhiteSpace(this.tbInput2.Text))
{
btnCalc.Enabled = true;
}
else
{
btnCalc.Enabled = false;
}
// For multi-field values
if ((rbtnPot.Checked || rbtnFib.Checked) &&
(!string.IsNullOrWhiteSpace(this.tbInput1.Text)
&& !string.IsNullOrWhiteSpace(this.tbInput2.Text)))
{
btnCalc.Enabled = true;
}
else
{
btnCalc.Enabled = false;
}
}
Because it is a homework, I am not going to give your the straight answer, but instead a hint: the problem is in the code of the
tbInput2_TextChangedevent (code that you posted).When
rbtnTraporrbtnFakare checked, your code will be run following such a path you don’t think it will. Set a breakpoint (F9) on the first line of thetbInput2_TextChangedcode and run the code step by step (F10) after entering some text intbInput2.You will see why your button
btnCalcis enabled as you think it is, but disabled in the next moment.Feel free to comment if you need more help afterwards. 🙂
EDIT
The problem comes from your
ifblocks. When one ofrbtnTrapandrbtnFakis checked:ifclause of the first block and then dobtnCalc.Enabled = true;but…elseclause of the second block (because(rbtnPot.Checked || rbtnFib.Checked)is false) and thus dobtnCalc.Enabled = false;.