don kussee -60 1410_appendix1 simple text editor don kussee
Post on 27-Dec-2015
224 Views
Preview:
TRANSCRIPT
Don Kussee -60 1410_appendix 2
TextBox Properties
• MultiLine = T ‘displays more than one line
• Text ‘string containing all the current text
• When there is highlighted text then– SelLength ‘ contains the number of characters– SelStart ‘contains the number of characters
from the start of the TextBox– SelText ‘contains the highlighted text
• If no selected text, then SelText = “”, SelLength = 0
Don Kussee -60 1410_appendix 3
Example 1
15 = Text1.SelLength19 = Text1.SelStartVAX text editor = Text1.SelText
Don Kussee -60 1410_appendix 5
Example 3
32 = Text1.SelLength3 = Text1.SelStart“grew up using a VAX text editor.” = Text1.SelText
Don Kussee -60 1410_appendix 6
To Clear the TextBox
• Text1.Text = “”
• cmdCut.Enable = F
• cmdCopy.Enable = F
Don Kussee -60 1410_appendix 7
To cut text
• Highlight some text and enable Cut & Copy
• Private Sub cmdCut_Click
• Text2.Text = Text1.SelText
• Text1.SelText = “”
• cmdCut.Enable = F
• cmdCopy.Enable = F
• End Sub
Don Kussee -60 1410_appendix 8
Using a State chart
• Define every possible condition for the program in general terms– TextBox empty, Paste buffer empty– Len(TextBox >0) , Paste buffer empty– Len(TextBox>0), Paste Buffer.caption >0– Len(TextBox>0), SelText>0 ….
• Define what objects can do what for that state and write the code for some event
Don Kussee -60 1410_appendix 9
Or Using a State chart
• Define what the objects are, and what their conditions are required to be
• Cut Button – Enabled = T when SelText>0– Enabled = F when SelText =0
• txtEdit_MouseUp ‘the code goes here
• Paste, Copy, Clear, Exit
Don Kussee -60 1410_appendix 11
Building Menus
• A menu for each form
• File Edit View Insert Format Help
• Can contain 4 levels maximum
• First level is displayed
• Other levels cascade down based on choices
• Can create Popup Menus
Don Kussee -60 1410_appendix 13
Menu Properties
• Caption shows up on form
• Name used in code
• Checked allows TF choice
• Enabled & Visible available
• Hotkeys using & letter
• Short cut keys (drop down key choices)
• Arrows to change levels and location
Don Kussee -60 1410_appendix 14
Popup Menu
• Different menus can be designed for each object - context sensitive
• Can be specified in any object_event code segment– Often associated with Mouse_Down
• Menu is defined using Menu design tool
• Can be copy of menu, or can be invisible
• Location and size can be specified
Don Kussee -60 1410_appendix 15
Popup Menus
Private Sub Form_MouseDown (Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then
PopupMenu mnuFile
End If
End Sub
Don Kussee -60 1410_appendix 17
Common Dialog
• Add to tool box– Project|Components
1 Place control on form
• Control is hidden during run mode
2 Set defaults to be used
3 Set Flags to be used
4 Define which Common Dialog to use .Show
Don Kussee -60 1410_appendix 18
Common Dialog cdl
• File Open - ShowOpen
• File Save - ShowSave
• Font - ShowFont
• Color - ShowColor
• Printer - ShowPrinter
• Help - poor implementation. Ok for very small files, use another for big tasks.
Don Kussee -60 1410_appendix 21
Files Common Dialog
• User can specify file and path
• Allows changes of drives, directories using WIN95 system tools
• FileName Default path & file name
• Filter Which types of files
Don Kussee -60 1410_appendix 23
Font Common Dialog
• FontName
• FontSize
• Color
• Flags
• Font Bold, Italic, Underline, Strikethru
Don Kussee -60 1410_appendix 25
Color Common Dialog
• Either system colors using constants vbBlack
• Normal colors can be shown using RGB( ) or QBColor ( )
• Custom Color gives second window
Don Kussee -60 1410_appendix 27
Printer Common Dialog
• Identifies all printers on the system
• Allows you to designate where you will ship the job to (does not ship it, just sets the switches).– Copies– Pages – from to – All – Current only
Don Kussee -60 1410_appendix 29
Files - Background
• Sequential - Records of non-equal length
• Random - Records of equal length
• Text Vs Binary - Binary only beneficial with lots of large numbers
• DataBase data storage - Large overhead, so poor for small amounts of data. Much data not in DataBases format
Don Kussee -60 1410_appendix 30
Text VS Binary
• Text – Each character takes one byte– Numbers translated into characters - each gets a
byte– All numbers are translated as they are read – Files can be read easily with DOS/windows
readers
Don Kussee -60 1410_appendix 31
Text Vs Binary
• Binary– Each byte is transferred without translation– Character takes one byte– Numbers are not treated as characters
• Each value requires memory size storage
• May use as much as 16 bytes per number
• Most save several bytes per number
– Not readable by DOS/windows
Don Kussee -60 1410_appendix 32
Sequential Files
• Text files
• Allow variable record length
• Most commonly used - text editors
• Like a tape recorder– Each must be read to find the next record– Can’t find 3 until you have read 1 & 2
Don Kussee -60 1410_appendix 33
Random Files
• Require fixed record length
• Much wasted internal space
• Can jump to the 5 record easy– If each was a length of 128 bytes– Seek (5 * 128) – Get FirstName, LName ‘ the read
• Open file For Random As 1 Len = 128
Don Kussee -60 1410_appendix 34
File Use - the code part
• Open the File
• Use the file– Input
• Input #, Line Input #, Get (Random)
– Output• Write # comma delimited, in quotes, newline• Print # space or newline delimited• Put (Random)
• Close the file
Don Kussee -60 1410_appendix 35
Sequential File Open Modes
• Input data from files into program– Opens file at the front
• Output data to files from program– Opens file at the front– Creates a file if none exists– Writes over current data
• Append data to file, – Opens file at the end – Creates a file if none found
Don Kussee -60 1410_appendix 36
File Open Modes
• Binary – Read-only, Write-only, Read and Write, Get &
Put used to access file– Creates file if it doesn’t exist
• Random – Read-only, Write-only, Read and Write– Creates file if it doesn’t exist
Don Kussee -60 1410_appendix 37
Network Operations
• Share.EXE must be installed – Read, Write, Read Write– Shared,
• Operations allowed by other processes – Lock Read – Lock Write– Lock Read Write
Don Kussee -60 1410_appendix 38
Open StatementOpen FilePath For Output As # 1
• Each file open at the same time gets a different number
• Max files open a system value (usually >50)
• FreeFile will generate the next open file number
Don Kussee -60 1410_appendix 39
Output Statements (usual)
Write #1, Form1.txtEdit.Text
Write #1, txtPasteBuffer.Text
• Saves the contents of the text box to the file in quotes, followed by an EOF
• Second Write would replace the EOF with a comma and save next text in quotes, followed by an EOF
Don Kussee -60 1410_appendix 40
File Use
• Output Input
• Print Line Input # or Input.– Writes display-formatted data to a sequential file. – Print #filenumber, [outputlist]
• Write Input– Writes data to a sequential file.– Write #filenumber, [outputlist]
Don Kussee -60 1410_appendix 41
Output Statements Print #1, Form1.PadText.Text
• , between items skips to next tab (14 places)
• ; between items puts them together (no space between them)
• Print #1, “ bananas ”, “apples”, “sauce”
• newline at the end of each statement
• Print #4, Spc(13) ‘would store 13 spaces
• Print #2, Tab(6) ‘tabs to 6 column
• Print #3, ‘outputs a blank line
Don Kussee -60 1410_appendix 42
Output StatementsWrite #1, names(index), “number”
• commas are between each item in the file
• “xxx” are around each string in the file
• a newline is inserted in the file after each statement
Don Kussee -60 1410_appendix 43
Input from file
• Reads data from an open sequential file and assigns the data to variables.Input #filenumber, varlist
Input #1, Form1.txtEdit.Text, txtPasteBuffer.Text
Line Input #1 puts into a string all data until EOL or EOF found
Don Kussee -60 1410_appendix 45
File pointer movement as event procedure cmdThreeAtATime_Click executes
Don Kussee -60 1410_appendix 46
Close # 1
• DOS file system used
• Data to buffer, then later, buffer to drive when buffer is full or file is closed
• Sends all data in the buffer to the file
• Appends an EOF, Updates Fat Table
• Closes the file
• Close ‘closes all open files
Don Kussee -60 1410_appendix 47
Input Statements (usual)Input #1, Form1.PadText.Text
• must match data in file to data type in program
• for string variables, – first “ is ignored, everything else (, space) up to
“ goes in– if no “ then end is assumed for , space or EOL– blank lines are 0 length strings
• Error generated on EOF
Don Kussee -60 1410_appendix 48
Input Statements (usual)Input #1, ScrollBar1.value, x, y
• for numerical variables– first non-space is start of number– space, comma or EOL is end of number– if non number (letters) the value is 0– blank line is 0
• arrays not usable array element is useable
Don Kussee -60 1410_appendix 49
EOF at end of file
• Error generated on EOF
index = 0
Do While Not EOF(1)
Input #1, x(index)
index = index + 1
Loop
Close # 1
Don Kussee -60 1410_appendix 50
Other Input Statements
• Input$ – only for Input of Binary mode– Returns all characters, commas, carriage
returns, linefeeds leading spaces
• Line Input #– reads all characters (except carriage returns &
linefeeds) in a line into a String or Variant
Don Kussee -60 1410_appendix 51
File use - the visual part
• Must determine the – Drive– Path– File name
• Must exist (or error) for Open Input• Will be generated for Open Output, Append• Errors will occur if Drive, Path, File
unavailable
Don Kussee -60 1410_appendix 52
Path using standard VB
• Build an input form using – DriveListBox– DirListBox– FileListBox
• Concatenate the variables together into path
• Filename$=Drive1.drive + “:\”
• Filename$=Filename$ + Dir1.Path + “\”
• Filename$=Filename$ + File.Filename
Don Kussee -60 1410_appendix 54
Code for OpenItem_click( )
Sub OpenItem_Click( )
CMDialog1.ShowSave
Open CMDialog1.FileName For Output as #1
Write #1, Form1.PadText.Text
Close #1
End Sub
Don Kussee -60 1410_appendix 55
File Save & Open Filter
• Defines the filter patterns to be displayed in a combo box description1 | filter1 | description2 | filter2 |description3 | filter3
• Then the filter index defines the default pattern CMDialog.filter= “Icons|*.ico|Bitmaps|*.bmp”
• CMDialog.FilterIndex = 2
Don Kussee -60 1410_appendix 56
CMDialog1.Flag =
• Flags sets and returns miscellaneous settings
• CONSTANT.TXT has define many constants
• OFN_FILEMUSTEXIST defined as &H1000
• Name and path validation– &H1000 Asserts the file must exist– &H400 Returns if exe different from default– &H8 Forces the directory to the default directory– &H100 Invalid characters not accepted– &H800 Invalid path not accepted
Don Kussee -60 1410_appendix 57
CMDialog1.Flag =
• Read only setting or returns– &H4 Hides read only check box– &H1 Set Read only bit – &H8000 Asserts file is NOT read only OR Write
. Protected
• File protection or creation– &H2000 Prompt for creation if it does not exist– &H2 Confirm overwriting an existing file– &H4000 Ignore file sharing violation
Don Kussee -60 1410_appendix 58
CMDialog1.Flag =
• Misc..– &H200 File Name list allows multiple
selections– &H10 Display a Help button
Don Kussee -60 1410_appendix 59
Code for SaveItem_click( )
• Sub OpenItem_Click( )– On Error Resume Next– CMDialog1.CancelError = -1– CMDialog1.DialogTitle = “Save File”– CMDialog1.Filter = “Text Files | *.txt|All files|*.*”– CMDialog1.FilterIndex = 1– CMDialog1.DefaultExt = “*.TXT”– CMDialog1.Flags = OFN_CREATEPROMPT – Or OFN_OVERWRITEPROMPT
Don Kussee -60 1410_appendix 60
Load current values Color, Font
CMDialog1.FontName = PadText.FontName
CMDialog1.FontItalic = PadText.FontItalic
CMDialog1.FontStrikethru = PadText.FontStrikethru
CMDialog1.FontSize = PadText.FontSize
CMDialog1.FontUnderline = PadText.FontUnderline
CMDialog1.Color = PadText.Color
CMDialog1.Flags = CF_SCREENFONTS
Don Kussee -60 1410_appendix 62
Error Handling
• Occurs with runtime errors
• Divide by zero
• Invalid input
• File Open, File Save– Directory or File missing– Disk Missing– Write protect or disk door open
Don Kussee -60 1410_appendix 63
Error trapping
• Establish a watch dog
• When the watch dog is triggered identify what to do– Continue the code anyway– Retry the code– Allow the user to intervene– Discontinue the program
• Return to the program or Exit
Don Kussee -60 1410_appendix 64
Error trapping
Private Sub object_event
On Error GoTo Apple ‘watch dog
VB code
End Sub
Apple: ‘ Label start of error handling code
VB Code that handles the error
End Sub ‘the error handling subroutine
Don Kussee -60 1410_appendix 65
Possible Error handling
• At the end of the error handling, at the end of the code
• Resume ‘reruns the error statement• Resume Next ‘starts at the next statement• Resume Label ‘Starts at a new location• Exit Sub ‘Dynamite out of subroutine• Exit Function ‘Escapes out of function
Don Kussee -60 1410_appendix 66
Error handling in functions
• IF a line of code calls a function1 which calls a function2 calls a function3
• An error in function3 looks up the chain for the closest On Error code
• Resume will restart the function1 call
• Best to turn off On Error if this is a problem
• On Error GoTo 0 turns off the watch dog
Don Kussee -60 1410_appendix 67
Some Trappable Errors
• 6 Overflow• 11 Division by Zero• 13 Type mismatch• 18 User interrupt• 52 Bad file name• 53 File not found• 54 Bad file mode• 55 File already open
• 57 Device I/O error• 58 File already exists• 59 Bad record length• 61 Disk full• 67 Too many files• 71 Disk not ready• 76 Path not found• 482 Printer error
Don Kussee -60 1410_appendix 68
Error Object
• Err.Number ‘contains the error number
• Err.Source ‘contains the object
• Err.Description ‘Cryptic error message
• Err.Clear ‘ Clears the error
• Err.Raise(number) ‘ Creates an error number for testing purposes
• On Error GoTo 0 ‘Turn off watch dog
top related