chapter 8 security, menus, and files

40
Chapter 8 Security, Menus, and Files

Upload: lynton

Post on 21-Jan-2016

46 views

Category:

Documents


0 download

DESCRIPTION

Chapter 8 Security, Menus, and Files. Security. Passwords are often used to control access to a computer or software program Passwords should be at least 6 characters in length and something that cannot be easily guessed The Password Form should be the Startup object - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 8 Security, Menus, and Files

Chapter 8Security, Menus, and Files

Page 2: Chapter 8 Security, Menus, and Files

Security• Passwords are often used to control access to a computer

or software program

• Passwords should be at least 6 characters in length and something that cannot be easily guessed

• The Password Form should be the Startup object

• It should not be possible to move, resize, or bypass the password form in VB so the Border Style should be set to Fixed Single and the Control Box property should be set to False (removes control boxes on form)

• The Tag property for a textbox can be used for the password and the PasswordChar property, such as * can be used to disguise the password as it is entered. It stores any extra data needed for the program along with a VB control

Page 3: Chapter 8 Security, Menus, and Files

Security Command Buttons

• A user should be given a set number of “chances” to enter the correct password.

• Two command buttons will be on form, Accept and Cancel

• Setting the Accept buttons Default property to True will execute the click event if the Enter key is pressed

• Setting the Cancel buttons Cancel property to True will activate the click event if the Escape key is depressed

Page 4: Chapter 8 Security, Menus, and Files

VB Code Box 8-1Code for Accept Button

Private Sub cmdAccept_Click() Static intNumTries as Integer 'saves # of attempts If UCase(txtPassword.text) = UCase(txtPassword.Tag) Then frmVintage.Show 'Go to main form Unload Me 'hide current form and remove from memory Else 'No match found intNumTries = intNumTries + 1 'Increment number of attempts If intNumTries >= 3 Then 'Too many attempts display MsgBox MsgBox "Too many attempts", vbCritical, "Access Denied" End Else 'Try again and display Msgbox box with ! MsgBox "Press Ok and try again", vbExclamation, _ "Incorrect Password" txtPassword.text = “”’clear password text box txtPassword.SetFocus ’set focus to password text box End If End IfEnd Sub

Page 5: Chapter 8 Security, Menus, and Files

Menu Systems in VB• A menu system for a form can be created using the Menu

Editor. Access by going to the Tools menu and select the Menu Editor option (Figure 8-4)

• Menu bar options as well as submenus can be created with this editor. It allows for the creation of access keys, and shortcut keys also

• Frequently used items should remain as buttons while less frequently used items could be menu items

• Menu and submenu items have caption and name properties (the prefix is mnu)

Page 6: Chapter 8 Security, Menus, and Files

Menu Systems in VB

• Code is written for menu items just like for other event procedures

• Menu options are always click events

• Most menus contain File and Help menu bar options in addition others specific to the application

Page 7: Chapter 8 Security, Menus, and Files

Vintage Video Plan

Menu Bar Item Submenu Item

File Exit

Members/Videos Check MemberFind Videos

Memos Create Memo

Help About

Page 8: Chapter 8 Security, Menus, and Files

Plan for Vintage Videos

Password form (Startup Form)

frmVintage (menusystem)

VideoSearchform

Memoform

LegendRental processMenu selections

MemberSearchform

Aboutform

Page 9: Chapter 8 Security, Menus, and Files

Using the Menu Editor

• Enter a caption for the first menu such as a File and give it a name such as mnuFile. The name should be descriptive and contain the menu or submenu name.

• Determine if the menu item should be enabled and visible by clicking the appropriate check box on the menu editor

• To include an item under a menu, such as Exit under the File menu, click the next button and the right arrow button and repeat previous two steps.

• Note that menus are flush with the left margin and submenus are indented and preceded by four dots

Page 10: Chapter 8 Security, Menus, and Files

Using the Menu Editor• To create a second Menu bar item press the next button and

the left arrow to outdent back to menu bar level and follow procedures on previous slide

• Access keys and shortcut keys can also be created– Access key -a key or a a key that combined with the Alt

key that can be used to access a menu or submenu option. Example: ALT F can access File menu. Place an & before the letter you want to be the access key. Two menu items can’t have the the same shortcut key

– Shortcut key - special set of keys that which can directly access a submenu option. Often function keys or the Control or Shift key combined with a letter/function key are used

Page 11: Chapter 8 Security, Menus, and Files

Adding Code to Menu Items

• There is no code for menu items, only submenu items

• Single click, instead of double clicking on the menu item to bring up the code window

• If code is totally new, type it in as usual

• If code exists in a previously created event, Cut and Paste the code into the menu click event

Page 12: Chapter 8 Security, Menus, and Files

