CHAPTER 2: BASIC WINDOWS
FORMS PROGRAMMING
APPLIED PROGRAMMING IN ENGINEERING
2.2 WINDOWS FORMS IN C#
• 2.2.1 Overview of Windows Forms:
⚬ What is Windows Forms:
⚬ Windows Forms (WinForms) is a graphical user interface (GUI)
framework for building desktop applications in C#.
⚬ It is part of the .NET framework and provides a set of tools to create
interactive applications with buttons, text fields, images, and other UI
elements.
⚬ The [Link] namespace is a core part of the .NET
framework that provides classes, controls, and components for building
graphical user interface (GUI) applications in Windows. It includes support
for forms, buttons, textboxes, menus, dialogs, and event handling.
2.2 WINDOWS FORMS IN C#
• 2.2.1 Overview of Windows Forms:
⚬ Key Features of Windows Forms:
⚬ Event-driven programming: Uses event handlers to respond to user actions (e.g.,
button clicks).
⚬ Drag-and-drop UI design: Easily design forms using Visual Studio’s designer.
⚬ Rich set of controls: Includes built-in controls like buttons, text boxes, labels, and
data grids.
2.2 WINDOWS FORMS IN C#
• 2.2.1 Overview of Windows Forms:
⚬ Structure of a Windows Forms Application:
⚬ A Windows Forms project consists of several key files that define the application's
structure and behavior:
⚬ [Link]: (Application Entry Point) contains the Main() method, which is the
starting point of the application.
⚬ [Link]: the Code-Behind File for the Form; defines the logic and behavior of
the form.
⚬ [Link]: contains code for UI elements, generated by Visual Studio’s
form designer.
2.2 WINDOWS FORMS IN C#
• 2.2.1 Overview of Windows Forms:
⚬ Application class:
⚬ The Application class in Windows Forms is a part of the [Link]
namespace and is responsible for managing the application's lifecycle, message
loop, and common settings.
⚬ The Application class is essential for managing the startup, execution, and
shutdown of Windows Forms applications .
⚬ It ensures that the application runs smoothly and properly handles user
interactions.
2.2 WINDOWS FORMS IN C
# :#
• 2.2.1 Overview of Windows Forms:
⚬ Key methods of Application class:
⚬ [Link](Form): Starts the message loop and displays the specified form.
⚬ [Link](): Closes all forms and stops the application.
⚬ [Link](): Processes pending Windows messages.
⚬ [Link](): Stops the message loop of the current thread.
⚬ Application. EnableVisualStyles(): Enables modern UI styles.
2.2 WINDOWS FORMS IN C#
• 2.2.2 The Form1 class:
⚬ In a Windows Forms application, Form1 is the default form created when you start a
new project.
⚬ It is a C# class that inherits from [Link].
⚬ When you create a new Windows Forms project, Visual Studio generates a Form1
class with three main files:
⚬ [Link]: Contains the logic (code) for the form, such as event handlers.
⚬ [Link]: Auto-generated file that defines UI elements (buttons, labels, etc.).
⚬ [Link]: Stores resources like images, localization data.
2.2 WINDOWS FORMS IN C#
• 2.2.2 The Form1 class:
⚬ [Link]:
using [Link];
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
2.2 WINDOWS FORMS IN C#
• 2.2.2 The Form1 class:
⚬ [Link]:
namespace WindowsFormsApp1
{
partial class Form1
{
private [Link] components = null;
#region Windows Form Designer generated code
private void InitializeComponent()
{
[Link] = new [Link]();
[Link] = [Link];
[Link] = new [Link](800, 450);
[Link] = "Form1";
}
#endregion
}
}
2.2 WINDOWS FORMS IN C#
• 2.2.2 The Form1 class:
⚬ Key point of Form1 class:
⚬ Form1 inherits from Form class, making it a Windows Form.
⚬ The InitializeComponent() method is automatically generated and initializes UI
elements.
⚬ The partial keyword allows the class to be split into multiple files ([Link] and
[Link]).
⚬ [Link] contains automatically generated code for UI initialization; so
avoid editing this file manually because Visual Studio might overwrite it (If you
need to modify the UI, use the Windows Forms Designer instead of editing
[Link])
2.2 WINDOWS FORMS IN C#
⚬ [Link]: when adding control (button) to the Form
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.button1 = new [Link]();
// button1
[Link] = new [Link](185, 139);
[Link] = "button1";
[Link] = new [Link](106, 42);
[Link] = 0;
[Link] = "button1";
// Form1
[Link] = [Link];
[Link] = new [Link](537, 434);
[Link](this.button1); [Link] = "Form1";
[Link] = "Form1";
}
#endregion
private [Link] button1;
2.2 WINDOWS FORMS IN C#
• 2.2.3 Working with Forms:
⚬ Properties of Form:
⚬ A Form in Windows Forms ([Link]) has various properties
that control its appearance, behavior, and functionality.
⚬ Below are some of the most commonly used properties:
⚬ Text: The title of the form (displayed in the title bar).
⚬ Size/ClientSize: Specifies the entire size/usable size of the form.
⚬ Width / Height: The width and height of the form separately.
⚬ BackColor/ ForeColor: Sets the background color/default text color of the form.
⚬ StartPosition: Defines the initial position of the form (e.g., CenterScreen, Manual).
⚬ Opacity: Controls the transparency of the form (range: 0.0 to 1.0).
2.2 WINDOWS FORMS IN C#
• 2.2.3 Working with Forms:
⚬ Properties of Form:
⚬ In Windows Forms, properties can be set in two main ways:
⚬ Using the Properties Window (Design Time)
⚬ Using C# Code (Runtime).
2.2 WINDOWS FORMS IN C
# :#
• 2.2.3 Working with Forms:
⚬ Properties Windows:
⚬ The Properties Window: is a visual tool in Visual
Studio that allows you to modify the properties of a
form or control without writing code.
⚬ It has a two-column layout to display and edit
properties efficiently:
⚬ Left Column: Displays the property name (e.g.,
Text, Size, BackColor).
⚬ Right Column: Displays the property value, which
can be modified.
2.2 WINDOWS FORMS IN C#
• 2.2.3 Working with Forms:
⚬ Setting Properties in C# Code:
⚬ Each property can be set directly using the dot (.) operator.
[Link] = new [Link](915, 690);
[Link] = new [Link]("Arial", 9.75F, [Link]);
[Link] = [Link];
[Link] = "Form1";
[Link] = "PROGRAM 1";
2.2 WINDOWS FORMS IN C#
• 2.2.3 Working with Forms:
⚬ Setting Properties in C# Code:
⚬ The [Link] namespace in C# provides classes for working with graphics,
colors, fonts, images, and drawing operations.
⚬ It is commonly used in Windows Forms applications for customizing UI elements. Key
classes of [Link]:
⚬ Color: Represents colors used for UI elements (e.g., [Link], [Link]).
⚬ Font: Defines text font, size, and style (new Font("Arial", 12, [Link])).
⚬ Brushes: Predefined solid brushes for filling shapes ([Link]).
⚬ Pens: Defines the outline color and thickness of shapes (new Pen([Link], 2)).
⚬ Point: Represents a coordinate point (new Point(10, 20)).
⚬ Size: Defines width and height (new Size(100, 50)).
⚬ Rectangle: Represents a rectangular area (new Rectangle(10, 10, 200, 100)).
⚬ Graphics: Provides methods for drawing on a form or control ([Link](...)).
2.2 WINDOWS FORMS IN C#
• 2.2.4 Form Events & Event Handling:
⚬ Events in C#:
⚬ An event in C# is a mechanism that allows an object (such as a form or a control) to
notify other objects when something happens.
⚬ Events are used to handle user actions like clicks, key presses, and mouse
movements.
⚬ Events follow the Publisher-Subscriber model:
⚬ Publisher: The object that raises (triggers) the event (e.g., a button).
⚬ Subscriber: The method that handles (responds to) the event.
2.2 WINDOWS FORMS IN C#
• 2.2.4 Form Events & Event Handling:
⚬ Form events in C#:
⚬ An event in Windows Forms is a mechanism that allows a form or control to
respond to user actions (e.g., clicks, key presses, resizing). Events are triggered
when something happens to a form or control.
⚬ Common Events in Windows Forms:
⚬ Load: The form is loaded.
⚬ Click: The user clicks on the form.
⚬ MouseClick/MouseMove/…: the mouse interaction, such as clicks, movement, and
scrolling.
⚬ KeyPress/KeyDown/KeyUp: A key is pressed while the form is active.
⚬ Resize: The form is resized.
⚬ FormClosing:The form is about to close.
2.2 WINDOWS FORMS IN C#
• 2.2.4 Form Events & Event Handling:
⚬ Event Handling Mechanism in Windows Forms:
⚬ Step 1: User Interaction Triggers an Event:
⚬ When a user interacts with a form/control (e.g., clicking a button), an event is raised.
⚬ The system detects this event and looks for an event handler associated with it.
⚬ Step 2: Event Object is Created and Passed:
⚬ The event system creates an event object.
⚬ This object contains information about the event.
⚬ Step 3: Delegates Handle Event Calls:
⚬ Each event in C# is associated with a delegate. A delegate is a reference to the
method that should execute when the event occurs.
⚬ Step 4: Event Handler Method is Executed:
⚬ If an event handler (a method) is assigned to the event, it execute. The event handler
processes the event and performs an action (e.g., displaying a message).
2.2 WINDOWS FORMS IN C#
• 2.2.4 Form Events & Event Handling:
⚬ Step 1: User Interaction Triggers an Event:
⚬ The Properties Window: allows you to easily select
and assign event handlers for a form or control
without writing code manually.
⚬ To choose event for handling:
⚬ Select a Form/Control.
⚬ Go to the Events (⚡) tab in Properties Window.
⚬ Find the event you need
⚬ Double-click on the event name (e.g.,
MouseClick).
2.2 WINDOWS FORMS IN C#
• 2.2.4 Form Events & Event Handling:
⚬ Step 2: Event Object is Created and Passed:
⚬ Double-click on the event name (e.g.,
MouseClick).
⚬ An Event Handler Method is automatically created.
2.2 WINDOWS FORMS IN C#
• 2.2.4 Form Events & Event Handling:
⚬ Step 3: Delegates Handle Event Calls:
⚬ Visual Studio automatically subscribes the event handler to the selected
control’s event.
⚬ In [Link], Visual Studio adds:
[Link] += new [Link](this.Form1_Click);
⚬ [Link]:the event.
⚬ [Link]: predefined delegate in C# used for handling events.
⚬ this.Form1_Click: the event handling method.
2.2 WINDOWS FORMS IN C#
• 2.2.4 Form Events & Event Handling:
⚬ Step 4: Coding for Event Handler Method :
⚬ In Form1..cs, Visual Studio adds:
private void Form1_Click(object sender, EventArgs e)
{
⚬ object sender: parameter that represents the control or object that raised the
event.
⚬ EventArgs e : contains additional information about the event that occurred.
2.2 WINDOWS FORMS IN C#
• 2.2.4 Form Events & Event Handling:
⚬ Example:
⚬ Handling MouseMove Event and displaying mouse coordinate to Form Title:
⚬ Track the mouse cursor position in a Windows Forms application.
⚬ Display the X and Y coordinates dynamically to the Form Title as the mouse
moves.
⚬ Update the data in real-time as the mouse moves.
End of Chapter 2.2