0% found this document useful (0 votes)
12 views68 pages

Module 6

This document covers advanced GUI creation using C# in the context of Dot Net Technologies, focusing on printing, graphics, custom controls, and dialog boxes. It explains how to use components like PrintDocument, GDI+, and various dialog types to enhance user interaction in Windows Forms applications. Additionally, it provides guidance on creating custom controls and managing user inputs through dialog boxes.

Uploaded by

PAulos
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)
12 views68 pages

Module 6

This document covers advanced GUI creation using C# in the context of Dot Net Technologies, focusing on printing, graphics, custom controls, and dialog boxes. It explains how to use components like PrintDocument, GDI+, and various dialog types to enhance user interaction in Windows Forms applications. Additionally, it provides guidance on creating custom controls and managing user inputs through dialog boxes.

Uploaded by

PAulos
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

Department of CE/IT

Dot Net Technologies

Creating Advanced GUI using C# Unit no : 6


Dot Net Technologies
(01CE0602)
Unit 6: Creating Advanced GUI using C#
1) Printing in C# Department of CE/IT
2) Working with Graphics GDI+
3) Creating custom control from scratch Unit no : 6
4) Dialog Boxes Dot Net Technology
(01CE0602)
Printing in Windows Forms consists primarily of using
the PrintDocument Component, component to enable the user to
print.

And the PrintPreviewDialog Control control, PrintDialog


Component and PageSetupDialog Component components to provide

Printing in
a familiar graphical interface to users accustomed to the Windows
operating system.
C#
Typically, you create a new instance of
the PrintDocument component, set the properties that describe what
to print using the PrinterSettings and PageSettings classes, and call
the Print method to actually print the document.
During the course of printing from a Windows-based application,
the PrintDocument component will show an abort print dialog box to
alert users to the fact that printing is occurring and to allow the print
job to be canceled.

Printing in
C#
Add a PrintDocument Component to your form
Write code to handle the PrintPage event.
Look at the Example below, in PrintPage event we will print rectangle
with color.

Creating a
Print Job
Now this PrintPage event will be raised, as soon as you call Print()
methods with object of PrintDocument component added to the form.
Creating a
Print Job

Now when you click the button on form as shown in next screen, it will
send the document to be printed the default printer.
It will print a rectangle with red color.
Creating a
Print Job
Creating a
Print Job
You can cancel the print job, by clicking on cancel button.
Now to prview, the page being printed before printing, we can use
print preview dailog.
PrintPreviewDialog will provide a used the preview, of what the
associated document will look like when printed.

To preview the document, add the print preview dialog from toolbox.

Associate the document to be print with PrintPreviewDialog control as


PrintPreview show below.

Dialog
Let see the whole code together

PrintPreview
Dialog
Let see the whole code together

PrintPreview
Dialog
 on clicking print button on form, print preview dialog will be shown
of the associated page to be sent to printer.
Now if you click print icon in print preview dialog, form will be
printed, if closed form will not be sent further for printing.
As we call [Link](), if user clicks ok

PrintPreview
Dialog
To do setting related to page, you can make use of PageSetupDialog
box.
To associate the document to be printed with the selected page setup
setting, assign document to be printed to page setup document as
shown below.

PageSetupDialog
Whole code toghether

PageSetupDialog
PageSetupDialog
GDI+ is a Graphics Device Interface that enables programmers to
write device-independent applications.
The services of GDI+ are exposed through a set of managed classes.
GDI+ is an application programming interface (API) that forms the
subsystem of the Microsoft Windows operating system.
Working GDI+ is responsible for displaying information on screens and
with printers.

Graphics The GDI+ API is exposed through a set of classes deployed as


managed code.
GDI+ This set of classes is called the managed class interface to GDI+. The
following namespaces make up the managed class interface:
[Link], [Link].2D, [Link],
[Link], [Link] Text.
Before you can draw lines and shapes, render text, or display and
manipulate images with GDI+, you need to create a Graphics object.
 The Graphics object represents a GDI+ drawing surface, and is the
object that is used to create graphical images.
There are two steps in working with graphics:
Working 1. Creating a Graphics Object
with 2. Using the Graphics object to draw lines and shapes, render text, or
Graphics display and manipulate images.

GDI+
Encapsulates a GDI+ drawing surface. This class cannot be inherited.

The Graphics class provides methods for drawing objects to the


display device.

You can draw many different shapes and lines by using methods of
Graphics a Graphics object such as DrawLine(), DrawRectangle() etc.

Class Graphics object has propeties such as


Clip - Gets or sets a Region that limits the drawing region of
this Graphics.
ClipBounds - Gets a RectangleF structure that bounds the clipping
region of this Graphics.
DpiX: Gets the horizontal resolution of this Graphics.
DpiY: Gets the Vertical resolution of this Graphics.
A graphics object can be created in a variety of ways.
Way 1: As an argument of PaintEvent
 Receive a reference to a graphics object as part of