Add an About Screen

• The About screen is used to provide information about the application and is under the Help menu item.

• Using Project/Add Form create and save the form as frmAbout

• The About Screen should have a command button (cmdOK) and should have following line of code to unload the form

Unload.Me

• Under the menu item mnuHelpAbout add this code:

frmAbout.show

Page 13: Chapter 8 Security, Menus, and Files

Creating a Memo Editor

• Creating a memo editor in VB involves creating another form (frmMemo) and using a textbox (txtMemo) as the location of the text and creating a menu system for the various editor commands

• The menu system will do such things as file operations, editing operations, and formatting options

• The textbox should have its Multiline property set to True This property allows any text entered in the text box to wrap when it reaches the boundary of the text box

• The Scrollbars property should be set to Vertical and its Top and Left Properties set to 0 to fill form.

Page 14: Chapter 8 Security, Menus, and Files

VB Code Box 8-2Form_Resize Event Procedure to

Resize Text Box

The Form_Resize event occurs when the user displays a textbox-- it can be used to cause the memo text box to fill its form by setting the Height and Width properties to ScaleHeight and ScaleWidth

Private Sub Form_Resize() txtMemo.Height = ScaleHeight txtMemo.Width = ScaleWidthEnd Sub

Page 15: Chapter 8 Security, Menus, and Files

The Memo Editor File Menu• The File Menu should have options to begin a new memo, open an

existing memo, save a memo under the current name or a new one, close the memo, print it, or exit the memo editor.

• Key to the File Menu is the use of the common dialog control which must be added to the Toolbar with the Project|Components VB menu option before being added to the form

• To use the common dialog control to work with files add code to the appropriate submenu item Click event to connect the event to the common dialog control

• The common dialog control (with dlg prefix) can be used to open or save files with the ShowOpen and ShowSave methods

• The CancelError Property should be set to True so it can take advantage of the On Error GoTo statement (discussed later)

Page 16: Chapter 8 Security, Menus, and Files

Saving a Memo• The Filter property of the common dialog control is used to

display a list of files

• It has the form:

dlgName.Filter = “description1|filter1|description2|filter2|etc”

where

descriptionN - description of file type you want to save as

filterN -actual filter

|(pipe)-separates descriptions and filters

• .For example to display all types of files and text files for common dialog control

• dlgMemo.Filter=“All files(*.*)|*.*|Text Files(*.txt)|*.txt”

Page 17: Chapter 8 Security, Menus, and Files

Saving a Memo (con’t)

• The FilterIndex property determines the default file type of the saved memo

• It uses the following syntax:

dlgName.FilterIndex=N

where N = the number of the type of file in the Filter property statement

For example:

dlgMemo.FilterIndex=2 (will save as text)

Page 18: Chapter 8 Security, Menus, and Files

VB Code Box 8-3

Code to Set up Filter property

dlgMemo.Filter = "All Files (*.*)|*.*|Text Files(*.txt)|*.txt"dlgMemo.FilterIndex = 2dlgMemo.ShowSave

Page 19: Chapter 8 Security, Menus, and Files

Saving a Memo (con’t)• When a file is selected or a filename entered in the Save

As dialog box (Figure 8-15), this becomes the Filename property for the common dialog control

• If the Filename property is assigned to a string variable it can be used in the Open Statement in the Save as procedure For example:

strTheFileName=dlgMemo.FileNameOpen strTheFileName for Output as #intFileNum

• Note:#intFilNum was previously declared as an Integer variable and represents the next available file number

Page 20: Chapter 8 Security, Menus, and Files

Saving a Memo (con’t)• Contents of the the textbox are then written to the file with

the Print statement and the File is then closed. The form level Boolean variable Saved is set to True to determine the file has been saved before closing it. For example:

Print #intFileNum,txtMemo.Text Close #intFileNum

blnSaved=True

• The FreeFile function returns an unused file number and is assigned to the FileNum variable in the Vintage problem

Page 21: Chapter 8 Security, Menus, and Files

Saving a Memo (con’t)

• The On Error Goto statement causes control to jump to a label when an error is encountered. In the Vintage problem the label is ErrHandler:

• Will be used to check to see if the user has clicked the Cacel button on the dialog box.

• The Exit Sub statement before Errhandler:label is important because it avoids executing the Errhandler as a normal step in the code

Page 22: Chapter 8 Security, Menus, and Files

VB Code Box 8-4Code for Save As Event Procedure

