0% found this document useful (0 votes)
15 views

Windows Forms and Control Programming

Uploaded by

Ayman Nabil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Windows Forms and Control Programming

Uploaded by

Ayman Nabil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

Windows Forms and Control

Programming
Prepared by:
Rajani Khushal K.
Introduction to Windows Forms
• System.Windows.Forms namespace used to
build windows based applications.
• There are three terms which belongs to
Windows.Forms namespace.
– Component
– Container
– Control
Introduction to Windows Forms
• Component:
– It is an object that permits sharing between
applications.
• Container:
– It is an object that can hold zero or more
components.
– A container is a grouping mechanism, and ensures
that sets of components are encapsulated and
manipulated in similar ways.
Introduction to Windows Forms
• Control:
– It is a component with a visual aspect.
– In Windows namespace, a control is a component
that presents a graphical interface on the window
desktop.
– Any visual interface we see on windows desktop is
a control, and any behind the control object of
that control is a component.
Introduction to Form
• The .NET framework provides all of the classes
that make up Windows Forms based
application through the System. Windows.
Forms namespace.
• Hierarchy of System. Windows. Forms
namespaces.
Introduction to Form
• Control
–ScrollableControl
• ContainerControl
–Form
–UserControl
Introduction to Form
• Control
– The Control Class is the fundamental base class for
other controls.
– It provides the basic functionality for a control,
such as sizing, visibility, and tab order.
• ScrollableControl
– It inherits directly from the Control class and
provides automatic scrolling capabilities for any
control that requires scroll bars.
Introduction to Form
• ContainerControl
– It inherits directly from the ScrollableControl class
and adds tabbing and focus management
functionality for controls that can host other
controls.
Introduction to Form
• Form:
– It inherits directly from the ContainerControl class
and represents any window displayed in the
application.
– The properties and methods provided by the Form
class allow us to display many different types of
forms, including dialog boxes and multiple
document interface (MDI) forms.
– All Windows forms are derived from this class.
Introduction to Form
• UserControl:
– It also inherits directly from the ContainerControl
class and provides an empty control that we can
use to create our own controls by using the
Windows Forms Designer.
Creating a
Windows Forms Application
Message Box
• It is a built in feature of Windows.
• A message box is a predefined window that
lets us display a message.
• We can also obtain simple responses from the
user such as Yes, No or Ok.
• In a window application, a message box is
supported by the MessageBox class.
• Messagebox is shown by using show() method
Message Box
• In windows application , a message box is
supported by the message box class.
• You can’t create a object of that class to
display a message box but you have to call the
static method called Show() which is defined
by message box
Message Box
• Messagebox is an enumeration that defines
the following values:
– OK
– AbortRetryIgnore
– OkCancel
– YesNo
– RetryCancel
– YesNoCancel
• It returns values by using DialogResult.
Message Box
• Returns value of Show method:

Abort Cancel Ignore No

None Ok Retry Yes


Message Box
• Message Icons Types:

Asterisk Error Hand

Exclamation None Question

Warning Information Stop


Dialog Box
• It is a form defined with particular properties.
• Like a form, a dialog box is referred to as a
container.
• Like a form, a dialog box is mostly used to host
child controls, insuring the role of dialog
between the user and the machine.
Characteristics of Dialog Box
• There is only close button.
• It cannot be minimized, maximized or
restored.
• It is usually modal, in which case the user is
not allowed to continue any other operation
on the same applications until the dailog box
is dismissed.
Uses of Dialog Box
• Display specific information to users.
• Gather information from users.
• Both display and gather information.
Types of Dialog Box
• There are two types of dialog boxes:
– Modal
– Modeless.
Dialog Box
• Modal:
– When a Modal dialog box is visible, it takes control
from application.
– Modal dialog boxes are mostly used for prompting
the user to make a selection, or to input some
data.
Dialog Box
• Modeless:
– Modeless dialog boxes are quite similar to popup
windows.
– These dialog boxes are used to display small
toolbars and other utility windows.
Different Types of Controls
• Label
• Button
• TextBox
• RadioButton
• ComboBox
• ListBox
• CheckedListBox
Different Types of Controls
• Panel Control:
– It enables us to work with a group of C# controls.
– We can use Panel control to hide or show a group
of components.
– The Panel Control is a container control to host a
group of similar child controls. One of the major
uses I have found for a Panel Control is when you
need to show and hide a group of controls.
Instead of show and hide individual controls, you
can simply hide and show a single Panel and all
child controls.
Different Types of Controls
• Panel Control
Properties Description

Border Style Get or Set type of border to display around the control

Enable Get or Set whether the panel is enabled. Controls within


the panel are disabled whenever the panel itself is
disabled

Visible Get or Set whether the panel is visible. Controls within


