2160711 Dot Net Technology Unit 7 Mastering Windows

  • Slides: 49
Download presentation
2160711 Dot Net Technology Unit - 7 Mastering Windows Forms Prof. Naimish R. Vadodariya

2160711 Dot Net Technology Unit - 7 Mastering Windows Forms Prof. Naimish R. Vadodariya 8866215253 naimish. vadodariya@darshan. ac. in

Outline § Printing § Handling Multiple Events § GDI+ § Creating Windows Forms Controls

Outline § Printing § Handling Multiple Events § GDI+ § Creating Windows Forms Controls Unit: 7 – Mastering Windows Forms 2 Darshan Institute of Engineering & Technology

Printing § In C#. NET, the Print. Document class allows you to print documents.

Printing § In C#. NET, the Print. Document class allows you to print documents. § You can add an object of this class to a project and than handle events, such as Print. Page. § The event Print. Page is called every time a new page is to be printed. § When Print. Document is added to a form, the Print. Document component appears in the component tray at the bottom of the windows forms designer. Unit: 7 – Mastering Windows Forms 3 Darshan Institute of Engineering & Technology

Printing Controls § Page. Setup. Dialog § Print. Document § Print. Preview. Control §

Printing Controls § Page. Setup. Dialog § Print. Document § Print. Preview. Control § Print. Preview. Dialog Unit: 7 – Mastering Windows Forms 4 0 Darshan Institute of Engineering & Technology

Page. Setup. Dialog § The Page. Setup. Dialog box allows you to specify page

Page. Setup. Dialog § The Page. Setup. Dialog box allows you to specify page orientation, paper size and margin size. § Page. Setup. Dialog control sets the page-related settings. § Namespace : • System. Windows. Forms. Page. Se tup. Dialog Unit: 7 – Mastering Windows Forms 5 0 Darshan Institute of Engineering & Technology

Print. Dialog § The Print. Dialog box lets the user print documents and this

Print. Dialog § The Print. Dialog box lets the user print documents and this dialog box is supported by the Print. Dialog class. § Namespace : • System. Windows. Forms. Print. Dialog Unit: 7 – Mastering Windows Forms 6 0 Darshan Institute of Engineering & Technology

Print. Document § The Print. Document control supports the actual events and opertations of

Print. Document § The Print. Document control supports the actual events and opertations of printing in C#. § Also sets the properties that describes how to print. § The Document property of the Print. Dialog class needs to be set before calling the Print dialog box. § This property accepts an object of the Print. Document class, which obtains the printer settings and sends the output to the printer. § Namespace : • System. Drawing. Print. Document Unit: 7 – Mastering Windows Forms 7 0 Darshan Institute of Engineering & Technology

Print. Preview. Control § Print. Preview. Control displays a document to be printed- e.

Print. Preview. Control § Print. Preview. Control displays a document to be printed- e. g. , it displays a Review of the document to be printed. This control has no buttons or any other user interface elements. § The Print. Preview. Control is typically used only when there is a need to write custom print preview user interfaces. § You can use Print. Preview. Control objects to create your own custom print previews. § Namespace : • System. Windows. Forms. Print. Preview. Control Unit: 7 – Mastering Windows Forms 8 0 Darshan Institute of Engineering & Technology

Print. Preview. Dialog § You can use the Print. Preview. Dialog that displays a

Print. Preview. Dialog § You can use the Print. Preview. Dialog that displays a preview of the document that is to be printed. § The Print. Preview. Dialog class supports the Print. Preview. Dialog control. § This class contains the buttons for printing, zooming in, displaying one or multiple pages, and closing the dialog box. § Namespace : • System. Windows. Forms. Print. Preview. Dialog Unit: 7 – Mastering Windows Forms 9 0 Darshan Institute of Engineering & Technology

Print. Preview. Dialog Cont. . Unit: 7 – Mastering Windows Forms 10 0 Darshan

Print. Preview. Dialog Cont. . Unit: 7 – Mastering Windows Forms 10 0 Darshan Institute of Engineering & Technology

Example - Printing § Take a windows form with Rich. Text. Box and two

Example - Printing § Take a windows form with Rich. Text. Box and two Button control and also take printing controls from toolbox, as shown in Figure: Unit: 7 – Mastering Windows Forms 11 0 Darshan Institute of Engineering & Technology

Example – Printing - Cont. . § Print. Preview Button private void btn. Print.

