I am trying to make a little game in C#.
The program asks the user for a any number.
The user then presses “GO” (button1) and the program checks whether the number is an even number or not. (x % 2 == 0)
I’m trying to get the program to show 4 checkboxes/radio buttons out of total of 8 depending on each case.
For example:
- If the number is an EVEN NUMBER: The program will show options 2,5,3,6.
- If the number is an ODD NUMBER: The program will show options 1,4,7,8.
(Options 1-8 were already included in the design.)
I need help with the if (x % 2 == 0) part. What do I write in it to make the checkboxes/radiobuttons appear or disappear?
By the way, is there a way to ask the user for a number without him having to click “GO”?
Like, use ENTER instead. If yes, what event is that?
Also, is there a way to limit the textbox to INT only?
I know it’s asking you to do the job, but I have tried, and I’m still a real beginner, therefore I think my way of learning is by actually experiencing it.
public partial class Form1 : Form
{
int x;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
x = int.Parse(textBox1.Text);
if (x % 2 == 0)
{
}
}
}
The
Visibleproperty of theCheckBox(inherited fromControl) will aid you in making the needed controls visible when you wish them to be. Your if would look something like,However, this can be optimized a bit by using the condition to set the visibility of all the
CheckBoxs at the same time instead of coding two conditionals – something like:As for on pressing enter, check the
OnKeyDownevent for you control, you can do this through the designer. Your event method would look like:This should help you with your problems.
ADDITIONAL RESPONSE