Button Control Demo in C#

A button is the most common graphical control in any programming languages. It is a control the user can click to provide input to an application.

In this simple tutorial, we will learn how to open a messagebox when a user clicks a button, change the text or caption of the button in runtime, disable the button (user cannot click or use the button) and hide or make the button invisible.

Let’s start this simple project.

  1. Open MS Visual Studio 2010.
  2. Create a new project (File, then New Project).
  3. Select Visual C# from the list and select Windows Form Application.
  4. We will add 4 buttons to the form.
    button1
  5. The first button is on displaying a message box, double click the button and paste the code below. The message states that you have clicked button1.
MessageBox.Show("you clicked button1");
  1. The second button is to disable or to deactivate the button from being click.
button2.Enabled = false;
  1. The third button will change the text or caption of the button. The initial caption is change text, but once you have click the button the text will be changed to hello world!.
button3.Text = "hello world!";
  1. The hide me button will make the button invisible. Double click the hide me button and paste the code.
button4.Visible = false;
  1. Save the test the simple project.
, , , , ,

Post navigation