C# Unit4 Part1 GUI Programming(highlight)
C# Unit4 Part1 GUI Programming(highlight)
C# Unit4 Part1 GUI Programming(highlight)
When a programmer wants to design a Windows form application, the user defined Form 1 class always inherits
from Form base class.
Controls
C# .NET supports various built-in controls that help to create applications. Controls are the objects that can be drag
and dropped on the form. The properties of these controls can be changed or set to suit the needs of the user. Each
control can have a programming code attached to it.
Event
Programmer creates GUI and writes the code to describe what happens when user interacts with GUI such as click,
double click, mouse movement etc. These interactions are called as events.
Events are the actions performed by the user while interacting with the application.
The code that responds to these events are called event handler and such kind of programming is called Event
Driven Programming.
Form Object
The most basic object in C# .NET is the form object. The form represents a window or a dialog box that makes up
an application user interface. The form is made up of various elements such as title bar, control box and border.
The form class can be used to create standard modeless windows or modal windows. Also, SDI and MDI forms
can be designed.
TextBox Control
It represents a windows textbox. This object is commonly used to accept user inputs.
Label Control
Represents windows standard label. It is the control that displays read only text. Hence labels are mainly used to
display output on the window.
Button Control
Represents the windows standard button.
CheckBox Control
CheckBox provides an option to select. It is a control that is checked when selected or uncheck when not selected.
It is used when we wish to give two-way choices to the user such as true or false, yes or no etc.
RadioButton Control
A RadioButton is similar to checkbox control. That is, it provides the user with an option on form. Unlike a
checkbox control, if there are more than one radio buttons on the form then the user can select only one at a time.
ListBox Control
A ListBox is a control that displays items in the form of a list. The programmers initialise the list box either at the
design time or either at the time of coding.
The purpose of ListBox is to let the user select the value from the list rather than type the values. The vertical and
horizontal scroll bars appear in the list box if the height and width of list box are not sufficient to display all the
items.
When the user selects an item from list box, the following actions happen:
DateTimePicker Control
The DateTimePicker is a dropdown list box at the top and the month calendar control appears in it. If you click on
arrow in DateTimePicker, it displays a month calendar. You can make selection of a date by clicking on the
calendar.
Menu’s (MenuStrip)
An important feature of user interface in a windows-based application is the menu. The menu is created with the
MenuStrip object. Menu items and options under each menu items can be added as per the user requirements.
Shortcuts and hotkeys also can be designed. MenuStrip items functions as a button control in the form.
ContextMenuStrip
It is the menu that opens on right click operation of a control or area in a window-based form. The
ContextMenuStrip items functions as a button control.
The ContextMenuStrip property of the Form has to be set to proper contexMenuStrip object to enable the right-
click option on the respective form.
ToolStrip
ToolStrip is a container for ToolStripItem elements. Each individual element on the ToolStrip is a ToolStripItem
that manages the layout and event model for the type it contains.
• Button
• Label
• SplitButton
• DropDownButton
• Separator
• ComboBox
• TextBox
• PreogressBar
GDI
The common language runtime uses an advanced implementation of the Windows Graphics Device Interface
(GDI) called GDI+. With GDI+ you can create graphics, draw text, and manipulate graphical images as objects.
GDI+ is used to render graphical images on Windows Forms and controls.
The services of GDI+ are exposed through a set of managed classes. Following are the managed classes and their
purposes:
Class name Purpose
System.Drawing Provides access to GDI+ basic graphics functionality.
System.Drawing.Drawing2D Provides advanced two-dimensional and vector graphics
functionality.
System.Drawing.Imaging Provides advanced GDI+ imaging functionality.
System.Drawing.Text Provides advanced GDI+ typography functionality.
System.Drawing.Printing Provides print-related services.
The Graphics class is at the core of GDI+ functionality; it is the class that actually draws lines, curves, figures,
images, and text.
GDI+ draws lines, rectangles, and other shapes on a coordinate system. You can choose from a variety of
coordinate systems, but the default coordinate system has the origin in the upper-left corner with the x-axis
pointing to the right and the y-axis pointing down. The unit of measure in the default coordinate system is the
pixel.
A computer monitor creates its display on a rectangular array of dots called picture elements or pixels. The number
of pixels that appear on the screen varies from one monitor to the next, and the number of pixels that appear on an
individual monitor can usually be configured to some extent by the user.
Drawing a Line
To draw a line, call the DrawLine method of the
Graphics object. The Pen object is passed as one of the
arguments to the DrawLine method. The following Drawing a Rectangle
Drawing rectangles with GDI+ is similar to drawing
lines. To draw a rectangle, you need a Graphics
myGraphics.DrawEllipse(myPen,
100, 50, 80, 40);
Drawing an Ellipse
To draw an ellipse, you need a Graphics object and a Pen
object. The Graphics object provides the DrawEllipse
method, and the Pen object stores attributes, such as
width and color, of the line used to render the ellipse.
The Pen object is passed as one of the arguments to the
DrawEllipse method. The remaining arguments passed
to the DrawEllipse method specify the bounding
rectangle for the ellipse. The following example draws
an ellipse; the bounding rectangle has a width of 80, a
height of 40, and an upper-left corner of (100, 50):
Drawing a Polygon
To draw a polygon, you need a Graphics object, a Pen object, and an array of Point (or PointF) objects. The
Graphics object provides the DrawPolygon method. The Pen object stores attributes, such as width and color, of
the line used to render the polygon, and the array of Point objects stores the points to be connected by straight
lines. The Pen object and the array of Point objects are passed as arguments to the DrawPolygon method. The
following example draws a threesided polygon. Note that there are only three points in myPointArray : (0, 0), (50,
30), and (30, 60). The DrawPolygon method automatically closes the polygon by drawing a line from (30, 60) back
to the starting point (0, 0).
Point[] myPointArray =
{
new Point(0, 0), new Point(50, 30), new
Point(30, 60)
};
myGraphics.DrawPolygon(myPen, myPointArray);
Dialog Box
A dialog box is a type of window used to enable common communication or dialog between a computer and its
users. They can be used for displaying messages, passwords, confirmation or deletion etc.
Example : While saving a file if user gives an existing name then a warning is shown that file in the same name
exist and whether it must be overwritten. The parent dialog cannot be saved unless user clicks OK or Cancel
button.
Example : In an MS Word, if user wants to Find and Replace a particular word , modeless dialog box is used. The
user can continue to work even if dialog box is open.
Any windows can become an MDI parent, if you set the IsMdiContainer property to True.