Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

C#.net-unit-4

The document provides an overview of C# Windows Forms, a GUI class library for developing desktop applications using the .NET Framework. It covers the creation of Windows Forms applications in Visual Studio, including the use of various controls, namespaces, and the functionality of the Form class. Additionally, it details the programming aspects of Windows Forms controls and their properties, methods, and events.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

C#.net-unit-4

The document provides an overview of C# Windows Forms, a GUI class library for developing desktop applications using the .NET Framework. It covers the creation of Windows Forms applications in Visual Studio, including the use of various controls, namespaces, and the functionality of the Form class. Additionally, it details the programming aspects of Windows Forms controls and their properties, methods, and events.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Unit-4 10hrs

Introducing windows forms: Overview of the system. windows. Forms Namespaces, An


Anatomy of a Form, A Simple Form Program, Function with Control Class, The
Functionality of the Form Class, Component class, control class.
Programming with windows forms controls: Working with Button types, Check Boxes,
Radio Buttons, Group Boxes, List Boxes, Calendar control, Timer, picture box, group box,
scroll bar, Progress bar, assigning tool tips for controls. Developing an UI.

Introduction to C# Windows Forms Applications


Windows Forms is a Graphical User Interface(GUI) class library which is bundled in .Net
Framework. Its main purpose is to provide an easier interface to develop the applications for
desktop, tablet, PCs. It is also termed as the WinForms. 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.
Creating a Windows Forms Application Using Visual Studio 2017
• First, open the Visual Studio then Go to File -> New -> Project to create a new
project and then select the language as Visual C# from the left menu. Click on
Windows Forms App(.NET Framework) in the middle of current window. After
that give the project name and Click OK.

• Here the solution is like a container which contains the projects and files that may
be required by the program.
• After that following window will display which will be divided into three parts as
follows:
1. Editor Window or Main Window: Here, you will work with forms
and code editing. You can notice the layout of form which is now
blank. You will double click the form then it will open the code for
that.

1
2. Solution Explorer Window: It is used to navigate between all items in
solution. For example, if you will select a file form this window then
particular information will be display in the property window.
3. Properties Window: This window is used to change the different
properties of the selected item in the Solution Explorer. Also, you can
change the properties of components or controls that you will add to
the forms.
• Now to add the controls to your WinForms application go to Toolbox tab
present in the extreme left side of Visual Studio. Here, you can see a list of
controls. To access the most commonly used controls go to Common Controls
present in Toolbox tab.

• Now drag and drop the controls that you needed on created Form. For example, if
you can add TextBox, ListBox, Button etc. as shown below. By clicking on the
particular dropped control you can see and change its properties present in the
right most corner of Visual Studio.

2
In the above image, you can see the TextBox is selected and its properties like
TextAlign, MaxLength etc. are opened in right most corner. You can change its
properties’ values as per the application need. The code of controls will be
automatically added in the background. You can check the Form1.Designer.cs file
present in the Solution Explorer Window.
• To run the program you can use an F5 key or Play button present in the toolbar of
Visual Studio. To stop the program you can use pause button present in the
ToolBar. You can also run the program by going to Debug->Start Debugging
menu in the menubar.

C# Windows Forms is a graphical user interface (GUI) framework that enables developers to
create desktop applications for the Windows operating system. Windows Forms applications
are created using the C# programming language and the .NET framework. They are built by

3
dragging and dropping controls such as buttons, text boxes, labels, and other user interface
elements onto a form.
1. The Windows Forms framework provides a rich set of controls that developers can
use to build applications with. These controls are designed to provide a consistent
and familiar user interface for Windows users. Developers can customize the
appearance and behavior of these controls by setting various properties and
handling events.
2. To create a Windows Forms application in C#, you can use Microsoft Visual
Studio, which is an integrated development environment (IDE) that provides a
visual designer to create and layout the user interface elements. The visual
designer is a drag-and-drop interface for building your UI, and you can easily
configure each control’s properties through a user-friendly interface.
3. In addition to the visual designer, Visual Studio also provides a code editor that
enables developers to write the C# code for the application’s logic. Developers
can handle events and perform tasks such as data validation, data manipulation,
and business logic implementation.
4. Windows Forms applications are versatile and can be used to create various types
of applications such as data entry, management, and reporting applications, as well
as games and multimedia applications.

4
Overview of the system. windows. Forms Namespaces

The System.Windows.Forms namespace in C# is associated with the development of


