Você está na página 1de 23

Session 9

WinForms & Windows Applications

Windows Applications and .Net


C# and .Net provide extensive support for
building Windows Applications
most important point about windows
applications is that they are 'event driven'
All windows applications present a graphical
interface to their users and respond to user
interaction.
This graphical user interface is called a
'Windows Form', or 'WinForm' for short.

. A windows form may contain text labels,


push buttons, text boxes, list boxes, images,
menus and vast range of other controls
WinForm is also a windows control just like a
text box, label, etc. In .Net, all windows
controls are represented by base class objects
contained in the System.Windows.Forms
namespace.

WinForm Basics
. The class System.Windows.Forms.Form is
the base class of all WinForms in .Net
Write a new class to represent the WinForm
and derive it from the
System.Windows.Forms.Form class:
class MyForm : System.Windows.Form
{
...
}

Instantiate various controls, set their


appropriate properties and add these to
MyForm's Controls collection.
Write another class containing the Main()
method. In the Main() method, call the
System.Application.Run() method, supplying
it with an instance of MyForm.

class Test
{
static void Main()
{
}
Application.Run(new MyForm());

The Application.Run() method registers your form as a


windows application in the operating system so that it may
receive event messages from the Windows Operating
System.

Building the "Hello WinForm"


Application
Let's build our first windows application,
which we will call "Hello WinForm". The
application will present a simple window with
a "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);
}
}
}

When we execute the above code, the following screen is displayed:

Adding Event Handling


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 lblGreeting = new Label();


lblGreeting.Text = "Hello WinForm";
lblGreeting.Location = new Point(100, 100);
Button btnExit = new Button();
btnExit.Text = "Exit";
btnExit.Location = new Point(180, 180);
btnExit.Size = new Size(80, 30);
btnExit.Click += new EventHandler(BtnExitOnClick);
this.Controls.AddRange(new Control[] {lblGreeting, btnExit});
}
public void BtnExitOnClick(object sender, EventArgs e)
{
Application.Exit();
}
}

following window will be displayed:

Visual Studio.Net & its IDE (Integrated


Development Environment

Visual Sutdio.Net provides a standard code editor and IDE for


all .Net applications, along with a standard debugger, project
and solution settings, form designer, integrated compiler and
lot of other useful tools.
The Visual Studio.Net IDE provides a standard text editor to
write .Net applications. The text editor is loaded with
IntelliSense and a hot compiler.
IntelliSense gives the text editor the ability to suggest different
options in the programming context
when you place a dot after the name of an object, the IDE
automatically provides you a list of all the members
(properties, methods, etc) of the object

The hot compiler highlights the syntax errors


in your program as you type the code. The
following figure shows an illustration of the
hot compiler at work in Visual Studio.Net.

Code Folding
One of the pleasant new features introduced in Visual
Studio.Net is code folding. With code folding, you
can fold/unfold the code using the + and - symbols
Usually the code can be folded/unfolded at each
scope boundary (method, class, namespace, property,
etc). You can also define regions within your code
and can fold/unfold the code within the region. The
region is defined using the #region...#endregion
preprocessor directives.

Integrated Compiler, Solution


builder and Debugger
Visual Studio.Net provides an integrated compiler to
compile and execute your application during
development.
You can either compile a single source file or the
complete project and solution (a group of files that
make up an application).
Once you have compiled your application, you can
debug it using the Visual Studio.Net debugger. You
can even create an installer for your application using
Visual Studio.Net!

Form Designer
The form designer allows you to design the
graphical user interface just by placing the
controls on the form from the Toolbox
You can set a lot of properties of the form and
its controls using the Properties window
The Visual Studio.Net IDE automatically
writes the code in the source file as you place
the controls on the form and change their
properties.

Toolbox window at the left hand side (#1


properties window at the right hand side (#2)
Event properties can be changed by switching to the Event Poperties pane
(#3) in the Properties Window
A window can be set for auto hide by the button marked #4 in the above
figure.
The right hand pane is marked with #5 in the above figure and has got the class view,

Você também pode gostar