Private Sub mnuFileSaveAs_Click() Dim intFileNum As Integer ’variable for file number On Error GoTo ErrHandler 'User clicked Cancel intFileNum = FreeFile 'Use next available file number 'Set up filter and set default to text files dlgMemo.Filter = "All Files(*.*)|*.*|Text Files(*.txt)|*.txt" dlgMemo.FilterIndex = 2 ’text file dlgMemo.ShowSave 'Display dialog box strTheFileName = dlgMemo.filename 'Select file name Open strTheFileName For Output As #intFileNum Print #intFileNum, txtMemo.Text 'write textbox contents to file Close #intFileNum ’close the file blnSaved = True ’makes sure the file is closed Exit Sub ErrHandler: Exit SubEnd Sub

Page 23: Chapter 8 Security, Menus, and Files

Opening a File• The code to open a file is very similar to the code for saving a file

• It involves displaying the Open File common dialog box (Figure 8-16), highlighting an existing file, or entering the name to open, and clicking the open button to assign the filename to the Filename property of the dialog control box

• A file can be opened from a common dialog box with the ShowOpen method and the contents of the file are input to the text box.

• The contents of the file can be input using the Input$ function:

Input$(number, #FileNumber) where number = number of characters in file that is identified by #FileNumber

• The LOF(FileNumber) function can be used to determine the number of characters in a file and can be inserted in the Input statement in place of the number argument. For example:

txtMemo=Input$(LOF(intFileNum), #intFileNum

Page 24: Chapter 8 Security, Menus, and Files

VB Code Box 8-5Code to Open a File

Private Sub mnuFileOpen_Click() Dim intFileNum As Integer On Error GoTo ErrHandler 'If cancel clicked, do this intFileNum = FreeFile 'Find next available free file dlgMemo.Filter = "All Files (*.*)|*.*|Text Files (*.txt)|*.txt" 'Set up filter and set default to text files dlgMemo.FilterIndex = 2 dlgMemo.ShowOpen 'Display dialog box strTheFileName = dlgMemo.filename 'Select file name Open strTheFileName For Input As #intFileNum txtMemo.Text = Input$(LOF(intFileNum), #intFileNum) 'Read all text in file Close #intFileNum blnSaved = True Exit SubErrHandler: 'User clicked Cancel on dialog box Exit SubEnd Sub

Page 25: Chapter 8 Security, Menus, and Files

Saving a File (File/Save Menu)

• Used to save a file that was previously saved using Save As

• Code is similar as the code for the Save As dialog box,except there is no need to display the Save As dialog box

• Should write code to ensure that the file name is stored in a variable

• You can also CALL another click event as opposed to having to recode it

• A memo can be saved by printing its contents to a file--its common to query the user to save the file if it has been changed. Use the following line of code to Print to a printer:

Printer.Print txtMemo.txt

Page 26: Chapter 8 Security, Menus, and Files

VB Code Box 8-6Code to Write Text to a File

Open strTheFileName for Input as intFileNumPrint #intFileNum, txtMemo.TextClose #intFileNumblnSaved = True_______________________________________Above code would be OK , unless the the user tries to save a file without using Save As first. Must check to see that the variable strTheFileName has something stored in it. Use code on following slide.

Page 27: Chapter 8 Security, Menus, and Files

VB Code Box 8-7Revised Code for Save Option

Private Sub mnuFileSave_Click()Dim intFileNum As IntegerOn Error GoTo ErrHandler 'Do if user clicked cancelintFileNum = FreeFile 'Use next available file numberIf strTheFileName <> "" Then

Open strTheFileName For Output As #FileNum 'File name exists Print #intFileNum, txtMemo.Text 'Print contents to file Close #intFileNum blnSaved = True 'This file has been saved

Else 'No file name; call Save As operation Call mnuFileSaveAs_Click 'call previously coded click event

End IfExit SubErrHandler: 'User clicked Cancel on dialog boxExit Sub

End Sub

Page 28: Chapter 8 Security, Menus, and Files

Closing and Exiting a File

• Involves clearing the txtMemo textbox after determining it whether or not the text has been saved

• It uses the Boolean variable: blnSaved. If blnSaved is True, then the text has been saved and the mnuFileSave click event is called

• If blnSaved is False, then the text has not been saved and the user should be queried as to whether to save the file.

• The code on the next slide works, unless the user has changed the text and forgot to save it. It is necessary to create a txtMemo_Change Event and add the following instruction to it

blnSaved=False

• See VB Code Box 8-9 for exiting a file

Page 29: Chapter 8 Security, Menus, and Files

VB Code Box 8-8Code for Close Submenu Option

Private Sub mnuFileClose_Click()Dim intYesNo As IntegerIf Not blnSaved Then ’File not previously saved

intYesNo = MsgBox("File not saved! Save it?", _vbYesNo, "Save File?") ’Query about saving fileIf intYesNo = VBYes Then 'User wants to save file

Call mnuFileSave_Click ’call click eventEnd If

End IftxtMemo = "" 'Clear text boxTheFileName = "" 'Clear file name

End Sub

Page 30: Chapter 8 Security, Menus, and Files

VB Code Box 8-9 Code for Exit Option

Private Sub mnuFileExit_Click() Call mnuFileClose_Click 'call click event Unload frmMemo ' remove form from memory frmVintage.Show 'display main formEnd Sub

Page 31: Chapter 8 Security, Menus, and Files

Slides Beyond this point are optional. If time allows you may complete Chapter 8

Page 32: Chapter 8 Security, Menus, and Files

The Memo Editing Submenu• Typical editing options are Cut, Copy, and Paste

• All three use the Clipboard object to temporarily save information

• The Clipboard object has no properties, but has three important methods

• The Clipboard SetText method transfers selected text to the clipboard (cuts) and the GetText method retrieves (pastes) text from clipboard

• The SelText property of the textbox stores what is in the clipboard

• The Clipboard Clear method clears the Clipboard

Page 33: Chapter 8 Security, Menus, and Files

Using the Clipboard

Page 34: Chapter 8 Security, Menus, and Files

The Enabled Property

• When the Memo form is loaded, all editing options should be inactive (disabled) because nothing is in the editing box.

• All editing options should also be disabled whenever the text box is cleared.

• When there is a change in the text box, the Cut, Copy, and Clear All options should be enabled.

• The Paste option should be disabled until the Cut or Copy option has been used

• Enabled=True means menu option is visible• Enabled=False means menu option is grayed out

Page 35: Chapter 8 Security, Menus, and Files

Cut, Copy, and Paste Text

• Code to Cut TextClipboard.Clear 'clear clipboard

Clipboard.SetText txtMemo.Seltext 'store highlighted text in the text box

txtMemo.SelText= “” 'remove selected text from textbox

mnuEditPaste.Enabled=True ‘enables Paste option on Menu

• Code to Copy textClipboard.Clear 'clear clipboard

Clipboard.SetText txtMemo.Seltext 'store highlighted text in the text box

mnuEditPaste.Enabled=True 'enables Paste option on Menu

• Code to Paste Text

txtMemo.Seltext=Clipboard.GetText()

Page 36: Chapter 8 Security, Menus, and Files

VB Code Box 8-10Disable the Edit Menu

• The following code should be called from the frmMemo_Formload, mnuFileNew, mnuFileClose, and mnuEditClear procedures

Sub Edit Disable()mnuEditCopy.Enabled=FalsemnuEditCut.Enabled=FalsemnuEditClear.Enabled=FalsemnuEditPaste.Enabled=False

End Sub

Page 37: Chapter 8 Security, Menus, and Files

The Memo Format Submenu• Formatting of text in the memo involves changing the Font

and Style of the text

• Menu control arrays are used to display different types of fonts and styles

• Fonts or styles are distinguished from each other by the index value which is entered in the Menu Editor

• The FontName property of the memo textbox determines the font type

• FontBold, FontItalics, FontUnderline properties of the textbox control the style of the text. Note:only one style and format can be in each memo

• Use the Select Case method to code the menus

Page 38: Chapter 8 Security, Menus, and Files

VB Code Box 8-12Code to Uncheck All Fonts

Dim intCounter As IntegerFor intCounter = 0 To 6 mnuFormatFontName(counter).Checked = FalseNext________________________________________Above code is typed in prior to the Select Case

Page 39: Chapter 8 Security, Menus, and Files

VB Code Box 8-11Code to Select Font for the Text BoxSelect Case IndexCase 0 txtMemo.FontName = "Arial"Case 1 txtMemo.FontName = "Courier New"Case 2 txtMemo.FontName = "Modern"Case 3 txtMemo.FontName = "MS Sans Serif"Case 4 txtMemo.FontName = "MS Serif"Case 5 txtMemo.FontName = "Times New Roman"Case 6 txtMemo.FontName = "WingDings"End SelectmnuFormatFontName(Index).Checked = True

Page 40: Chapter 8 Security, Menus, and Files

VB Code Box 8-13Code to Select a Style for the Text

Private Sub mnuFormatStyleType_Click(Index As Integer)Select Case IndexCase 0 txtMemo.FontBold = False txtMemo.FontItalic = False txtMemo.FontUnderline = FalseCase 1 txtMemo.FontBold = True txtMemo.FontItalic = False txtMemo.FontUnderline = FalseCase 2 txtMemo.FontBold = False txtMemo.FontItalic = True txtMemo.FontUnderline = FalseCase 3 txtMemo.FontBold = False txtMemo.FontItalic = False txtMemo.FontUnderline = TrueCase 4 txtMemo.FontBold = True txtMemo.FontItalic = True txtMemo.FontUnderline = FalseEnd SelectEnd Sub