Windows Forms applications. Windows Forms (WinForms) is a traditional UI framework for
creating desktop applications on the Microsoft .NET platform. Below are some key
namespaces within the System.Windows.Forms namespace hierarchy:
1. System.Windows.Forms:
The primary namespace for Windows Forms. It contains essential classes and controls for
building desktop applications. Examples include Form (for creating windows), Button,
TextBox, Label, and many other UI controls.
2. System.Windows.Forms.Design:
This namespace includes classes for design-time support in Visual Studio when working with
Windows Forms. It provides features to help with designing and customizing forms and
controls in the Visual Studio designer.
3. System.Windows.Forms.DataVisualization.Charting:
This namespace is specifically for chart controls, providing classes for creating various types
of charts, such as bar charts, line charts, and pie charts.
4. System.Windows.Forms.Integration:
It contains classes related to integrating Windows Forms with other UI technologies, such as
WPF. The ElementHost class, for example, allows you to host WPF controls within a
Windows Forms application.
5. System.Windows.Forms.Layout:
This namespace contains classes related to layout management in Windows Forms. It
includes the LayoutEngine class and others for controlling the layout of controls on a form.
6. System.Windows.Forms.VisualStyles:
It includes classes for working with visual styles, allowing you to apply styles to your
Windows Forms controls to achieve a consistent appearance with the operating system.
7. System.Windows.Forms.ComponentModel.Com2Interop:
This namespace contains types for COM interoperation in the context of Windows Forms,
particularly for component models.
8. System.Windows.Forms.Design.Behavior:
It contains classes related to design-time behavior for controls in Windows Forms
applications.
System.Windows.Forms.Design.Layout 9. namespace, this one includes additional
: classes related to design-time layout
Similar to System.Windows.Forms.Layout behavior in Visual Studio.
the
10. System.Windows.Forms.Design.Serialization:

These namespaces collectively provide the tools and classes necessary for creating,
designing, and interacting with the UI in Windows Forms applications. When you're building
a desktop application using Windows Forms in C#, you'll frequently use types from these
namespaces to design the user interface, handle events, and manage the overall application
flow.

5
These are just a few examples, and there are many more control classes available in both
Windows Forms and WPF. Depending on your application's requirements, you choose the
appropriate controls to build your user interface.
When working with controls, you typically instantiate them, set properties to configure their
appearance and behaviour, and add them to a container (such as a form or panel). Event
handlers are often used to respond to user interactions with these controls.

An Anatomy of a Form, Example:


public partial class MainForm : Form
{
// Class code goes here }
2. Constructor:
The form class has a constructor where you can initialize components, set properties, and
perform other setup tasks. The InitializeComponent method is often called in the constructor
and is responsible for setting up the visual components designed in the form designer.
example
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}}
3. InitializeComponent Method:
The InitializeComponent method is automatically generated by the form designer and
contains code to set up controls, layout, and other visual elements defined in the form 4.
Controls:
Controls are the building blocks of a form, representing UI elements like buttons, textboxes,
labels, and more. You can add controls programmatically or use the form designer to drag and
drop them onto the form.
example
public partial class MainForm : Form
{ private Button
myButton;
public MainForm()
{
InitializeComponent();
// Create and configure a button
myButton = new Button();
myButton.Text = "Click Me";
myButton.Click += MyButton_Click; //
Add the button to the form's controls
this.Controls.Add(myButton);
}}
5. Event Handlers:
Event handlers are methods that respond to user actions or other events. For example, a
button click event handler might show a message box. Example
private void MyButton_Click(object sender, EventArgs e) {

6
MessageBox.Show("Button Clicked!"); }
6. Properties and Methods:
The form class exposes various properties and methods that allow you to customize its
behavior and appearance. Common properties include Text (for setting the form's title) and
BackColor (for setting the background color).
Example
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent(); // Set
form properties this.Text = "My
Windows Form";
this.BackColor = Color.LightGray;
}}
7. Layout:
You can set the layout of controls on the form using properties like Dock, Anchor, and by
using layout panels (e.g., FlowLayoutPanel, TableLayoutPanel).
8. Menu and Toolbar:
Forms can have menus and toolbars for navigation and interaction. You can add menus and
toolbars through the form designer or programmatically.
9. Closing Event:
You can handle the FormClosing event to perform actions before the form is closed. Example
private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
// Perform cleanup or additional actions before closing }
10. Run the Application:
Typically, a Windows Forms application starts by creating an instance of the main form and
running it using the Application.Run method. Example
static class Program
{

static void Main()


{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}

A Simple Form Program using


System;
using System.Windows.Forms;

namespace SimpleFormExample
{
public partial class MainForm : Form

7
{
private Button myButton;
private TextBox myTextBox;

public MainForm()
{
InitializeComponent();
InitializeUI();
}

private void InitializeUI()


{
// Button
myButton = new Button();
myButton.Text = "Click Me";
myButton.Click += MyButton_Click;

// TextBox
myTextBox = new TextBox();

// Set layout
FlowLayoutPanel flowLayoutPanel = new FlowLayoutPanel();
flowLayoutPanel.Controls.Add(myButton);
flowLayoutPanel.Controls.Add(myTextBox);

// Set the form layout


this.Controls.Add(flowLayoutPanel);

// Set form properties


this.Text = "Simple Form Example";
}

private void MyButton_Click(object sender, EventArgs e)


{
string message = "Hello, " + myTextBox.Text + "!";
MessageBox.Show(message, "Greeting");
}
}

static class Program


{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

8
Application.Run(new MainForm());
}
}
}
In this example, we create a simple Windows Form with a button and a textbox. When the
button is clicked, a message box appears with a greeting that includes the text entered in the
textbox.
The Functionality of the Form Class

