winforms Basic controls Button

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

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.

enter image description here

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:

enter image description here



Got any winforms Question?