10-1 chapter 10 security, menus, files, and graphics

51
10-1 Chapter 10 Security, Menus, Files, and Graphics

Upload: amberlynn-powell

Post on 04-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-1

Chapter 10

Security, Menus, Files, and Graphics

Page 2: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-2

Learning Objectives

Require a password to protect the information system from unauthorized access.

Create a menu for a project using the MainMenu control and add code to the menu items to execute actions when an item is selected.

Work with direct access data files

Page 3: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-3

Learning Objectives (continued)

Add and use the SaveFileDialog and OpenFileDialog controls to save and open files.

Use graphics objects in your projects to draw figures on a form.

Use graphics objects to add print and print preview capabilities to a project.

Add the MS Chart control to the Toolbox and then to a form to use in charting data.

Page 4: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-4

Using Passwords for Security (1/2)

Add a password form to project– Add a dialog box form

Set the FormBorderStyle property to FixedSingle Set the ControlBox property to False

– Make that form the startup object– Use the password TextBox Tag property to set the

password at design time– Set the PasswordChar property of the TextBox so that the

password does not get displayed as it is typed in Tpically use * as the PasswordChar

Page 5: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-5

Using Passwords for Security (2/2)

Set the AcceptButton property of the form Set the CancelButton property of the form Add code to the form:

– If AcceptButton is pushed Test the password entered against value of Tag If the same, hide dialog box and display application Otherwise display some message box to try again

– If CancelButton is pushed Quit

Page 6: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-6

Password Form

Page 7: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-7

Step-by-step 10-1: Adding Password Security

Demo

Page 8: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-8

Menus and design

Examine menus of known application Try to maintain a Common User Interface

– Use a menu hierarchy the user is used to– Arrows indicate that a submenu will be displayed– Ellipsis (…) indicate that a dialog box will appear– Use shortcut keys so that the menu can be used

directly from the keyboard

Page 9: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-9

More menu design

Keep main menus similar to the menus that we see in other Windows programs

Group together related commands under a main menu heading that reflects the overall relationship of those commands

Use unique menus when necessary but try to keep their use to a minimum.

Use common elements to give the user an idea of what will happen when a menu option is selected

Use common elements to show the user what alternatives exist for accessing the command on toolbars or from the keyboard.

Page 10: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-10

The MainMenu control

Drag a MainMenu control to your project

It gets displayed in the component tray

Type the menu captions directly on the form

Page 11: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-11

Menu creation

To add a separator line– Type a dash (-) as the

menu text– Right click and choose

New Separator

To add a submenu– Type a caption to the

right of the menu

Page 12: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-12

Menu item properties

Page 13: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-13

Adding code to menu item

Double click the menu item An editor window appears with the right

event stub Type in the code corresponding to the

selection of that menu item

Page 14: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-14

Converting event procedure from button click to menu click

1. Open the Code window by double-clicking the button for which the code is to be transferred.

2. Use the mouse to highlight the code for that event procedure [do not include the first statement (starting with “Sub”) and the last (End Sub) statements].

3. Cut the code using the Edit|Cut menu option (use Ctrl-x as a shortcut for this operation) and delete the first and last statements of the empty event procedure.

4. Open the Code window for the corresponding submenu item Click event by clicking it once or by just changing to the menu item in the Code window.

5. Paste the code into the submenu item Click event procedure using the Edit|Paste menu option (use Ctrl-v as a shortcut for this operation).

6. Delete the button control from the form.

Page 15: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-15

Step-by-step 10-2: Adding Menus to a VB .NET Project

Demo

Page 16: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-16

Using Direct Access Files

Direct access file: Data file in which data are assumed to be stored as fixed length records

Similar to sequential access files– Can be read and written directly from VB .Net

Similar to database files– Data are stored as records– Data can be accessed directly

Page 17: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-17

Depiction of direct access file

Page 18: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-18

How to access data

The key parameter: the record number– Identifies the record uniquely

The record representation– Use a named Structure with elements of the right

types– Need to be able to declare “fixed-length” strings

<VBFixedString(n)> Public Var i a bl e Name As String n is the length of the string

Page 19: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-19

Structure example

Structure EmpRecord <VBFixedString(15)> Public strFName As String

<VBFixedString(20)> Public strLName As String

<VBFixedString(11)> Public strSSNum As String

<VBFixedString(12)> Public strPhone As String

<VBFixedString(10)> Public strPaytype As String

Public decPayRate As Decimal

End Structure

Page 20: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-20

Opening a Direct Access File:FileOpen parameters

Page 21: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-21

File Access Commands

FilePut– FilePut(file number, variable name, record

number)

FileGet– FileGet(file number, variable name, record

number)

FileClose

Page 22: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-22

Writing Data to Direct Access File

