C # Check if button is pressed

I am making a program that should be continued if 2 conditions are given. The first 2 TextBoxes has the same word and a button is pressed that opens a new form. You see that I am trying to create a registration form;) Now I have an event for the "full" button.

private void button2_Click(object sender, EventArgs e) { if (textBox2.Text == textBox3.Text && ???) { StreamWriter myWriter = File.CreateText(@"c:\Program Files\text.txt"); myWriter.WriteLine(textBox1.Text); myWriter.WriteLine(textBox2.Text); 

My problem is that I cannot find a method that gives something like button1.Click or something similar.

I hope someone can help me here.

+6
source share
4 answers

Click is an event that fires immediately after releasing a mouse button. Therefore, if you want to check the handler for button2.Click , if button1 was previously pressed, all you could do was have a handler for button1.Click , which sets the bool flag of your own value to true.

 private bool button1WasClicked = false; private void button1_Click(object sender, EventArgs e) { button1WasClicked = true; } private void button2_Click(object sender, EventArgs e) { if (textBox2.Text == textBox3.Text && button1WasClicked) { StreamWriter myWriter = File.CreateText(@"c:\Program Files\text.txt"); myWriter.WriteLine(textBox1.Text); myWriter.WriteLine(textBox2.Text); button1WasClicked = false; } } 
+21
source

It helped me a lot: I wanted to save the values ​​from my gridview, and it reloaded my gridview / overriding my new values, since I have IsPostBack inside my PageLoad.

 if (HttpContext.Current.Request["MYCLICKEDBUTTONID"] == null) { //Do not reload the gridview. } else { reload my gridview. } 

SOURCE: http://bytes.com/topic/asp-net/answers/312809-please-help-how-identify-button-clicked

0
source

button1, button2 and button3 have the same even handler

 private void button1_Click(Object sender, EventArgs e) { Button btnSender = (Button)sender; if (btnSender == button1 || btnSender == button2) { //some code here } else if (btnSender == button3) //some code here } 
0
source

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.

0
source

Source: https://habr.com/ru/post/945018/


All Articles