1 / 40

Chapter 8 Security, Menus, and Files

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

lynton
Download Presentation

Chapter 8 Security, Menus, and Files

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 8Security, Menus, and Files

  2. 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

  3. 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

  4. 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 'Tryagain and display Msgbox box with ! MsgBox "Press Ok and try again", vbExclamation, _ "Incorrect Password" txtPassword.text = “”’clear password text box txtPassword.SetFocus ’setfocus to password text box End If End If End Sub

  5. 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)

  6. 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

  7. Vintage Video Plan

  8. Plan for Vintage Videos

  9. 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

  10. 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 submenuoption. 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

  11. 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

  12. 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

  13. 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.

  14. 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 = ScaleWidth End Sub

  15. 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)

  16. 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”

  17. 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)

  18. VB Code Box 8-3Code to Set up Filter property dlgMemo.Filter = "All Files (*.*)|*.*|Text Files(*.txt)|*.txt" dlgMemo.FilterIndex = 2 dlgMemo.ShowSave

  19. 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.FileName Open strTheFileName for Output as #intFileNum • Note:#intFilNum was previously declared as an Integer variable and represents the next available file number

  20. 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

  21. 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

  22. VB Code Box 8-4Code for Save As Event Procedure Private Sub mnuFileSaveAs_Click() Dim intFileNum As Integer ’variablefor 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 Sub End Sub

  23. 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

  24. 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 Sub ErrHandler: 'User clicked Cancel on dialog box Exit Sub End Sub

  25. 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

  26. VB Code Box 8-6Code to Write Text to a File Open strTheFileName for Input as intFileNum Print #intFileNum, txtMemo.Text Close #intFileNum blnSaved = 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.

  27. VB Code Box 8-7Revised Code for Save Option Private Sub mnuFileSave_Click() Dim intFileNum As Integer On Error GoTo ErrHandler 'Do if user clicked cancel intFileNum = FreeFile 'Use next available file number If 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 If Exit Sub ErrHandler: 'User clicked Cancel on dialog box Exit Sub End Sub

  28. 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

  29. VB Code Box 8-8Code for Close Submenu Option Private Sub mnuFileClose_Click() Dim intYesNo As Integer If Not blnSaved Then ’File not previously saved intYesNo = MsgBox("File not saved! Save it?", _ vbYesNo, "Save File?") ’Query about saving file If intYesNo = VBYes Then 'User wants to save file Call mnuFileSave_Click ’call click event End If End If txtMemo = "" 'Clear text box TheFileName = "" 'Clear file name End Sub

  30. 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 form End Sub

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

  32. 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

  33. Using the Clipboard

  34. 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

  35. Cut, Copy, and Paste Text • Code to Cut Text Clipboard.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 text Clipboard.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()

  36. 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=False mnuEditCut.Enabled=False mnuEditClear.Enabled=False mnuEditPaste.Enabled=False End Sub

  37. 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

  38. VB Code Box 8-12Code to Uncheck All Fonts Dim intCounter As Integer For intCounter = 0 To 6 mnuFormatFontName(counter).Checked = False Next ________________________________________ Above code is typed in prior to the Select Case

  39. VB Code Box 8-11Code to Select Font for the Text Box Select Case Index Case 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 Select mnuFormatFontName(Index).Checked = True

  40. VB Code Box 8-13Code to Select a Style for the Text Private Sub mnuFormatStyleType_Click(Index As Integer) Select Case Index Case 0 txtMemo.FontBold = False txtMemo.FontItalic = False txtMemo.FontUnderline = False Case 1 txtMemo.FontBold = True txtMemo.FontItalic = False txtMemo.FontUnderline = False Case 2 txtMemo.FontBold = False txtMemo.FontItalic = True txtMemo.FontUnderline = False Case 3 txtMemo.FontBold = False txtMemo.FontItalic = False txtMemo.FontUnderline = True Case 4 txtMemo.FontBold = True txtMemo.FontItalic = True txtMemo.FontUnderline = False End Select End Sub

More Related