1 Graphical User Interface_250213_200647
1 Graphical User Interface_250213_200647
Net
C# and .Net provide extensive support for building Windows Applications. The most important point being 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 (‘WinForm’ for short). A windows form
may contain text labels, push buttons, text boxes, list boxes, images, menus and vast range of other controls.
iii). Instantiate various controls, set their appropriate properties and add these to MyForm's Controls
collection.
iv). 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 the form as a windows application in the operating system so
that it may receive event messages from the Windows Operating System.
Example
Create an application that presents a simple window with a "Hello Windows Form" greeting at the center.
using System;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsApplication
{
class Test
{
static void Main()
{
Application.Run(new MyWindow());
}
Application Programming Notes ~ Fred Page 1 of 8
}
class MyWindow : Form
{
public MyWindow() : base()
{
this.Text = "Windows Application";
this.Size = new Size(300, 300);
using System;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsApplication
{
class Test
{
static void Main()
{
Application.Run(new MyWindow());
}
}
class MyWindow : Form
{
public MyWindow() : base()
{
// Form
this.Text = "Windows Application";
Application Programming Notes ~ Fred Page 2 of 8
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);
Explanations
The constructor of MyWindow is used set certain properties of the Form. In addition StartPosition property has
been set to specify the position of the form on the screen when the application starts. The type of this property is an
enumeration called 'FormStartPosition'.
An an event handler method for this button called BtnExitOnClick() has been created that contains code to exit the
application. The event handler has also been subscribed to the btnExit's Click event. Notice that both the
label and the button have been added to the form's Controls collection using the AddRange() method of form
class that adds an array of controls to the Controls collection of form. This method takes an array of type
Control as its parameter.
Example
A windows application that display a message using a message dialog
using System;
using System.Windows.Forms;
using System.Drawing;
namespace CSharpSchool
{
Application Programming Notes ~ Fred Page 3
of 8
class Test
{
static void Main()
{
Application.Run(new MyWindow());
}
}
class MyWindow : Form
{
public MyWindow() : base()
{
// Form
this.Text = "Windows Application";
this.Size = new Size(300, 300);
this.StartPosition = FormStartPosition.CenterScreen;
// Button1
Button btnExit = new Button();
btnExit.Text = "Exit";
btnExit.Location = new Point(180, 180);
btnExit.Size = new Size(80, 30);
btnExit.Click += new EventHandler(BtnExitOnClick);
// Button2
Button btnMessage = new Button();
btnMessage.Text = "Message";
btnMessage.Location = new Point(50, 180);
btnMessage.Size = new Size(80, 30);
btnMessage.Click += new
EventHandler(btnMessageOnClick); // Adding controls to
Form
this.Controls.AddRange(new Control[] {btnMessage,
btnExit});
}
public void BtnExitOnClick(object sender, EventArgs e)
{
Application.Exit();
}
public void btnMessageOnClick(object sender, EventArgs e)
{
MessageBox.Show("Hello");
}
}
}
The Toolbox, Properties Window, Help Window, Solution Explorer Window, Output Window and other
helping windows in Visual Studio IDE can be set for Docking and Auto hiding. Windows that are set for auto
hide appears only when they get focus (e.g. they have mouse pointer over them or receive a mouse click), and
hide when they lose focus. The hidden windows are always accessible through the left and right hand panes of the
form designer window. If some of these windows are not visible in your visual studio IDE, you can make them
visible from the View menu on the standard menu bar.
Properties
The first two TextBox controls are used for data entry - use these names: txtNumber1 and txtNumber2
The third TextBox control is used to display the output. Set the Name property as txtResults
Name the buttons btnAdd, btnSubtract, btnClear, and btnExit.
using System;
using System.Windows.Forms;
namespace ComputeFormsApplication
{
public partial class ComputeForm : Form
{
int number1, number2;
public ComputeForm()
{
InitializeComponent();
}
Controls
Controls are all instances of control classes and the collection of control classes are arranged in a class hierarchy i.e.
System.Windows.Forms.Control Class.
A summary of some of these controls is presented below.
Control Description
Label Used to display some text on the form. Instance of System.Windows.Forms.Label.
Important
Button Used to display a Push Button on the form. Instance of
System.Windows.Forms.Button. Important properties are Text, Name and Font.
Usually the Click event is handled for the Button.
TextBox Provides the user with an area to write/edit text. Instance of
System.Windows.Forms.TextBox. Important properties are Text (to set startup text and get
what the user has entered), Name, Alignment, Multiline (boolean), ScrollBars (a set of scroll
bars attached with the text box), ReadOnly (boolean), WordWrap (boolean) and
PasswordChar (character used for password masking). Important events are
TextChanged (default) and KeyPress.
CheckBox Allows a user to tick or untick a box to state their preference for something.
CheckBoxes are usually used in a group contained in a GroupBox. Any, all or none of the
checkboxes in a group can be selected. Instance of
System.Windows.Forms.CheckBox. Important properties are Text, Name, Checked
(boolean) and CheckState. Important events are CheckedChanged and
CheckStateChanged.
Application Programming Notes ~ Fred Page 8
of 8