intFileNum = FreeFile()FileOpen(intFileNum, strFileName, _

OpenMode.Random, _OpenAccess.ReadWrite, _ OpenShare.Shared, Len(udtEmp))

‘For-Next loop to write records to fileFor intCurrent = 0 To intEmpCntr - 1

FilePut(intFileNum, udtEmployees(intCurrent), intCurrent + 1)Next

FileClose(intFileNum)

Page 23: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-23

Reading Data from Direct Access File

intFileNum = FreeFile()

FileOpen(intFileNum, _ strFileName, _ OpenMode.Random, _ OpenAccess.ReadWrite, _ OpenShare.Shared, Len(udtEmp))

‘For-Next loop to read records from fileFor intCurrent = 0 To intEmpCntr - 1

FileGet(intFileNum, udtEmployees(intCurrent), intCurrent + 1)Next FileClose(intFileNum)

Page 24: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-24

Step-by-Step 10-4: Direct Access Files

Demo

Page 25: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-25

Using File Dialog Boxes

Two controls with obvious purposes– OpenFileDialog– SaveFileDialog

Common method and properties– ShowDialog(): displays the dialog box– Filter: gets or sets the file name filter string

OSD.Filter=“All Files (*.*)|*.*|Text Files (*.txt)|*.txt”

– FilterIndex=gets or sets the index of current filter OSD.FilterIndex=1 ‘Text Files (*.txt) is current filter

Page 26: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-26

More on File Dialog Boxes

You do not need to instantiate the objects Simply drag the control on the project You may want to change the name The most important property

– FileName: gets or sets the file name selected

Page 27: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-27

Typical use of File Dialog Box

Set the filter and the filter index Call ShowDialog() method to display the dialog box Interact with dialog box to select of type in file name Dismiss the dialog by clicking the Open or Save or

Cancel buttton Read the FileName property if anything but the

Cancel button was selected Take action corresponding to the file name chosen

Page 28: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-28

Checking for Errors:Using the FileInfo object

FileInfo object is part of the Syste.IO namespace

Typical use:– Import System.IO– Dim objFI As New FileInfo(strFileName)– If objFI.Exists Then ’check for existence of file– ……

Page 29: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-29

More on FileInfo object

CopyTo: Allows to copy to another file name Create: creates a file Delete: permanently delete a file Directory: gets an instance of the parent directory Extension: the extension part of the file name FullName: full path to the file or directory Length: the size of the file Name: the name of the file

Page 30: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-30

Step-by-step 10-4: Using File Dialog Boxes

Demo

Page 31: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-31

Working with Graphics

To draw graphics on forms, use GDI+– Graphics Design Interface +

Most tools are in the System.Drawing namespace Steps to create graphics on a control (form or other)

in VB .Net– Create a graphics object– Use the graphics object to draw

Lines Shapes Text Images

Page 32: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-32

Main Graphics Objects

Pen– Used to draw lines or

curves

Properties– Alignment– Color– PenType– Width

Brush– Used to fill the interior of

shapes

Types of Brushes– SolidBrush– TextureBrush– Drawing 2D namespace

HatchBrush LinearGradientBrush PathGradientBrush

Page 33: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-33

.Net Graphics Coordinate System:Coordinates are in pixels

Page 34: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-34

The Panel Control

Can be used to group items on a form Can be used to draw on it Call the CreateGraphics method

– Returns a graphics object on which to draw Other controls with CreateGraphics method

– Form– PictureBox– Button

Page 35: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-35

Steps to draw on an object

Obtain a Graphics object (call CreateGraphics method) If necessary

– Create a Pen object– Create a Brush object

Call methods of the Graphics object and pass the Pen or the Brush as parameter as needed

– DrawRectangle– Clear– FillEllipse– DrawPolygon– FillPolygon

Page 36: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-36

Step-by-step 10-4: Experimenting with Graphics

Demo

Page 37: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-37

More methods of the Graphics Class

Page 38: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-38

Printing

Use the PrintDocument object– Available from the System.Drawing namespace– Imports System.Drawing.Printing

PrintDocument can raise PrintPage event– Raised whenever the Print method is called– Write code to format the printed document in the

PrintPage event procedure

Page 39: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-39

Steps to use PrintDocument

1. Create the item that we want to print.

2. Set the location on the document where it should be printed.

3. Select style, font, color, etc. of the item that you want to print.

4. Use the appropriate method to draw the item on the PrintDocument object.

Page 40: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-40

Code to Specify the Format of the Print Document (1/3)

Private Sub objPrintDocument_Print(ByVal sender As Object, _ ByVal e As PrintPageEventArgs)

Dim sngX As Single Dim sngY As Single

Dim sngLeftMargin As Single = e.MarginBounds.Left Dim sngRightMargin As Single = e.MarginBounds.Right Dim sngTopMargin As Single = e.MarginBounds.Top

