I am currently using the code in main.cs to draw out the GUI instead of using the [design] form. I understand that if you are using the [design] file to draw your GUI, all you have to do to create a event handler is just to double click the object.
But since I am using the code the draw out the labels/buttons, how do I do a event handler for that particular button/object?
p.Controls.Add(new Button
{
Text = "Clear",
Name = "btnClear",
Location = new Point(416, 17)
});
e.g. how do I add an event handler for the above code?c
You need to first create an instance of your button and assign it to a variable. Then you can add an event handler by calling
+=on the Click event of the button.In Visual Studio, after typing
btnClear.Click +=you can just press Tab twice and it will add the rest of the code.