Example – Printing - Cont. . § Print. Preview Button private void btn. Print. Preview_Click(object sender, Event. Args e) { print. Preview. Dialog 1. Document = print. Document 1; print. Preview. Dialog 1. Show. Dialog(); } § Print. Document 1 (Print. Page Event) e. Graphics. Draw. String(rich. Text. Box 1. Text, new Font("Arial", Convert. To. Int 64(12)), Brushes. Blue, e. Margin. Bounds. Left + 10, e. Margin. Bounds. Top + 10); Unit: 7 – Mastering Windows Forms 12 Darshan Institute of Engineering & Technology

Example – Printing Cont. . § Print Button page. Setup. Dialog 1. Printer. Settings

Example – Printing Cont. . § Print Button page. Setup. Dialog 1. Printer. Settings = print. Document 1. Printer. Settings; page. Setup. Dialog 1. Page. Settings = print. Document 1. Default. Page. Settings; if (page. Setup. Dialog 1. Show. Dialog() == Dialog. Result. OK) { print. Document 1. Printer. Settings = page. Setup. Dialog 1. Printer. Settings; print. Document 1. Default. Page. Settings = page. Setup. Dialog 1. Page. Settings; } print. Preview. Dialog 1. Document = print. Document 1; print. Preview. Dialog 1. Show(); Unit: 7 – Mastering Windows Forms 13 Darshan Institute of Engineering & Technology

