As for button1.click apply an if loop so that when the button is pressed, the code is executed, for example: if the button is pressed, then assign the value in textBox1 to 'a'. What form should I write it in?

  • one
    Cycle if O_o - Specter
  • Well, I probably did not put it that way, I want to do this: I will write the code for Delphi, maybe it will be clearer: if button1.click then a: = b + c; Now you need to write this code for c #. - navi1893
  • one
    You will get a more accurate answer to your question if you formulate it more competently. if is not a loop, and neither loops nor conditions are associated with button click handlers. You could write something like: "How can I make certain code run when I press a button?" Did you want to ask this? - Modus Nov.
  • Yes! or rather, if the button is pressed, can you help to execute a specific code? - navi1893

1 answer 1

A double click on the button will automatically create a handler for you, in which you will add the behavior you need. For your example, something like this will be:

 void button1_Click(object sender, EventArgs e) { a = textBoxName.Text; } 

It's not entirely clear why you added the if keyword to the question and also called it a cycle. I can only assume that you want to do some behavior, depending on the button you clicked, but at the same time in one developer. For such an implementation, you need to select all the necessary buttons and again, automatically create one handler for them all. To determine which of the buttons was pressed, you need to set the sender event parameter to the type of the button and "look" by its properties.

 var btn = sender as Button; ...