the PaintEventArgs in the Paint event of a form or control.
 This is usually how you obtain a reference to a graphics object when
Creating creating painting code for a control.

Graphics
Object
Way 2: CreateGraphics methods of control
 Call the CreateGraphics method of a control or form to obtain a
reference to a Graphics object that represents the drawing surface of
that control or form.
 Use this method if you want to draw on a form or control that
already exists.
Creating
Graphics
Object
Way 3: To create a Graphics object from an Image
 Call the [Link] method, supplying the name of the
Image variable from which you want to create a Graphics object.

Creating
Graphics
Object
To draw lines with GDI+ you need to create a Graphics object and
a Pen object.
The Graphics object provides the methods that actually do the
drawing, and the Pen object stores attributes, such as line color, width,
and style.
Using  To create Pen Object:

Graphics
Object to
Draw
Defines an object used to draw lines and curves. This class cannot be
inherited.

Overloaded Constructor
1. Pen(Brush)Initializes a new instance of the Pen class with the
specified Brush.

Pen Class 2. Pen(Color)Initializes a new instance of the Pen class with the
specified color.
3. Pen(Brush, Single)Initializes a new instance of the Pen class with
the specified Brush and Width.
4. Pen(Color, Single)Initializes a new instance of the Pen class with
the specified Color and Width properties.
Properties of Pen Class:
1. Width - Gets or sets the width of this Pen, in units of the Graphics
object used for drawing.
2. Color - Gets or sets the color of this Pen.
3. Brush - Gets or sets the Brush that determines attributes of this Pen.
4. StartCap – Gets or sets the cap style used at the startof lines drawn
Pen Class with this Pen.
5. EndCap - Gets or sets the cap style used at the end of lines drawn
with this Pen.
Methods:
1. Clone() - Creates an exact copy of this Pen.
2. Dispose() - Releases all resources used by this Pen.
DrawLine has 4 Overloaded methods

DrawLine()
Example
DrawLine()
DrawRectangle has 3 Overloaded Methods:

DrawRectangle()
Example
DrawRectangle()
DrawEllipse has 4 Overloaded methods

DrawEllipse()
Example
DrawEllipse()
A closed shape, such as a rectangle or an ellipse, consists of an outline
and an interior.
The outline is drawn with a pen and the interior is filled with a brush.
GDI+ provides several brush classes for filling the interiors of closed
shapes:
1. SolidBrush,
Filling 2. HatchBrush,
Graphical 3. TextureBrush,
Object 4. LinearGradientBrush
 All of these classes inherit from the Brush class.
 Brush Class is Abstract.
 HatchBrush and LinearGradientBrush avaialble in namespace -
[Link].Drawing2D
 Whereas, SolidBrush and TextureBrush available in
[Link]
This is an abstract base class and cannot be instantiated.

To create a brush object, use classes derived from Brush, such as
SolidBrush, TextureBrush, and LinearGradientBrush.

Brush Method:
1. Clone() - When overridden in a derived class, creates an exact copy
Class of this Brush.
2. Dispose() - Releases all resources used by this Brush object.
3. Equals(Object) - Determines whether the specified object is equal to
the current object.
Creating
SolidBrush
FillRectangle()
Example
FillRectangle()
FillEllipse()
Example
FillEllipse()
HatchBrush
LinearGradient
Brush
Draws the specified text string at the specified location with the
specified Brush and Font objects.

DrawString()
Defines a particular format for text, including font face, size, and style
attributes.

This class cannot be inherited.

Constructors: Font class has many overloaded Constructors, for


Font example:
Class Font(String, Single) - public Font (string familyName, float emSize);
Font(String, Single, FontStyle) –
public Font([Link] family, float emSize,
[Link] style);
Properties:
1. Bold - Gets a value that indicates whether this Font is bold.
2. Italic - Gets a value that indicates whether this Font is Italic.
3. Size - Gets the em-size of this Font measured in the units specified
by the Unit property.
4. Height - Gets the line spacing of this font.
Font
Class  Methods:
1. Clone() - Creates an exact copy of this Font.
2. Dispose() - Releases all resources used by this Font.
3. GetHeight() - Returns the line spacing of this font.
Example
DrawString()
Windows Forms controls are reusable components that encapsulate
user interface functionality and are used in client-side

Windows-based applications. Not only does Windows Forms provide


many ready-to-use controls, it also provides the infrastructure for
developing your own controls.

Custom
Control You can combine existing controls, extend existing controls, or author
your own custom controls.
Extending Control – Using Inheritance

