When starting out with any new programming language, one of the first things you must learn how to do is output information to the screen.
Since time immemorial, the program that has been teaching students to perform this task is the much-adored "Hello World!" application.
The "Hello World!" application has one goal and one goal only: to display a text message to the application's user. The method used to display this message makes little difference -- though it is typically in the programming language of choice's standard output. It can also be shown as output to a command prompt, an input box, a text box, or an alert box.
Today we are going to learn how to create a "Hello World!" application in Microsoft's Visual C# 2008 .NET programming language using a Windows Forms Application.
Since Windows Forms Applications don't produce a standard output, we will be using the System.Windows.Forms class to display a MessageBox.
- The first thing you want to do is open Visual Studio by going to the Start Menu and opening 'Microsoft Visual Studio 2008.'
- On the Visual Studio Start Page, click 'Create: Project...' under the Recent Projects heading.
- The 'New Project' window will open and allow you to change the Name, Location, and Solution Name of your new project. You can change the default values to reflect the purpose of your application. Click the 'OK' button to create your project.
- The Visual Studio Form Designer will open showing a visual representation of your form (Form1). Double-click on the client area (the grey part) of the form to open up the Code Window for Form1's Load event.
- The code inside the Form1_Load event should look like this:private void Form1_Load(object sender, EventArgs e) {}The curly braces are used to delineate the start and the end of a block of code. All of the code we add to the Form's Load event will need to go between these curly braces.
- Between the curly braces, add the following:MessageBox.Show("Hello World!");
- The resulting code should now look like this:private void Form1_Load(object sender, EventArgs e) {MessageBox.Show("Hello World!");}Visual C# is a highly case and syntax sensitive language. You must add the text exactly as shown -- in Visual C#, the term "MessageBox" has a different meaning than "messagebox." Be sure that your syntax, such as the parenthesis and semicolons, are all correct.
- Press the F5 key to run the project. The project should compile and a messagebox window should pop up with the exclamation "Hello World!"
If you experienced any errors during compiling, go back and make sure that your code is identical to the code shown in step 6. If you need to make changes do so and then press F5 to restart your project.
Congratulations. You now know the basics of producing a simple Windows application using the Visual C# programming language.
Join the Conversation