Reputation: 11
I'm confused on how to get this program to do a few certain things and a question for future reference for myself:
Question: Can you do if statements with buttons example: if (button1 is clicked) do this else if (button2 is clicked) do this
The goal of my program is to help learn basic integer mathematics and the four sections are ADDING, SUBTRACTING, MULTIPLYING, and MOD. I have four buttons for each one of those topics, and a fifth button to submit the answer for the question, I have 3 text boxes, the first being the question that is presented, second being the users answer, third being whether the answer was correct or not
What I currently have set up:
private void button1_Click(object sender, EventArgs e)
{
Random random1 = new Random();
int randomNumber1 = random1.Next(6, 11);
Random random2 = new Random();
int randomNumber2 = random2.Next(1, 6);
textBox1.Text = "What is " + randomNumber1 + " + " + randomNumber2 + " ?";
}
private void button2_Click(object sender, EventArgs e)
{
Random random1 = new Random();
int randomNumber1 = random1.Next(6, 11);
Random random2 = new Random();
int randomNumber2 = random2.Next(1, 6);
textBox1.Text = "What is " + randomNumber1 + " - " + randomNumber2 + " ?";
}
private void button3_Click(object sender, EventArgs e)
{
Random random1 = new Random();
int randomNumber1 = random1.Next(6, 11);
Random random2 = new Random();
int randomNumber2 = random2.Next(1, 6);
textBox1.Text = "What is " + randomNumber1 + " * " + randomNumber2 + " ?";
}
private void button4_Click(object sender, EventArgs e)
{
Random random1 = new Random();
int randomNumber1 = random1.Next(6, 11);
Random random2 = new Random();
int randomNumber2 = random2.Next(1, 6);
textBox1.Text = "What is " + randomNumber1 + " % " + randomNumber2 + " ?";
}
private void button5_Click(object sender, EventArgs e)
{
Random random1 = new Random();
int randomNumber1 = random1.Next(6, 11);
Random random2 = new Random();
int randomNumber2 = random2.Next(1, 6);
if (Convert.ToInt16(textBox2.Text) == (randomNumber1) + (randomNumber2))
textBox3.Text = "Correct!";
else if (Convert.ToInt16(textBox2.Text) == (randomNumber1) - (randomNumber2))
textBox3.Text = "Correct!";
else if (Convert.ToInt16(textBox2.Text) == (randomNumber1) * (randomNumber2))
textBox3.Text = "Correct!";
else if (Convert.ToInt16(textBox2.Text) == (randomNumber1) % (randomNumber2))
textBox3.Text = "Correct!";
else
textBox3.Text = "Incorrect!";
}
}
}
and what I'm looking to do is if button 1 is clicked you add, button 2 you subtract, button 3 you multiply, button 4 you mod and then depending on which one was clicked you click submit and it'll tell you whether you got the right answer or not. What I currently have somewhat does that but if the answer is any of the four types of questions answers it'll show it as correct.
ie: the question is 8 + 3 and you put 5, it'll say correct because 8 - 3 is 5
Upvotes: 1
Views: 3444
Reputation: 100547
You are generating random (*) numbers to create question and again to validate it. It makes absolutely no sense since you have something like "What is 3 + 4" and you comparing result to 7 + 2
or some other random value.
You should save generated values and operation for verification in some member variable, or even easier simply save expected result.
Random random = new Random();
int expectedResult;
private void button3_Click(object sender, EventArgs e)
{
int randomNumber1 = random.Next(6, 11);
int randomNumber2 = random.Next(1, 6);
textBox1.Text = "What is " + randomNumber1 + " * " + randomNumber2 + " ?";
expectedResult = randomNumber1 * randomNumber2;
}
private void button5_Click(object sender, EventArgs e)
{
textBox3.Text =
int.Parse(textBox2.Text) == expectedResult ? "Correct!" : "Incorrect!";
}
(*) Note that your current "random" numbers are not exactly random due to your code restarting sequence on each call - so pairs of numbers would be very closely related. Note that numbers generated for expression will likely be different from numbers generated for the check. Proper usage is to have one single Random
object (potentially one-per-thread, but you don't need to worry about it for your case).
Upvotes: 0
Reputation: 63317
You can try the following code instead, you code has fairly many things redundant and the checking mechanism to determine if user answers correctly is totally wrong:
public enum Operation {
Add,
Subtract,
Divide,
Multiply,
Modulo
}
Random rand = new Random();
private decimal GenerateQuestion(Operation o){
int a = rand.Next(6, 11);
int b = rand.Next(1, 6);
decimal result = 0;
string os = "";
switch(o){
case Operation.Add:
result = a + b;
os = "+";
break;
case Operation.Subtract:
result = a - b;
os = "-";
break;
case Operation.Multiply:
result = a * b;
os = "*";
break;
case Operation.Divide:
result = (decimal)a/b;
os = "/";
break;
case Operation.Modulo:
result = a % b;
os = "%";
break;
}
textBox1.Text = string.Format("What is {0} {1} {2}?", a,os,b);
return result;
}
decimal result;
private void button1_Click(object sender, EventArgs e)
{
result = GenerateQuestion(Operation.Add);
}
private void button2_Click(object sender, EventArgs e){
result = GenerateQuestion(Operation.Subtract);
}
private void button3_Click(object sender, EventArgs e){
result = GenerateQuestion(Operation.Multiply);
}
private void button4_Click(object sender, EventArgs e){
result = GenerateQuestion(Operation.Modulo);
}
private void button5_Click(object sender, EventArgs e){
decimal v;
if(decimal.TryParse(textBox2.Text, out v)){
textBox3.Text = (v == result) ? "Correct!" : "Incorrect!";
}else {
textBox3.Clear();
MessageBox.Show("Enter a number please!");
}
}
Upvotes: 2
Reputation: 2395
I am assuming you are doing a Windows Application here (not Web). In that case, why don't you simply have a class level variable to store the last operation (subtract, add etc), and include check of that variable in the if condition.
eg:
public enum Operations
{
Add,
Subtract
}
private void button1_Click(object sender, EventArgs e)
{
Random random1 = new Random();
int randomNumber1 = random1.Next(6, 11);
Random random2 = new Random();
int randomNumber2 = random2.Next(1, 6);
var currentOperation = Operations.Add
textBox1.Text = "What is " + randomNumber1 + " + " + randomNumber2 + " ?";
}
if (Convert.ToInt16(textBox2.Text) == (randomNumber1) + (randomNumber2) && currentOperation == Operations.Add)
textBox3.Text = "Correct!";
Upvotes: 0
Reputation: 2152
You can assign one event handler to multiple buttons, and then check the EventArgs
to tell which button fired the event, and put appropriate conditional logic in there.
Not sure if this is ASP.NET or WinForms, but it works something along the lines of:
btn1.Click += button_Click;
btn2.Click += button_Click;
btn3.Click += button_Click;
private void button_Click(object sender, EventArgs e)
{
var btn = sender as Button;
if (sender != null) {
if (btn is btn1) {
//do something
}
else if (btn is btn2) {
//do something
}
//etc
}
}
I have added the event handlers declaratively but you could do this through the properties dialog or in the markup.
Upvotes: 0