Dim objTitleFont As New Font("Courier New", 14, FontStyle.Bold) Dim objHeadingFont As New Font("Courier New", 12, FontStyle.Bold) Dim objRecordFont As New Font("Courier New", 12)

Dim objBrush As SolidBrush = New SolidBrush(Color.Black) Dim objpen As Pen = New Pen(Color.Black) Dim strLine, strName As String, intCounter As Integer strLine = "Employees"

Page 41: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-41

Code to Specify the Format of the Print Document (2/3)

sngX = sngLeftMargin + 10 sngY = sngTopMargin + 5

e.Graphics.DrawString(strLine, objTitleFont, objBrush, sngX, sngY)

strLine = "Name" & Space(26) ' write column headings strLine = strLine & "Phone" & Space(10) strLine = strLine & "Pay Type" sngY = sngY + 34

e.Graphics.DrawString( strLine, objHeadingFont, objBrush, sngX, sngY)

sngY = sngY + 24

e.Graphics.DrawLine(objpen, sngX, sngY, sngRightMargin - 10, sngY)

Page 42: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-42

Code to Specify the Format of the Print Document (3/3)

For intCounter = 0 To intEmpCntr - 1 ' print records strName = Trim(udtEmployees(intCounter).strFName) & " “ & _

Trim(udtEmployees(intCounter).strLName)

strLine = Spacer(strName, 30) strLine = strLine & Spacer(Trim(udtEmployees(intCounter).strPhone), 15) &

Chr(9) strLine = strLine & udtEmployees(intCounter).strPaytype & Chr(9) sngY = sngY + 24

e.Graphics.DrawString(strLine, objRecordFont, objBrush, sngX, sngY) Next

intCounter sngY = sngY + 36 ’ print total employees strLine = "Number of Employees = " & CStr(intEmpCntr)

e.Graphics.DrawString(strLine, objHeadingFont, objBrush, sngX, sngY) End Sub

Page 43: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-43

AddHandler: Dynamic Event Dispatch

You can use a generic procedure (with the right paramters list) to be executed when an event is raised

Use the AddHandler statement– AddHandler ObjectName.Event, AddressOf

ProcedureName Allows to use the same code for

– Printing to the printer– Printing to a PrintPreviewDialog control

Page 44: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-44

Code for Printing the Document

Private Sub mnuFilePrint_Click( ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles

mnuFilePrint.Click

Dim objPrintDocument As New PrintDocument() AddHandler objPrintDocument.PrintPage, AddressOf objPrintDocument_Print

If PrinterSettings.InstalledPrinters.Count = 0 Then MessageBox.Show( "No printers are currently installed", _

"Print Error", _MessageBoxButtons.OK, _ MessageBoxIcon.Information)

Exit Sub End If

objPrintDocument.Print() End Sub

Page 45: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-45

Print Preview

Use the PrintPreviewDialog control Set the UseAntiAlias property to True When print preview is requested

– Set the Document property of the control to the PrintDocument object with the correct PrintPage event

– Cal the ShowDialog to display the preview control

Page 46: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-46

Code for Previewing the Document

Private Sub mnuFilePrintPreview_Click( ByVal sender As System.Object, _ByVal e As System.EventArgs) _Handles _ mnuFilePrintPreview.Click

Dim objPrintDocument As New PrintDocument()

AddHandler objPrintDocument.PrintPage, AddressOf objPrintDocument_Print

If PrinterSettings.InstalledPrinters.Count = 0 Then MessageBox.Show( "No printers are currently installed", "Print Error", _

MessageBoxButtons.OK, MessageBoxIcon.Information)

Exit Sub End If

dlgPreview.document = objPrintDocument dlgPreview.ShowDialog()

End Sub

Page 47: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-47

Step-by-step 10-5: Adding Print and Print Preview

Demo

Page 48: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-48

Adding Chart Control to Toolbox

Make your form active Right-Click the

Windows Forms tab Click Customize

Toolbox Select the COM

component tab Check Microsoft Chart

Control 6.0

Page 49: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-49

Setting Chart Properties

Use the properties window as for any other control

OR Right click on the

control, select “Properties” and use the dialog box that pops up

Page 50: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-50

Step-by-step 10-5: Working with the Chart Control

Demo

Page 51: 10-1 Chapter 10 Security, Menus, Files, and Graphics

10-51

Copyright 2004 John Wiley & Sons, Inc.

All rights reserved. Reproduction or translation of this work beyond that permitted in section 117 of the 1976 United States Copyright Act without express permission of the copyright owner is unlawful. Request for further information should be addressed to the Permissions Department, John Wiley & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages caused by the use of these programs or from the use of the information herein