Problem:
Write a program in c# that will display Hello World in a form of a message box, in a caption or text of the button, label and even in the text or caption of the another form.
Solution:
We will create a step by step tutorial on how to display Hello World in different styles using the Windows Form Application in C#, Windows Form Application will allow us to develop an application with GUI or Graphical User Interface.
C# is one of the programming languages included in the Microsoft Visual Studio. The others are VB.Net, Visual C++ and Visual F#.
Let’s begin the tutorial:
- Open your Microsoft Visual Studio 2010 or higher.
- Click File then select New Project.
- Select C#, then Windows Form Application and click OK.
- Designing the form in C# is same with VB.Net, just drag and drop the controls to the form.
- We will now add component or controls to the form.
- 4 buttons
- 1 label
- The design of the form. You can also design it according to you taste.
Figure 1
- Next is we’re going to display the Hello World in a message box. Double clickt the Message Box button and paste the code.
MessageBox.Show("Hello World!");
The code above must be place between the lines of:
private void button1_Click(object sender, EventArgs e)
{
// paste code here
}
- Next is in the text of the button. Double click the button with the text Button (refer to Figure 1). Paste the code
button2.Text = “Hello World!”;
The same with the previous button, the code must be on the Click event of the button.
- The third one is the text of the label. Double click the button with the Label text or caption. Paste the code
label1.Text="Hello World!";
- Lastly is to display the message Hello World in another form. We will add a form to our c# project. In the Project menu select Add Windows Form, the select Windows Form and click Add.
- Back to the first form. Double click the Form button and paste the following code.
Form2 f2 = new Form2();
f2.Show();
f2.Text = "Hello World!";
The newly create form will be open if the user clicks the Form button and display the Hello World in the caption or header part of the form.
- Press F5 to run the project. Don’t forget to save.