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.
- Open MS Visual Studio 2010.
- Create a new project (File, then New Project).
- Select Visual C# from the list and select Windows Form Application.
- We will add 4 buttons to the form.
- 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");
- The second button is to disable or to deactivate the button from being click.
button2.Enabled = false;
- 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!";
- The hide me button will make the button invisible. Double click the hide me button and paste the code.
button4.Visible = false;
- Save the test the simple project.