0% found this document useful (0 votes)
21 views20 pages

.Net

The document provides an overview of Windows Forms, a UI framework for building Windows desktop applications, highlighting its features, controls, and event handling. It includes step-by-step instructions for creating a Windows Forms application using Visual Studio 2017, along with examples of common controls and their properties. Additionally, it explains the concept of menus in Windows applications and their characteristics.

Uploaded by

Ruchita Maaran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views20 pages

.Net

The document provides an overview of Windows Forms, a UI framework for building Windows desktop applications, highlighting its features, controls, and event handling. It includes step-by-step instructions for creating a Windows Forms application using Visual Studio 2017, along with examples of common controls and their properties. Additionally, it explains the concept of menus in Windows applications and their characteristics.

Uploaded by

Ruchita Maaran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

UNIT – 5

Windows Forms is a UI framework for building Windows desktop


apps. It provides one of the most productive ways to create
desktop apps based on the visual designer provided in Visual
Studio. Functionality such as drag-and-drop placement of visual
controls makes it easy to build desktop apps.
Windows Forms for .NET provides new features and enhancements
over .NET Framework.
• Windows Forms Overview:
o A UI technology for .NET providing managed libraries.
o Simplifies tasks like file system operations.
o Ideal for creating smart-client apps using Visual
Studio.
• Key Features of Windows Forms Apps:
o Display information and request user input.
o Communicate with remote computers over a network.
• Forms in Windows Forms:
o A form is the visual interface to display information.
o Apps are built by adding controls to forms.
• Controls:
o Discrete UI elements to display or accept data input.
o Examples include text boxes, buttons, drop-downs,
radio buttons, and webpages.
• Event Handling:
o User actions (e.g., clicks or key presses) generate
events.
o Events are processed through code.
• Custom Controls:
o UserControl class allows creating custom controls if
existing ones are insufficient.
The applications which are developed by using Windows Forms or
WinForms are known as the Windows Forms Applications that runs
on the desktop computer. WinForms can be used only to develop
the Windows Forms Applications not web applications. WinForms
applications can contain the different type of controls like
labels, list boxes, tooltip etc.

Steps to Create a Windows Forms Application Using Visual Studio


2017 (5 Marks)
1. Create a New Project:
o Open Visual Studio 2017.
o Go to File -> New -> Project.
o Select Visual C# from the left menu.
o Click on Windows Forms App (.NET Framework) in the
middle pane.
o Name your project and click OK.
2. Understand the Windows Interface:
o Editor Window/Main Window: Used for working with
forms and editing code.
o Solution Explorer Window: Navigate and manage project
files.
o Properties Window: Modify properties of selected items
or controls.
3. Add Controls to the Form:
o Open the Toolbox from the left side of Visual Studio.
o Go to Common Controls for frequently used controls
like TextBox, Button, etc.
o Drag and drop controls onto the form.
o Modify control properties (e.g., TextAlign, MaxLength)
from the Properties Window.
4. Edit the Code:
o Double-click a control to open its event handler in
the code editor.
o Write or customize code as required.
o View auto-generated code in the Form1.Designer.cs file
in Solution Explorer.
5. Run and Debug the Application:
o Press F5 or click the Play button in the toolbar to
run the program.
o Use the Pause button or stop debugging options to end
execution.
o Alternatively, go to Debug -> Start Debugging in the
menu bar.
(OR)
Step-Wise Guide to Create a Windows Forms Application
1. Create a Project
1. Open Visual Studio.
2. On the start window, select Create a new project.
3. In the Create a new project window:
o Search for Windows Forms App (.NET Framework).
o Refine search by selecting C# as the language and
Windows as the platform.
o Select the Windows Forms App (.NET Framework)
template for C#.
4. In the Configure your new project window:
o Enter the project name, e.g., HelloWorld.
o Click Create.
5. Visual Studio opens your new project.

2. Create the Application


Add a Button to the Form
1. Open the Toolbox (from the left panel or View > Toolbox or
Ctrl+Alt+X).
2. Expand Common Controls and pin the Toolbox using the Pin
icon.
3. Drag a Button control onto the form.
4. In the Properties window:
o Change the Text property from "button1" to Click this.
o Change the Name property from "button1" to
btnClickThis.
Add a Label to the Form
1. Drag a Label control from the Toolbox onto the form,
beneath the button.
2. In the Properties window:
o Change the Name property from "label1" to
lblHelloWorld.
3. Add Code to the Form
1. Open the code editor by double-clicking the button on the
form.
2. In the Form1.cs file, add the following line inside the
button's click event handler:
3. lblHelloWorld.Text = "Hello World!";

4. Run the Application


1. Click the Start button (or press F5) to run the
application.
o (Screenshot: Shows the Start button.)
2. In the running application:
o Click the Click this button.
o Observe the label text changes to Hello World!.

3. Close the application to stop debugging.

Properties of Windows Forms in C#


Windows Forms have a variety of properties that can be
configured to control the behavior and appearance of forms and
their components. Common properties include:
1. Text: Sets the title of the form displayed on the title
bar.
2. Name: Identifies the form programmatically.
3. BackColor: Specifies the background color of the form.
4. ForeColor: Specifies the color of the text in controls on
the form.
5. Size: Defines the height and width of the form.
6. StartPosition: Determines the initial position of the form
when it is displayed.
7. WindowState: Specifies the state of the window (Normal,
Minimized, or Maximized).
8. FormBorderStyle: Determines the border style of the form
(e.g., Fixed, Sizable, None).
9. Icon: Sets the icon displayed on the title bar.
10. MaximizeBox: Enables or disables the maximize button
on the form.
11. MinimizeBox: Enables or disables the minimize button
on the form.
12. Enabled: Enables or disables the form and its child
controls.
13. Visible: Determines whether the form is visible or
hidden.
14. Opacity: Sets the transparency level of the form
(range: 0.0 to 1.0).
15. ControlBox: Determines whether the form displays a
control box in the title bar.
Events of Windows Forms in C#
Events allow the form to respond to user actions or system
changes. Common events include:
1. Load: Triggered when the form is loaded into memory.
o Example: Setting initial values or configurations.
2. Shown: Triggered when the form is first displayed.
3. Click: Triggered when the form is clicked.
4. DoubleClick: Triggered when the form is double-clicked.
5. KeyDown: Triggered when a key is pressed while the form is
focused.
6. KeyUp: Triggered when a key is released while the form is
focused.
7. MouseClick: Triggered when the mouse is clicked on the
form.
8. MouseDoubleClick: Triggered when the mouse is double-
clicked on the form.
9. MouseEnter: Triggered when the mouse pointer enters the
form.
10. MouseLeave: Triggered when the mouse pointer leaves
the form.
11. FormClosing: Triggered when the form is about to
close.
o Example: Prompt the user to save changes.
12. FormClosed: Triggered after the form is closed.
13. Resize: Triggered when the form is resized.
14. Paint: Triggered when the form needs to be redrawn.
15. Activated: Triggered when the form gains focus.
16. Deactivated: Triggered when the form loses focus.
17. TextChanged: Triggered when the Text property of the
form changes.
These properties and events are essential for customizing and
managing a Windows Form application in C#.
using System;
using System.Drawing;
using System.Windows.Forms;
public class MainForm : Form
{
public MainForm()
{
// Set properties of the form
this.Text = "My First Windows Form"; // Title of the
form
this.BackColor = Color.LightBlue; // Background color
this.FormBorderStyle = FormBorderStyle.FixedDialog; //
Fixed border style
this.StartPosition = FormStartPosition.CenterScreen; //
Start in the center of the screen
this.Size = new Size(400, 300); // Width and height
of the form
}
public static void Main()
{
Application.Run(new MainForm());
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
public class MainForm : Form
{
private Button btnClickMe;
public MainForm()
{
// Initialize form properties
this.Text = "Event Demo Form";
this.Size = new Size(400, 300);
// Initialize a button
btnClickMe = new Button();
btnClickMe.Text = "Click Me";
btnClickMe.Size = new Size(100, 50);
btnClickMe.Location = new Point(150, 100);
// Subscribe to events
this.Load += MainForm_Load; // Form load event
this.FormClosing += MainForm_FormClosing; // Form
closing event
btnClickMe.Click += BtnClickMe_Click; // Button click
event
// Add button to the form
this.Controls.Add(btnClickMe);
}
// Event handler for form load
private void MainForm_Load(object sender, EventArgs e)
{
MessageBox.Show("Welcome to the Event Demo Form!",
"Info");
}
// Event handler for button click
private void BtnClickMe_Click(object sender, EventArgs e)
{
MessageBox.Show("Button clicked!", "Info");
}
// Event handler for form closing
private void MainForm_FormClosing(object sender,
FormClosingEventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure you
want to exit?",
"Exit Confirmation", MessageBoxButtons.YesNo);
if (result == DialogResult.No)
{
e.Cancel = true; // Cancel the closing event
}
}
public static void Main()
{
Application.Run(new MainForm());
}
}

Common Controls in Windows Forms


1. Button
• Description: A clickable control to trigger an action.
• Key Properties:
o Text: Displays the label on the button.
• Key Events:
o Click: Triggered when the button is clicked.
• Example:
Button button1 = new Button();
button1.Text = "Click Me";
button1.Location = new Point(50, 50);
button1.Click += (sender, e) => MessageBox.Show("Button
Clicked!");
this.Controls.Add(button1);

2. Label
• Description: Displays text, typically as a caption for
other controls.
• Key Properties:
o Text: The text displayed in the label.
o Font: Defines the font style and size.
• Key Events:
o Click: Triggered when the label is clicked.
• Example:
Label label1 = new Label();
label1.Text = "Enter your name:";
label1.Location = new Point(20, 20);
this.Controls.Add(label1);

3. TextBox
• Description: Allows text input or displays text.
• Key Properties:
o Text: Gets or sets the content.
o Multiline: Enables multi-line input.
o ScrollBars: Adds scrollbars (Both, Horizontal,
Vertical).
o ReadOnly: Prevents editing.
• Key Events:
o TextChanged: Triggered when text changes.
• Example:
TextBox textBox1 = new TextBox();
textBox1.Multiline = true;
textBox1.ScrollBars = ScrollBars.Vertical;
textBox1.Location = new Point(20, 50);
textBox1.Size = new Size(200, 100);
this.Controls.Add(textBox1);
4. GroupBox
• Description: Groups related controls with a border and
caption.
• Key Properties:
o Text: Sets the group box title.
• Example:
GroupBox groupBox1 = new GroupBox();
groupBox1.Text = "User Details";
groupBox1.Location = new Point(10, 10);
groupBox1.Size = new Size(300, 200);
RadioButton radioButton1 = new RadioButton();
radioButton1.Text = "Male";
radioButton1.Location = new Point(20, 30);
RadioButton radioButton2 = new RadioButton();
radioButton2.Text = "Female";
radioButton2.Location = new Point(20, 60);
groupBox1.Controls.Add(radioButton1);
groupBox1.Controls.Add(radioButton2);
this.Controls.Add(groupBox1);

5. Panel
• Description: A container for other controls with optional
scrollbars.
• Key Properties:
o AutoScroll: Enables scrolling.
• Example:
Panel panel1 = new Panel();
panel1.Size = new Size(300, 200);
panel1.Location = new Point(10, 10);
panel1.AutoScroll = true;
Label label1 = new Label();
label1.Text = "This is a panel.";
label1.Location = new Point(10, 10);
panel1.Controls.Add(label1);
this.Controls.Add(panel1);

6. RadioButton
• Description: Allows users to select one option from a
group.
• Key Properties:
o Checked: Indicates if the radio button is selected.
• Key Events:
o CheckedChanged: Triggered when the state changes.
• Example:
RadioButton radioButton1 = new RadioButton();
radioButton1.Text = "Option 1";
radioButton1.Location = new Point(20, 20);
radioButton1.CheckedChanged += (sender, e) =>{
if (radioButton1.Checked)
MessageBox.Show("Option 1 Selected!");
};
this.Controls.Add(radioButton1);

7. ListBox
• Description: Displays a list of items for selection.
• Key Properties:
o Items: The collection of items in the ListBox.
o SelectionMode: Specifies single or multiple selection.
• Key Events:
o SelectedIndexChanged: Triggered when the selection
changes.
• Example:
ListBox listBox1 = new ListBox();
listBox1.Location = new Point(20, 20);
listBox1.Size = new Size(200, 100);

listBox1.Items.Add("Item 1");
listBox1.Items.Add("Item 2");
listBox1.Items.Add("Item 3");
listBox1.SelectedIndexChanged += (sender, e) => {
string selectedItem = listBox1.SelectedItem?.ToString();
MessageBox.Show("You selected: " + selectedItem);
};
this.Controls.Add(listBox1);

8. MessageBox
• Description: Displays a modal dialog with a message and
buttons.
• Key Methods:
o MessageBox.Show: Displays the MessageBox.
• Example:
• MessageBox.Show("Hello, World!", "Greeting",
MessageBoxButtons.OK, MessageBoxIcon.Information);

What is a Menu?
In a Windows-based application, a menu is a graphical control
element that provides users with a list of commands or options
to perform actions or navigate the application. Menus are an
essential part of the user interface in desktop applications,
allowing users to interact with the software in an organized and
intuitive way.
Menus are usually located at the top of a window and are
structured hierarchically with menu headers (e.g., File, Edit,
View) and sub-menu items (e.g., New, Open, Save).
Characteristics of a Menu:
• Organized Commands: Menus group related functionalities
under headers.
• Interactive: Menus are activated through mouse clicks or
keyboard shortcuts.
• Expandable: Menus can have nested submenus, offering a
deeper structure.
Example of a Basic Menu Layout:
File Edit View Help
-----------------------------------
New Undo Zoom In About
Open Redo Zoom Out
Save
Exit
Types of Menus in Windows Forms:
1. Main Menu: The primary menu bar at the top of the form
(e.g., File, Edit).
2. Submenus: Nested items under a main menu header.
3. Context Menu (Popup Menu): A menu that appears upon right-
clicking a specific control or area in the application.
4. Tool Strip Menu (MenuStrip): An enhanced menu introduced in
.NET 4.0 that provides more functionality and modern
design.
Menus in Programming:
Menus in a Windows Forms application are typically created using
the MainMenu class in .NET (prior to .NET 4.0) or the MenuStrip
control (in .NET 4.0 and later). These classes allow developers
to define menus, add items, and handle user interactions.

What is a Dialog Box in C#?


A dialog box is a small window used to communicate or interact
with the user in a C# application. It prompts the user for
input, provides warnings, or displays information.
Types of Dialog Boxes:
1. Modal Dialog Box: Pauses the application until closed
(e.g., Save Confirmation).
2. Modeless Dialog Box: Allows the application to continue
running (e.g., Find-Replace).
Common Dialog Boxes:
• MessageBox: Displays messages or warnings.
csharp
Copy code
DialogResult res = MessageBox.Show("Are you sure?",
"Confirmation", MessageBoxButtons.OKCancel);
if (res == DialogResult.OK) { /* Perform task */ }
• OpenFileDialog: Opens a file.
csharp
Copy code
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Text Files (*.txt)|*.txt";
if (ofd.ShowDialog() == DialogResult.OK) { /* Open file */ }
• SaveFileDialog: Saves a file to a selected location.
csharp
Copy code
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == DialogResult.OK) { /* Save file */ }
• FontDialog/ColorDialog: Configures fonts or colors.
Custom Dialog Boxes:
Create by adding a form, customizing its properties, and using
ShowDialog() for modal or Show() for modeless display.
Steps to Use a Common Dialog Box:
1. Instantiate the dialog.
2. Set properties as needed.
3. Call ShowDialog() to display.
These dialog boxes enhance user experience by standardizing
interactions like file operations, warnings, and input prompts.

ToolTip in C#
In Windows Forms, a ToolTip is a small pop-up that appears when
the cursor hovers over a control, providing a brief description.
The ToolTip class, found in System.Windows.Forms, allows the
creation and customization of these tooltips.
Two Methods to Create ToolTips:
1. Design-Time:
o Drag and drop the ToolTip control from the toolbox to
the form.
o Modify its properties in the properties window.
2. Run-Time:
o Create a ToolTip control programmatically.
o Example:
csharp
Copy code
ToolTip t_Tip = new ToolTip();
t_Tip.SetToolTip(box1, "Name should start with a capital
letter");
t_Tip.SetToolTip(box2, "Password should be at least 8
characters");
Key Properties:
• Active: Activates or deactivates the ToolTip.
• AutoPopDelay: Time the ToolTip stays visible.
• InitialDelay: Time before the ToolTip appears.
• IsBalloon: Displays the tooltip as a balloon.
• ToolTipIcon: Adds an icon to the ToolTip.
Example:
ToolTip t_Tip = new ToolTip();
t_Tip.Active = true;
t_Tip.AutoPopDelay = 4000;
t_Tip.InitialDelay = 600;
t_Tip.IsBalloon = true;
t_Tip.ToolTipIcon = ToolTipIcon.Info;
t_Tip.SetToolTip(box1, "Name should start with Capital letter");
t_Tip.SetToolTip(box2, "Password should be greater than 8
words");
Conclusion:
ToolTip provides a simple way to add brief, helpful descriptions
to your form controls, improving user experience by giving them
hints about input requirements or actions.

You might also like