In C# with Windows Forms, the Form class is a fundamental class that represents a window
or dialog box in a desktop application. The Form class provides a wide range of properties,
methods, and events that enable you to customize and control the behavior and appearance of
your application's user interface. Here are some key functionalities provided by the Form
class:

Properties:
Text:
Allows you to set or get the text displayed in the title bar of the form.
Example
this.Text = "My Form";

BackColor and ForeColor:


Sets or gets the background and foreground colors of the form.
Example this.BackColor =
Color.LightGray;
this.ForeColor = Color.Black;

Size and Location:


Defines the size and location of the form.
Example
this.Size = new Size(400, 300);
this.Location = new Point(100, 100);

WindowState:
Gets or sets a value indicating whether the form is minimized, maximized, or normal.
this.WindowState = FormWindowState.Maximized;

TopMost:
Gets or sets a value indicating whether the form should be displayed as a topmost form.
this.TopMost = true;

Methods:
Show() and ShowDialog():

Displays the form on the screen. ShowDialog is used to display the form as a modal dialog.
this.Show();

9
// or
this.ShowDialog();

Close(): Closes
the form.
this.Close();

Events:
1.Load:

Occurs when the form is first loaded. private void


MainForm_Load(object sender, EventArgs e) {
// Form loaded, perform initialization tasks
}
2.Closing:

Occurs before the form is closed. Can be used to prompt the user for confirmation
private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
// Check if the user wants to save changes before closing, for example }
3.Activated and Deactivate:

Occur when the form is activated (comes to the foreground) or deactivated (loses focus).
private void MainForm_Activated(object sender, EventArgs e) {
// Form is activated
}

private void MainForm_Deactivate(object sender, EventArgs e) {


// Form is deactivated
}
4.Resize:

Occurs when the form is resized