the panel are invisible whenever the panel itself is
invisible.
Different Types of Controls
• Group Box
– In Windows form, Group Box is a container which
contains multiple controls on it.

– In other words, Group Box is a frame display


around a group of controls with a suitable
optional title.
Different Types of Controls
• A Group Box is used to categorize the related
controls in a group. The GroupBox class is used to
represent the windows group box and also provide
different types of properties, methods, and events.

• It is defined
under System.Windows.Forms namespace.
Different Types of Controls
• Properties of Group Box
1. Location : Indicate location of group box
in windows form.
2. Size : Indicate the size of Group Box
3. Text : Indicate the text of group box ,
shows the purpose and content
inside the group box.
4. Name : Shows the name of group box
Different Types of Controls
• ListView Control
– It displays a list of items with icons.
– We can use a list view to create a user interface
like the right pane of Windows Explorer.
– The control has four view modes:
• LargeIcon
• SmallIcon
• List
• Details
Different Types of Controls
• ListView Control

Items Gets the collection of items in the list


LabelEdit Gets or sets weather user can edit item labels in the
list

LargeImageList Gets or Sets the ImageList for the largeIcon view


MultiSelect Gets or Sets whether multiple items in the list may
be selected as the same time. Default is false

SelectedItems Gets the collection of items selected in the list


SmallimageList Gets or Sets the Image list instance for the views
other than the LargeIcon view

Sorting Gets or Sets the how items in the list are sorted
Different Types of Controls
• ListView Control

MultiSelect Get or Set whether multiple items in the list may be


selected at the same time. Default is false
• Methods

Clear Remove all items and columns from list view control

EnsureVisible Ensure given items is visible.


• Public Events

AftreLabelEdit Occur after an item label has been edited


ColumnClick Occur When users click a column header in the details view
ItemDrag Occur when a user begins dragging an item to the list
SelectedIndexChanged Occur when a selection state of the item changed
Different Types of Controls
• TreeView Control
– It is used to display a hierarchy of nodes to users,
like file and folders are displayed in left pane of
Windows Explorer.
– Each node in the tree view might contain other
nodes called child nodes.
– We can display parent nodes or nodes that
contain child nodes as expanded or collapsed.
– We can also have check box next to nodes.
Different Types of Controls
• TreeView Control Properties
– CheckBoxes
– HideSelection
– Nodes
– PathSeparator
– SelectedNode
– ShowPlusMinus
– TopNode
Different Types of Controls
• TreeView Control Methods
– CollapseAll
– GetNodeAt
– GetNodeCount
Different Types of Controls
• TreeView Control Events
– AfterExpand
– BeforeCollapse
– BeforeSelect
Different Types of Controls
• ImageList
– It is used to store images, which can be displayed
by controls.
Different Types of Controls
• ScrollBar:
– They are used to provide easy navigation through
a long list of items or a large amount of
information by scrolling either horizontally or
vertically within an application or control.
– Scroll bars are common element of the Window
interface which is derived from ScrollableControl
class.
Different Types of Controls
• Tooltip:
– It provides short and quick explanations of the
purpose of a control or other object.
– A number of classes provide their own tool tip
mechanism through a ToolTipText property.
– The ToolTip class is a component that provides a
small popup window for a control.
– This window normally contain short phrase
describing the purpose of the control.
Different Types of Controls
• Tooltip Properties:
– Active
– AutomaticDelay
– AutoPopDelay
– InitialDelay
– ReshowDelay
– ShowAlways
Different Types of Controls
• Tooltip Methods:
– GetToolTip
– RemoveAll
– SetToolTip
Different Types of Controls
• NotifyIcon:
– Icons in the notification area shortcuts to
processes that are running in the background of a
computer, such as My Computer or Recycle Bin.
– These processes do not come with their own user
interface.
– The NotifyIcon class provides a way to program in
this functionality.
– To handle this object doubleclick event handler is
used
Different Types of Controls
• Timer:
– It is a component that raises an event at regular
intervals.
– The Windows Forms Timer class represents a
timer component that raises events at user
defined intervals.
– This timer is optimized for use in Windows Forms
Applications and occurs within the processing
thread of a Form Object.
Different Types of Controls
• Timer Properties:
– Enabled
– Interval
• Timer Methods:
– Start
– Stop
• Timer Events:
– Tick
Different Types of Controls
• Menu (MenuStrip)
The MenuStrip control was introduced in version 2.0 of
the .NET Framework.

The MenuStrip control supports the multiple-document


interface (MDI) and menu merging, tool tips.

You can enhance the usability and readability of your


menus by adding access keys, shortcut keys, check marks,
images.
Different Types of Controls
• PictureBox
The picture Box control is used to display graphics in
bitmap , gif , jpeg or icon format.
Scroll bar are not supported when the image is
larger that the client area , so care must be taken to
ensure that the image appears properly with the
control.
You can set the picture at run time using image
property.
Different Types of Controls
• Picture Box Properties
Properties Description

