Buttons are one of the simplest controls and mostly used for executing some code when the user wants.
Here we have a really simple case, show a Message box when a button is clicked. We add a button to a form, name it cmdShowMessage
as used in code to identify the object and set the buttons text to Show Message.
We just need to double click the button on the visual designer and Visual Studio will generate the code for the click Event. Now we just need to add the code for the MessageBox there:
private void cmdShowMessage_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello world!");
}
If we run the program now and click the button we'll see the message appearing: