I am very new to this site. I am a bachelor student, I am a bachelor of computer applications. I am making a simple program in Visual Studio using C # and I ran into the same problem, how to check if a button is pressed? I wanted to do it
if(-button1 is clicked-) then { this should happen; } if(-button2 is clicked-) then { this should happen; }
I did not know what to do, so I tried to find a solution on the Internet. I have many solutions that did not help me. So, I tried something on my own and did it,
int i; private void button1_Click(object sender, EventArgs e) { i = 1; label3.Text = "Principle"; label4.Text = "Rate"; label5.Text = "Time"; label6.Text = "Simple Interest"; } private void button2_Click(object sender, EventArgs e) { i = 2; label3.Text = "SI"; label4.Text = "Rate"; label5.Text = "Time"; label6.Text = "Principle"; } private void button5_Click(object sender, EventArgs e) { try { if (i == 1) { si = (Convert.ToInt32(textBox1.Text) * Convert.ToInt32(textBox2.Text) * Convert.ToInt32(textBox3.Text)) / 100; textBox4.Text = Convert.ToString(si); } if (i == 2) { p = (Convert.ToInt32(textBox1.Text) * 100) / (Convert.ToInt32(textBox2.Text) * Convert.ToInt32(textBox3.Text)); textBox4.Text = Convert.ToString(p); }
I declared the variable "i" and assigned it different values ββin different buttons and checked the value i in the if function. It worked. Give your suggestions, if any. Thanks.
source share