C# Hello World Program in Different Styles

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:

  1. Open your Microsoft Visual Studio 2010 or higher.
  2. Click File then select New Project.
  3. Select C#, then Windows Form Application and click OK.
    c#HelloWorld1
  4. Designing the form in C# is same with VB.Net, just drag and drop the controls to the form.
  5. We will now add component or controls to the form.
    1. 4 buttons
    2. 1 label
  6. The design of the form. You can also design it according to you taste.
    c#HelloWorld2

Figure 1

  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
}
  1. 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.

  1. 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!";
  1. 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.
  1. 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.

  1. Press F5 to run the project. Don’t forget to save.

Download

, , , , ,

Post navigation