BorderStyle Get or set the style of the border to display for the control

Image Get or Set the image to display in the picture box

SizeMode Get or set the pictureBox Size Mode enumeration value


indicating how the image is display. The default is normal.
Different Types of Controls
• Example :
pictureBox1.Image =
Image.FromFile("c:\\testImage.jpg");

The SizeMode property, which is set to values in


the PictureBoxSizeMode enumeration, controls
the clipping and positioning of the image in the
display area.
Different Types of Controls
• Example :
pictureBox1.SizeMode =
PictureBoxSizeMode.StretchImage;
Different Types of Controls
There are five different PictureBoxSizeMode is
available to PictureBox control.
• AutoSize - Sizes the picture box to the image.
• CenterImage - Centers the image in the
picture box.
• Normal - Places the upper-left corner of the
image at upper left in the picture box
• StretchImage - Allows you to stretch the image
in code
Different Types of Controls
• RichTextBox:
– It is a text editing control that can handle special
formatting features.
– The Windows Forms RichTextBox control is used
for displaying, entering and manipulating text wit
formatting.
– It is similar to TextBox Control but additionally it
can display fonts, colors and links; load text and
embedded images from a file; and find specificed
characters.
Different Types of Controls
• Selection Properties of RichTextBox:
– SelectionFont
– SelectionColor
– SelectionBullet
• Paragraph Formatting Properties:
– SelectionIndent
– SelectionRightIndent
– SelectionHangingIndent
Different Types of Controls
• Manipulate Files
– LoadFile
– SaveFile
Different Types of Controls
• ProgressBar:
– It visually indicates the progress of a lengthy
operation in one of three styles:
• Segmented Blocks that increase in steps from left to
right.
• A continuous bar that fills from left to right
• A block that scrolls across a ProgressBar in a marquee
fashion
Different Types of Controls
• ProgressBar Properties:
– Minimum
– Maximum
– Step
– Value
• ProgressBar Methods:
– Increment
– PerformStep
Different Types of Controls
• TabControl:
– They are used to compact a large amount of data
into a single form by segmenting the data into
different screens, or tab pages.
– The tabs can contain pictures and other controls.
– We can use tabs to create multiple page dialog
box.
Different Types of Controls
• Tab Pages:
– They are the heart and soul of a tab control.
– They define the tabs displayed to the user and the
layout of the controls that appear when each page
is displayed.
– Tab Pages are inherited from the Panel Class.
– TabPages collection reflects the order of tabs in
the TabControl.
Different Types of Controls
• TabControl Properties:
– Alignment
– Appearance
– DrawMode
– HotTrack: Tab changes appearance when mouse
moves over them.
– ImageList
– ItemSize
– MultiLine
Different Types of Controls
• TabControl Properties:
– SelectedIndex
– SelectedTab
– RowCount
– ShowToolTips
– SizeMode
– TabCount
– TabPages
Different Types of Controls
• TabControl Methods:
– GetTabRect
• TabControl Events:
– DrawItem
– SelectedIndexChanged
Data Grid View
• Displaying data in a tabular format is a task
you are likely to perform frequently.
• The DataGridView control is designed to be a
complete solution for displaying tabular data
with Windows Forms.
• The DataGridView control is highly
configurable and extensible, and it provides
many properties, methods, and events to
customize its appearance and behavior.
Data Grid View
• The DataGridView control makes it easy to
define the basic appearance of cells and the
display formatting of cell values.
• All cells derive from the DataGridViewCell
base class.
• Each cell within the DataGridView control can
have its own style, such as text format,
background color, foreground color, and font.
Events

DataGridView.CellValueChanged The DataGridView.CellValueChanged even


t occurs when the user-specified value is
committed, which typically occurs when
focus leaves the cell.
RowLeave Occurs when a row loses input focus and
is no longer the current row.
SelectionChanged Occurs when the current selection
changes.
TextChanged Occurs when the value of
the Text property changes.
Property
Property Name Description

RowCount Gets or sets the number of rows displayed


in the DataGridView.
SelectedCells Gets the collection of cells selected by the
user.
SelectedRows Gets the collection of rows selected by
the user.
SelectedColumns Gets the collection of columns selected by
the user.
ScrollBars Gets or sets the type of scroll bars to
display for the DataGridView control.
RowHeaderVisible Gets or sets a value indicating whether
the column that contains row headers is
displayed.
ReadOnly Gets or sets a value indicating whether
the user can edit the cells of
the DataGridView control.
Methods

AutoResizeColumns Adjusts the width of all columns to fit the


contents of their cells.

AutoResizeRows Adjusts the heights of some or all rows to


fit their contents.

OnMouseClick Raises the MouseClick event.

OnRowEnter Raises the RowEnter event.

You might also like