Example – Printing Cont. . private void btn. Print. Preview_Click( object sender, Event. Args

Example – Printing Cont. . private void btn. Print. Preview_Click( object sender, Event. Args e) { print. Preview. Dialog 1. Document = print. Document 1; print. Preview. Dialog 1. Show. Dialog(); } private void btn. Print_Click(object sender, Event. Args e) { page. Setup. Dialog 1. Printer. Settings = print. Document 1. Printer. Settings; page. Setup. Dialog 1. Page. Settings = print. Document 1. Default. Page. Settings; if (page. Setup. Dialog 1. Show. Dialog() == Dialog. Result. OK) { print. Document 1. Printer. Settings = page. Setup. Dialog 1. Printer. Settings; print. Document 1. Default. Page. Settings = page. Setup. Dialog 1. Page. Settings; } print. Preview. Dialog 1. Document = print. Document 1; print. Preview. Dialog 1. Show(); } private void print. Document 1_Print. Page( object sender, Print. Page. Event. Args e) { e. Graphics. Draw. String(rich. Text. Box 1. Text, new Font("Arial", Convert. To. Int 64(12)), Brushes. Blue, e. Margin. Bounds. Left + 10, e. Margin. Bounds. Top + 10); } Unit: 7 – Mastering Windows Forms 14 Darshan Institute of Engineering & Technology

Output Unit: 7 – Mastering Windows Forms 15 Darshan Institute of Engineering & Technology

Output Unit: 7 – Mastering Windows Forms 15 Darshan Institute of Engineering & Technology

Output Cont. . Unit: 7 – Mastering Windows Forms 16 Darshan Institute of Engineering

Output Cont. . Unit: 7 – Mastering Windows Forms 16 Darshan Institute of Engineering & Technology

Handling Multiple Events § C#. NET is an event-driven language, which means the objects

Handling Multiple Events § C#. NET is an event-driven language, which means the objects of C#. NET reacts when an event is triggered, for example, clicking a button or entering some text into a text box & many more. § An event is nothing but an action that calls a function or may cause another event, which do something as specified in the code. § There are mainly two types of events: • Mouse events (events generated by supplying the input through a mouse) • Keyboard events (events generated by supplying the input through the keyboard) Unit: 7 – Mastering Windows Forms 17 0 Darshan Institute of Engineering & Technology

Mouse Events § You can handle mouse events-such as mouse movements in forms and

Mouse Events § You can handle mouse events-such as mouse movements in forms and controls. Events Description Mouse. Down Occurs when the mouse pointer is over the control and a mouse button is pressed Mouse. Enter Occurs when the mouse pointer enters the control Mouse. Hover Occurs when the mouse pointer moves to and fro over the control Mouse. Leave Occurs when the mouse pointer leaves the control Mouse. Move Occurs when the mouse pointer moves over the control Mouseup Occurs when the mouse pointer is over the control and a mouse button is released Mouse. Wheel Occurs when the mouse wheel moves, while the control has focus Unit: 7 – Mastering Windows Forms 18 0 Darshan Institute of Engineering & Technology

Keyboard Events § In C#. NET, the following events are available to handle the

Keyboard Events § In C#. NET, the following events are available to handle the keyboard events: Events Description Key. Down Occurs when a key is pressed down while the control has focus Key. Press Occurs when a key is pressed while the control has focus Key. Up Occurs when a key is released while the control has focus Unit: 7 – Mastering Windows Forms 19 0 Darshan Institute of Engineering & Technology

Example - Handling Multiple Events § Create a form with two labels, two textbox

Example - Handling Multiple Events § Create a form with two labels, two textbox and a button control. Unit: 7 – Mastering Windows Forms 20 0 Darshan Institute of Engineering & Technology

Handling Multiple Events Cont. . using System; using System. Drawing; using System. Windows. Forms;

Handling Multiple Events Cont. . using System; using System. Drawing; using System. Windows. Forms; namespace Demo_Windows { public partial class frm. Multiple. Events : Form { public frm. Multiple. Events() { Initialize. Component(); } private void text. Box 1_Mouse. Enter(object sender, Event. Args e) { text. Box 1. Back. Color = Color. Red; text. Box 1. Fore. Color = Color. White; } private void text. Box 1_Mouse. Leave(object sender, Event. Args e) { text. Box 1. Back. Color = Color. White; text. Box 1. Fore. Color = Color. Black; } Unit: 7 – Mastering Windows Forms 21 0 Darshan Institute of Engineering & Technology

Handling Multiple Events Cont. . private void text. Box 2_Mouse. Enter(object sender, Event. Args

Handling Multiple Events Cont. . private void text. Box 2_Mouse. Enter(object sender, Event. Args e) { text. Box 2. Back. Color = Color. Red; text. Box 2. Fore. Color = Color. White; } private void text. Box 2_Mouse. Leave(object sender, Event. Args e) { text. Box 2. Back. Color = Color. White; text. Box 2. Fore. Color = Color. Black; } private void btn. Add_Click(object sender, Event. Args e) { Message. Box. Show("Your name is " + text. Box 1. Text + " " + text. Box 2. Text); } } } Unit: 7 – Mastering Windows Forms 22 0 Darshan Institute of Engineering & Technology

Output Unit: 7 – Mastering Windows Forms 23 0 Darshan Institute of Engineering &

Output Unit: 7 – Mastering Windows Forms 23 0 Darshan Institute of Engineering & Technology

GDI+ § GDI+ stands for Graphics Device Interface. § In Windows Forms Applications, GDI+

GDI+ § GDI+ stands for Graphics Device Interface. § In Windows Forms Applications, GDI+ is used to create graphics, draw text, and manipulate graphical images. § The basic use of GDI+ is to render graphical images on a form. § The core of the GDI+ functionality is the Graphics class. § GDI+ consists of a set of base classes that are available to control the custom drawing on the screen. § This set of classes is collectively called the GDI+ managed class interface. Unit: 7 – Mastering Windows Forms 24 Darshan Institute of Engineering & Technology

Namespaces of GDI+ System. Drawing 2 D System. Drawing. Imaging System. Drawing. Printing System.

Namespaces of GDI+ System. Drawing 2 D System. Drawing. Imaging System. Drawing. Printing System. Drawing. Design System. Drawing. Text Unit: 7 – Mastering Windows Forms 25 Darshan Institute of Engineering & Technology

Namespaces of GDI+ Cont. . Namespace Description System. Drawing Contains most of the classes,

Namespaces of GDI+ Cont. . Namespace Description System. Drawing Contains most of the classes, structs, enums and delegates concerned with the basic functionality of drawing System. Drawing 2 D Provides support for advance 2 -D vector, drawing and graphics path System. Drawing. Imaging Contains classes that provide advanced imaging functionality System. Drawing. Printing Contains classes that assist in printing System. Drawing. Design Contains classes that extend design-time UI logic and drawing System. Drawing. Text Contains classes to provide advanced GDI+ typography functionality Unit: 7 – Mastering Windows Forms 26 Darshan Institute of Engineering & Technology

Graphics Class § Graphics class provides a method, such as Create. Graphics(), for drawing

Graphics Class § Graphics class provides a method, such as Create. Graphics(), for drawing objects. § In addition, you can draw many different shapes and lines by using different methods of the Graphics object, such as Draw. Line, Draw. Arc, Draw. Closed. Curve, Draw. Polygon, and Draw. Rectangle. § You can also draw images by using the Drawlmage() and Drawlcon() methods of the Graphics class. Unit: 7 – Mastering Windows Forms 27 Darshan Institute of Engineering & Technology

Graphics Objects Name Description Brush Used to fill enclosed surfaces with patterns, colors, or

Graphics Objects Name Description Brush Used to fill enclosed surfaces with patterns, colors, or bitmaps. Pen Used to draw lines and polygons, including rectangles, arcs, and pies. Font Used to describe the font to be used to render text. Color Used to describe the color used to render a particular object. In GDI+ color can be alpha blended. Unit: 7 – Mastering Windows Forms 28 Darshan Institute of Engineering & Technology

Pen Class § A pen draws a line of specified width and style. §

Pen Class § A pen draws a line of specified width and style. § You always use Pen constructor to create a pen. § The constructor initializes a new instance of the Pen class. You can initialize it with a color or brush. § Example • public Pen(Color); o Pen pn = new Pen(Color. Blue); • public Pen(Brush); Unit: 7 – Mastering Windows Forms 29 Darshan Institute of Engineering & Technology

Properties of Pen Class Property Description Alignment Gets or sets the alignment for objects

Properties of Pen Class Property Description Alignment Gets or sets the alignment for objects drawn with this Pen. Brush Gets or sets the Brush that determines attributes of this Pen. Color Gets or sets the color of this Pen. Width Gets or sets the width of this Pen. Unit: 7 – Mastering Windows Forms 30 Darshan Institute of Engineering & Technology

Brush Class § The Brush class is used to fill the interiors of graphical

Brush Class § The Brush class is used to fill the interiors of graphical shapes, such as rectangles, ellipse, polygons, and paths. § You can draw the outline of the shape using the Pen class and fill the shape using the Brush class. § The different types of brushes available in Windows Forms are Solid. Brush, Hatch. Brush, Texture. Brush, Linear. Gradient. Brush, and Path. Gradient. Brush. § Example § Brush brsh = new Solid. Brush(Color. Red), 40, 140); Unit: 7 – Mastering Windows Forms 31 Darshan Institute of Engineering & Technology

Different Types of Brush Type Description Solid. Brush Fills the shape with one solid

Different Types of Brush Type Description Solid. Brush Fills the shape with one solid color. Hatch. Brush Fills the shape with a hatch style. Texture. Brush Fills the shape with a pattern stored in a bitmap. Linear. Gradient. Brush Fills the shape with color that changes gradually as you move across the shape. Path. Gradient. Brush Fills the shape with color which changes as you move from the center of a shape towards the edge. Unit: 7 – Mastering Windows Forms 32 Darshan Institute of Engineering & Technology

Font Class § The Font class defines a particular format for text such as

Font Class § The Font class defines a particular format for text such as font type, size, and style attributes. § You use font constructor to create a font. § Example • public Font(Font, Font. Style); o Font font = new Font("Times New Roman", 26); Unit: 7 – Mastering Windows Forms 33 Darshan Institute of Engineering & Technology

Properties of Font Class Property Description Bold Gets a value indicating whether this Font

Properties of Font Class Property Description Bold Gets a value indicating whether this Font is bold. Italic Gets a value indicating whether this Font is Italic. Underline Gets a value indicating whether this Font is underlined. Name Gets the face name of this Font. Size Gets the size of this Font. Strikeout Gets a value indicating whether this Font is strikeout (has a line through it). Unit: 7 – Mastering Windows Forms 34 Darshan Institute of Engineering & Technology

Color Structure § A Color structure represents an ARGB color. § Here are ARGB

Color Structure § A Color structure represents an ARGB color. § Here are ARGB properties of it: Property Description A Gets the alpha component value for this Color. B Gets the blue component value for this Color. G Gets the green component value for this Color. R Gets the red component value for this Color. § You can call the Color members. Each color name (say Blue) is a member of the Color structure. § Example • Color. Blue • Color. Red Unit: 7 – Mastering Windows Forms 35 Darshan Institute of Engineering & Technology

Example – GDI+ using System. Drawing; using System. Windows. Forms; private void frm. Parent.

Example – GDI+ using System. Drawing; using System. Windows. Forms; private void frm. Parent. Form_Paint(object sender, Paint. Event. Args e) { Graphics g = e. Graphics; namespace Base. Form g. Draw. Line(Pens. Blue, 10, 200, 100); { public partial class frm. Parent. Form : Form g. Draw. Ellipse(Pens. Red, 20, 150, 80); g. Fill. Ellipse(Brushes. Black, 20, 150, 80); { public frm. Parent. Form() g. Draw. Rectangle(Pens. Green, 30, 100, 120, 60); { Initialize. Component(); g. Fill. Rectangle(Brushes. Magenta, 30, 100, 120, 60); } } Unit: 7 – Mastering Windows Forms 36 Darshan Institute of Engineering & Technology

Output – GDI+ Unit: 7 – Mastering Windows Forms 37 Darshan Institute of Engineering

Output – GDI+ Unit: 7 – Mastering Windows Forms 37 Darshan Institute of Engineering & Technology

Creating Windows Forms Controls § A user control is nothing, but user defined control.

Creating Windows Forms Controls § A user control is nothing, but user defined control. § That means you can create our own control, and you can use it in any form, wherever required. § The user control may contain some user interface with controls like buttons, textboxes etc. § So finally, you need to design the user interface only once in the "User Control", and you can use it manytimes, in any form. § Advantage : Avoids repetition of design and code. Unit: 7 – Mastering Windows Forms 38 Darshan Institute of Engineering & Technology

Creating Windows Forms Controls Cont. . § Create User Control • Open Visual studio,

Creating Windows Forms Controls Cont. . § Create User Control • Open Visual studio, go to File New Project. • Click on "Project" menu "Add User Control". • Enter the name of the new user control. • Click on "Add". § Design and Develop the User Control • Design the UI in the user control, by dragging the controls from the toolbox. • Write the code in "Code window". (User control also supports event handlers similar to forms). Unit: 7 – Mastering Windows Forms 39 Darshan Institute of Engineering & Technology

Creating Windows Forms Controls Cont. . § Invoke the User Control • Open the

Creating Windows Forms Controls Cont. . § Invoke the User Control • Open the required form and build the project. • Then the user control name will be displayed in the toolbox automatically. • To invoke the user control on a form, just drag it from the toolbox into the form designer. Unit: 7 – Mastering Windows Forms 40 Darshan Institute of Engineering & Technology

Creating Windows Forms Controls Cont. . § Open Visual Studio File New Project §

Creating Windows Forms Controls Cont. . § Open Visual Studio File New Project § After Selecting Windows Forms Application, Add User Control. Unit: 7 – Mastering Windows Forms 41 Darshan Institute of Engineering & Technology

Creating Windows Forms Controls Cont. . § Give User Control Name and Press Add.

Creating Windows Forms Controls Cont. . § Give User Control Name and Press Add. Unit: 7 – Mastering Windows Forms 42 Darshan Institute of Engineering & Technology

Creating Windows Forms Controls Cont. . § Now below screen appears & you can

Creating Windows Forms Controls Cont. . § Now below screen appears & you can put any control from toolbox in user control area. § After writing code for user control, build Application. § Now you can use this control in any windows form. Unit: 7 – Mastering Windows Forms 43 Darshan Institute of Engineering & Technology

Creating Windows Forms Controls Cont. . § Constructor public User. Control 1() { Initialize.

Creating Windows Forms Controls Cont. . § Constructor public User. Control 1() { Initialize. Component(); this. Load += new Event. Handler(User. Control 1_Load); this. cmb. City. Selected. Index. Changed += new Event. Handler(cmb. City_Selected. Index. Changed); } § User Control Load Event void User. Control 1_Load(object sender, Event. Args e) { Bind. Combo. Box(); } Unit: 7 – Mastering Windows Forms 44 Darshan Institute of Engineering & Technology

Creating Windows Forms Controls Cont. . § Properties public string Selected. Text { get

Creating Windows Forms Controls Cont. . § Properties public string Selected. Text { get { return cmb. City. Text; } } public string Selectedvalue { get { return cmb. City. Selected. Value. To. String(); } } Unit: 7 – Mastering Windows Forms 45 Darshan Institute of Engineering & Technology

User Control Cont. . § Function : Bind. Combo. Box() private void Bind. Combo.

User Control Cont. . § Function : Bind. Combo. Box() private void Bind. Combo. Box() { Data. Table dt. City = new System. Data. Table(); dt. City. Columns. Add("txt. Part"); dt. City. Columns. Add("value. Part"); dt. City. Rows. Add("Rajkot", "1"); dt. City. Rows. Add("Ahmedabad", "2"); dt. City. Rows. Add("Patna", "3"); dt. City. Rows. Add("Siddhpur", "4"); cmb. City. Data. Source = dt. City; cmb. City. Display. Member = "txt. Part"; cmb. City. Value. Member = "Value. Part"; } Unit: 7 – Mastering Windows Forms 46 Darshan Institute of Engineering & Technology

Output – User Control Unit: 7 – Mastering Windows Forms 47 Darshan Institute of

Output – User Control Unit: 7 – Mastering Windows Forms 47 Darshan Institute of Engineering & Technology

Output - User Control Cont. . § Now add new windows form to your

Output - User Control Cont. . § Now add new windows form to your application & check in toolbox, user control is there. § You can simply drag and drop it to your windows form. Unit: 7 – Mastering Windows Forms 48 Darshan Institute of Engineering & Technology