private void Main Form_Resize(object sender, EventArgs e) {
// Form is resized }
These are just a few examples of the properties, methods, and events provided by the Form
class. The Form class is the foundation for creating the user interface of your desktop
applications and plays a crucial role in managing the user experience.
In C#, the term "control class" typically refers to classes that represent user interface elements
(controls) in graphical applications. Controls are objects that users interact with to perform
actions or provide input. In the context of Windows Forms or WPF applications, these classes
are often part of the System.Windows.Forms or System.Windows.Controls namespaces.

CONTROL CLASS
Here's a brief overview of control classes in both Windows Forms and WPF:

Windows Forms (System.Windows.Forms):

10
1. Button:
Represents a standard button.
Example
Button myButton = new Button(); 2.TextBox:
Represents a single-line text box for user input.
Ex:
TextBox myTextBox = new TextBox();

3.Label:
Displays text and allows for simple formatting.
Ex;
Label myLabel = new Label();

4.ComboBox:
Represents a drop-down list.
Ex:
ComboBox myComboBox = new ComboBox();

5.DataGridView:
Displays data in a tabular format.
Ex:
DataGridView myDataGridView = new DataGridView();

6.PictureBox:
Displays an image.
Ex:
PictureBox myPictureBox = new PictureBox();

These are just a few examples, and there are many more control classes available in both
Windows Forms and WPF. Depending on your application's requirements, you choose the
appropriate controls to build your user interface.
When working with controls, you typically instantiate them, set properties to configure their
appearance and behavior, and add them to a container (such as a form or panel). Event
handlers are often used to respond to user interactions with these controls.

11
Programming with windows forms controls: Working with Button types, Check Boxes,
Radio Buttons, Group Boxes, List Boxes, Calendar control, Timer, picture box, group box,
scroll bar, Progress bar, assigning tool tips for controls. Developing an UI.
A Button is an essential part of an application, or software, or webpage. It allows the user to
interact with the application or software. For example, if a user wants to exit from the current
application so, he/she click the exit button which closes the application. It can be used to
perform many actions like to submit, upload, download, etc. according to the requirement of
your program. It can be available with different shape, size, color, etc. and you can reuse
them in different applications. In .NET Framework, Button class is used to represent
windows button control and it is inherited from ButtonBase class. It is defined under
System.Windows.Forms namespace.
Property Description

Using BackColor property you can set the background color of the
button.
BackColor

Using BackgroundImage property you can set the background image


on the button.
BackgroundImage

Using AutoEllipsis property you can set a value which shows that
whether the ellipsis character (…) appears at the right edge of the
control which denotes that the button text extends beyond the
specified length of the button.
AutoEllipsis

Using AutoSize property you can set a value which shows whether
the button resizes based on its contents.
AutoSize

Using Enabled property you can set a value which shows whether
the button can respond to user interaction.
Enabled

12
Using Events property you can get the list of the event handlers that
are applied on the given button.
Events

Font Using Font property you can set the font of the button.

FontHeight Using FontHeight property you can set the height of the font.

Using ForeColor property you can set the foreground color of the
button.
ForeColor

Height Using Height property you can set the height of the button.

Image Using Image property you can set the image on the button.

Margin Using Margin property you can set the margin between controls.

Name Using Name property you can set the name of the button.

Padding Using Padding property you can set the padding within the button.

Using Visible property you can set a value which shows whether the
button and all its child buttons are displayed.
Visible

Important Events on Button

13
Event Description

Click This event occur when the button is clicked.

This event occur when the user performs double click on the
DoubleClick button.

Enter This event occur when the control is entered.

This event occur when the character, or space, or backspace key is


KeyPress pressed while the control has focus.

Leave This event occur when the input focus leaves the control.

MouseClick This event occur when you click the mouse pointer on the button.

This event occur when you double click the mouse pointer on the
MouseDoubleClick button.

MouseHover This event occur when the mouse pointer placed on the button.

MouseLeave This event occur when the mouse pointer leaves the button.

Important Events on CheckBox


Event Description

Event Description

CheckedChanged This event occur when the value of the Checked property changes.

CheckStateChanged This event occur when the value of the CheckState property
changes.

Click This event occur when the control is clicked.

14
DoubleClick This event occur when the user double-clicks the CheckBox control.

Leave This event occur when the input focus leaves the control.

MouseClick This event occur when the control is clicked by the mouse.

MouseDoubleClick This event occur when the user double-clicks the CheckBox control.

MouseHover This event occur when the mouse pointer rests on the control.

control.

BorderStyle This property indicates the border style for the control.

LISTBOX
Constructor
Constructor Description

ListBox() This Constructors is used to initialize a new instance of the ListBox class.

Properties
Property Description

This property is used to get or set a value that indicates whether the
AutoSize
control resizes based on its contents.

BackColor This property is used to get or set the background color for the

15
Font This property is used to get or set the font of the text displayed by the
control.

ForeColor This property is used to get or set the foreground color of the control.

Height This property is used to get or set the height of the control.

Location This property is used to get or set the coordinates of the upper-left
corner of the ListBox control relative to the upper-left corner of its
form.

Name This property is used to get or set the name of the control.

TabStop This property is used to get or set a value that shows whether the user
can press the TAB key to provide the focus to the ListBox.

Size This property is used to get or set the height and width of the control.

Text This property is used to get or set the text to be displayed in the
RichTextBox control.

Visible This property is used to get or set a value indicating whether the control
and all its child controls are displayed.

Width This property is used to get or set the width of the control.

16
ColumnWidth This property is used to get or set the width of columns in a
multicolumn ListBox.

HorizontalExtent This property is used to get or set the width by which the horizontal
scroll bar of a ListBox can scroll.

ItemHeight This property is used to get or set the height of an item in the ListBox.

Items This property is used to get the items of the ListBox.

PreferredHeight This property is used to get the combined height of all items in the
ListBox.

SelectedIndex This property is used to get or set the zero-based index of the currently
selected item in a ListBox.

SelectedItem This property is used to get or set the currently selected item in the
ListBox.

SelectedIndices This property is used to get a collection that contains the zero-based
indexes of all currently selected items in the ListBox.

Sorted This property is used to get or set a value indicating whether the items
in the ListBox are sorted alphabetically.

17
TopIndex This property is used to get or set the index of the first visible item in
the ListBox.

18

You might also like