Custom
Control
Painting of a control is accomplished by the execution of code in the
control's OnPaint method.

The single argument of the OnPaint method is a PaintEventArgs object


that provides all of the information and functionality required to render
your control.

Custom
Control PaintEventArgs provides as properties two principal objects that will
be used in the rendering of your control:
1)ClipRectangle 2)Graphics Object

The OnPaint event is fired whenever the control is drawn or refreshed


on the screen, and the ClipRectangle object represents the rectangle in
which painting will take place.
When inheriting from the Control class, you must override
the OnPaint method and provide graphics-rendering code within.

 If you want to provide a custom graphical interface to a user control


or an inherited control, you can also do so by overriding
the OnPaint method.

Custom
Control
After Building the project, the control will be available in tool box as
user control and can be dragged and added as other control to the form

Custom
Control
Creating User Control

Provides an empty control that can be used to create other controls.

The UserControl gives you the ability to create controls that can be
Custom used in multiple places within an application or organization.

Control You can include all the code needed for validation of common data
you ask the user to input; some examples of this are email addresses.

Lets create a UserControl named Label_Text as every TextBox has


Label Associated with it to convey the type of input in TextBox.
To create UserControl
Click in Project – Click Add User Control (Windows Forms)..

Custom
Control
Select UserControl (Windows Forms) and provide Name as
Label_Textbox.cs

Custom
Control
You will be provided with empty panel type component, as shown
below

Custom
Control

Drag and Drop, Label and a TextBox into it.


Go to Code windows by pressing F7 and put the following code
In the code added a property LabelText, that changes text of Label and
KeyPress Event on TextBox to only numeric Input.

Custom
Control
Build the project again and the User Control Label_Textbox will be
available in toolbox for usage.

Custom
Control

Now drag and drop the control the form as you do for other controls.
Using Label_Textbox Control to take age as input.

Custom
Control
 Dialog boxes are windows but with a specific intent and user
experience.

A dialog box in C# is a type of window, which is used to enable


common communication or dialog between a computer and its user.

Dialog Boxes  A dialog box is most often used to provide the user with the means for
specifying how to implement a command or to respond to a question.

[Link] is a base class for a dialog box.


Dialog boxes are used to:

Display specific information to users.


Gather information from users.
Both display and gather information.
Display an operating system prompt, such as print window.
Select a file or folder.
Dialog Boxes
 A dialog box can be displayed in two ways: modal and modeless.

Displaying a modal dialog box to the user is a technique with which


the application interrupts what it was doing until the user closes the
dialog box.
Dialog boxes are used to:

Display specific information to users.


Gather information from users.
Both display and gather information.
Display an operating system prompt, such as print window.
Select a file or folder.
Dialog Boxes
 A dialog box can be displayed in two ways: modal and modeless.

Dialogs

Modeless Modal
A modeless dialog box doesn't prevent a user from activating other
windows while it's open.

For example, if a user wants to find occurrences of a particular word in


a document, a main window will often open a dialog box to ask a user
what word they're looking for.

Modeless Since the application doesn't want to prevent the user from editing the
Dialog Boxes document, the dialog box doesn't need to be modal.

A modeless dialog box at least provides a Close button to close the


dialog box.

Other buttons may be provided to run specific functions, such as


a Find Next button to find the next word in a word search
Displaying a modal dialog box to the user is a technique with which
the application interrupts what it was doing until the user closes the
dialog box.

This generally comes in the form of a prompt or alert.

Modal Other windows in the application can't be interacted with until the
Dialog Boxes dialog box is closed.

Once the modal dialog box is closed, the application continues.

The most common dialog boxes are used to show an open file or save
file prompt, displaying the printer dialog, or messaging the user with
some status.
Displaying a modal dialog box to the user is a technique with which
the application interrupts what it was doing until the user closes the
dialog box.

This generally comes in the form of a prompt or alert.

Display Form
as Modal Other windows in the application can't be interacted with until the
dialog box is closed.
Dialog Boxes
Once the modal dialog box is closed, the application continues.

The most common dialog boxes are used to show an open file or save
file prompt, displaying the printer dialog, or messaging the user with
some status.
Creating 2 forms – Form1 and Form2 .
Displaying form2 as Modal Dialog Box and displaying the text box
value in dialog box, in form1 after submitting.

Display Form
as Modal
Dialog Boxes
Display Form
as Modal
Dialog Boxes
Set property of ok button from properties window
Similarly for cancel button from properties window

Display Form
as Modal
Dialog Boxes
Display Form
as Modal
Dialog Boxes
Display Form On clicking Show Dialog
as Modal
Dialog Boxes
On clicking Ok, content of textbox1 in form 2 will be visible in form1

Display Form
as Modal
Dialog Boxes
Thanks

You might also like