How do I make my first “Hello, WinForm” .NET form application without VS.NET in Visual C#?

C# Version

Building the “Hello WinForm” Application Let’s now build our first windows application called “Hello WinForm”. The application will present a simple window with “Hello WinForm” greeting at the center. The source code of the program is:

using System;
using System.Windows.Forms;
using System.Drawing;
namespace CSharpSchool
{
	class Test
	{
		static void Main()
		{
			Application.Run(new MyWindow());
		}
	}
	class MyWindow : Form
	{
		public MyWindow() : base()
		{
			this.Text = "My First Windows Application";
			this.Size = new Size(300, 300);
			Label lblGreeting = new Label();
			lblGreeting.Text = "Hello WinForm";
			lblGreeting.Location = new Point(100, 100);
			this.Controls.Add(lblGreeting);
		}
	}
}
Understanding the Code
In the start, we included three namespaces to our application
using System;
using System.Windows.Forms;
using System.Drawing;


The System namespace, as we stated in the first lesson, is the necessary ingredient of all C# applications. In fact, the Application class that we used later in the Main() method is defined in this namespace. The System.Windows.Forms namespaces contains the base classes for windows controls like Form, Label and Button. Finally, including the System.Drawing namespace is necessary as it contains the useful classes related to the drawing of controls. The Size and Point classes used later in the program are actually defined in System.Drawing namespace.

Later, we derived a new class ‘MyWindow’ from the Form class defined in

System.Windows.Forms 
	class MyWindow : Form
	{
	...
	}


In the constructor of MyWindow, we specified the size and title of the form. The size is defined using the System.Drawing namespace’s Size class. We passed two integers to the constructor of Size to specify the width and the height of the form.

	public MyWindow() : base()
	{
		this.Text = "My First Windows Application";
		this.Size = new Size(300, 300);


Next in the constructor, we created a text label and added it to the Controls collection of the Form. A text label is used to write some text on the form. The System.Windows.Forms.Label class is used to create a text label in windows applications. We set the text of the Label using its Text property which is of string type. All the controls contained by a form must be added to its Controls collection; hence we have also added our label to this collection.

	public MyWindow() : base()
	{
		this.Text = "My First Windows Application";
		this.Size = new Size(300, 300);
		Label lblGreeting = new Label();
		lblGreeting.Text = "Hello WinForm";
		lblGreeting.Location = new Point(100, 100);
		this.Controls.Add(lblGreeting);
	}


Finally, we have created a Test class containing the Main() method. In the Main() method, we have instantiated the MyWindow class and passed its reference to the Application.Run() method so it may receive messages from the Windows Operating System.

When we execute the above code, the following screen is presented as output



To close the application, press the close button on the title bar.

Adding Event Handling Let’s now add a button labeled ‘Exit’ to the form. The ‘Exit’ button will close the application when it is clicked. In .NET, Push Buttons are instances of the System.Windows.Forms.Button class. To associate some action with the button click, we need to create an event handler and register (or add) it to the Button’s Click event. Following is the code for this modified application

using System;
using System.Windows.Forms;
using System.Drawing;
namespace CSharpSchool
{
	class Test
	{
		static void Main()
		{
			Application.Run(new MyWindow());
		}
	}
	class MyWindow : Form
	{
		public MyWindow() : base()
		{
			// Form
			this.Text = "My First Windows Application";
			this.Size = new Size(300, 300);
			this.StartPosition = FormStartPosition.CenterScreen;
			// Label
			Label lblGreeting = new Label();
			lblGreeting.Text = "Hello WinForm";
			lblGreeting.Location = new Point(100, 100);
			// Button
			Button btnExit = new Button();
			btnExit.Text = "Exit";
			btnExit.Location = new Point(180, 180);
			btnExit.Size = new Size(80, 30);
			btnExit.Click += new EventHandler(BtnExitOnClick);
			
			// Adding controls to Form
			this.Controls.AddRange(new Control[] {lblGreeting, btnExit});
		}
		public void BtnExitOnClick(object sender, EventArgs e)
		{
			Application.Exit();
		}
	}
}


In the constructor of MyWindow, first we have set certain properties of the Form. In this code, we have also used the StartPosition property of the Form, which sets the start up position of the form on the screen. The type of this property is an enumeration called ‘FormStartPosition’. We have set the start position of the form to the center of the screen.

The new inclusion in the code is the Exit button called ‘btnExit’. We have created the button using the base class System.Windows.Forms.Button. Later, we set various properties of the button like its text label (Text), its Location and its Size. Finally, we have created an event handler method for this button called BtnExitOnClick(). In the BtnExitOnClick() method, we have written the code to exit the application. We have also registered this event handler to the btnExit’s Click event (To understand the event handling in C#, see lesson 10 of the C# school). In the end, we have added a label and button to the form’s Controls collection. Note that this time we have used AddRange() method of form class to add an array of controls to the Controls collection of form. This method takes an array of type Control as its parameter.

The output of the code will be



Now you can press either the Exit Button or the close button at title bar to exit the application.

Index

 
Printer friendly version of the fzdnetfaq3-vs-helloworld-without-vscs page


Sponsored links

.Net Application Updating
One easy to use component adds safe and reliable updating features. Download today for a free trial.
Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Check Out IT Certification Preparation Materials
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
Villanova University Six Sigma & IT Certificate Programs
100% Online programs in Six Sigma, IS Security, CISSP Prep, Business Analysis, Proj. Mgmt. and more!
.Net Localization in three simple steps (WYSIWYG)
Localize .Net, C#/C/C++ & Delphi apps visually. HTML, HTML Help, XML & databases. Try Sisulizer now!

Advertisement



Free Magazine

Free Magazines
eWeek The essential technology information source for builders of e-business.... subscribe now

Newsletter | Submit Content | About | Advertising | Awards | Contact Us | Link to us |
© 1996-2008 Community Networks Ltd All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Synchron Data - .NET development.