home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / VISUAL_B / ARQS_ZIP / MAINF.ZIP / UTILS.BAS < prev    next >
Encoding:
BASIC Source File  |  1992-07-05  |  9.5 KB  |  266 lines

  1. DefInt A-Z
  2.  
  3. Declare Function ControlhWnd% Lib "CTLHWND.DLL" (Ctl As Control)
  4.  
  5. 'API function used by SelectItem
  6. Declare Function SendMessage Lib "User" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Any) As Long
  7. 'API function used by ClearList (requires different calling convention than Select Item)
  8. Declare Function SendMessage2 Lib "User" Alias "SendMessage" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, lParam As Any) As Long
  9.  
  10. Function FileErrors (ErrVal As Integer) As Integer
  11. ' This function is a modification of one of the sample programs included
  12. ' in the Visual Basic Programmer's Guide. We do *not* assert copyright
  13. ' for this function.
  14.     ' Return Value  Meaning         Return Value    Meaning
  15.     ' 0             Resume          2               Unrecoverable error
  16.     ' 1             Resume Next     3               Unrecognized error
  17.     MsgType% = 48   ' Exclamation point
  18.     Select Case ErrVal
  19.     Case ErrTYPEMISMATCH                        ' Error #13
  20.         Msg$ = "A data type mismatch occurred."
  21.     Case ErrDEVICEUNAVAILABLE                   ' Error #68
  22.         Msg$ = "That device is unavailable."
  23.         MsgType% = MsgType% + 4                 '
  24.     Case ErrDISKNOTREADY                        ' Error #71
  25.         Msg$ = "Put a disk in the drive and close the door"
  26.     Case ErrDEVICEIO                            ' Error # 57
  27.         Msg$ = "Ouch! Internal Disk Error"
  28.         MsgType% = MsgType% + 4
  29.     Case ErrDISKFULL                            ' Error #61
  30.         Msg$ = "Umph! Disk full--Continue?"
  31.         MsgType% = 35
  32.     Case ErrBADFILENAME, ErrBADFILENAMEORNUMBER ' Errors #64, #52
  33.         Msg$ = "Um, I can't find that file..."
  34.     Case ErrPATHDOESNOTEXIST                    ' Error #76
  35.         Msg$ = "That directory path doesn't exist."
  36.     Case ErrBADFILEMODE                         ' Error #54
  37.         Msg$ = "Can't open the file for that type of access."
  38.     Case ErrFILEALREADYOPEN                     ' Error #55
  39.         Msg$ = "The file is already open."
  40.     Case ErrPASTENDOFFILE                       ' Error #62
  41.         Msg$ = "This file has a nonstandard end-of-file"
  42.         Msg$ = Msg$ + " marker, or an attempt was made to read"
  43.         Msg$ = Msg$ + " beyond the end-of-file marker."
  44.     Case Else
  45.         FileErrors = 3
  46.         Msg$ = "Uh oh. Error #" + Str$(Err) + " was encountered." + Chr$(13) + Chr$(10)
  47.         Msg$ = Msg$ + "System Message: " + Error$
  48.         MsgBox Msg$, 16, "Bad News"
  49.         Stop
  50.         Exit Function
  51.     End Select
  52.     
  53.     Response% = MsgBox(Msg$, MsgType%, "Ouch! Disk Error")
  54.  
  55.     Select Case Response%
  56.     Case 1, 4                                   ' OK, Retry buttons
  57.         FileErrors = 0
  58.     Case 5                                      ' Ignore button
  59.         FileErrors = 1
  60.     Case 2, 3                                   ' Cancel, Abort buttons
  61.         FileErrors = 2
  62.     Case Else
  63.         FileErrors = 3
  64.     End Select
  65. End Function
  66.  
  67. Function FileOpener (NameToUse$, Mode%, RecordLength%) As Integer
  68. ' This function is one of the sample programs included in the Visual
  69. ' Basic Programmer's Guide (pp. 318-320). We do *not* assert copyright
  70. ' for this function.
  71.     ' Opens a file in specified access mode.
  72.     ' Arguments: NameToUse$     -    String with a valid filename
  73.     '            Mode%          -    Integer of file access mode
  74.     '            RecordLength%  -    Length of one record
  75.     ' Returns:   Integer by which the file can be specified in the program.
  76.     '            Returns 0 if file open fails.
  77.  
  78.     FileNum% = FreeFile             'Get the next free file number
  79.     On Error GoTo OpenerError       'Error handler
  80.     Select Case Mode                'Determine the mode we want
  81.     Case REPLACEFILE
  82.         Open NameToUse For Output As FileNum%
  83.     Case READFILE
  84.         Open NameToUse For Input As FileNum%
  85.     Case ADDTOFILE
  86.         Open NameToUse For Append As FileNum%
  87.     Case RANDOMFILE
  88.         Open NameToUse For Random As FileNum% Len = RecordLength%
  89.     Case BINARYFILE
  90.         Open NameToUse For Binary As FileNum%
  91.     Case Else
  92.         Exit Function
  93.     End Select
  94.  
  95.     FileOpener = FileNum%           'Return the file number
  96. Exit Function
  97.  
  98. OpenerError:
  99.     Action% = FileErrors(Err)
  100.     Select Case Action%
  101.         Case 0
  102.         Resume
  103.         Case Else
  104.         FileOpener = 0      'Too bad....
  105.         Exit Function
  106.     End Select
  107.  
  108. End Function
  109.  
  110. Function FioSlice (InString$, WordNum%, Delimeter$, StartPos%) As String
  111.  
  112. ' FioSlice was outlined by Ed Girou. FioSlice parses strings into definable words
  113. ' that can be acted upon. Given that many CompuServe commands have a syntax like
  114. ' "DOW PROTO:B THISFILE THISNAME" it's handy to be able to just ask for the second
  115. ' word in the string.
  116.  
  117. ' ================================================================================================
  118. ' MAINTENANCE HISTORY:
  119. ' Version   Date        Coder       Action
  120. '   0.1     5/1/92      Murdoch     Initial keyin
  121. '   0.1a    5/3/92      Murdoch     Slight amendment--fewer messages, more values returned.
  122. '
  123. ' To do:
  124. '
  125.  
  126. ' ================================================================================================
  127.     If Len(InString$) = 0 Then
  128.     FioSlice = GioDEFAULT                        ' GioDEFAULT is global for bad news
  129.     Exit Function
  130.     End If
  131.     
  132.     If Len(Delimeter$) = 0 Then                    ' We'll default to using a space
  133.     Delimeter$ = " "
  134.     End If
  135.     
  136.     If WordNum% = 0 Then                           ' We'll be thorough...
  137.     WordNum% = 1                                 ' ...in our error testing
  138.     End If
  139.     
  140.     If StartPos% = 0 Then
  141.     StartPos% = 1
  142.     ElseIf StartPos% >= Len(InString$) Then
  143.     FioSlice = GioDEFAULT
  144.     Exit Function
  145.     End If
  146.  
  147.     ' This loop starts by initializing DelimeterPos%. We count from 1 to WordNum%--
  148.     ' remember that For..Next loops are evaluated at the *end* of each iteration. If
  149.     ' WordNum% equals 1 we'll just go through once. If WordNum% equals 5 we'll go through
  150.     ' 5 times. (If WordNum% had equalled 0 we'd have reset it to 1 in the trap above.)
  151.     ' We'll find the first instance of Delimeter$. If we have to count in several words,
  152.     ' we'll assign the old DelimiterPos% to BeginPos%, and search again. Once we're done,
  153.     ' we'll use Mid$ to extract just that portion we want.
  154.     
  155.     DelimeterPos% = StartPos%                       ' Initialize the value
  156.     For Counter% = 1 To WordNum%                    ' Set up counter loop
  157.     BeginPos% = DelimeterPos%                   ' This helps when searching for word 2 or 3
  158.     DelimeterPos% = InStr((BeginPos% + 1), InString$, Delimeter$)
  159.                             
  160.     If DelimeterPos% = 0 Then
  161.         If WordNum% - Counter% > 0 Then         ' Oops! We can't find the word
  162.         FioSlice = GioDEFAULT               ' GioDEFAULT is our "bad news" global
  163.         Exit Function
  164.         Else                                    ' We're looking for the last word
  165.         DelimeterPos% = Len(InString$) + 1  ' We'll adjust DelimeterPos% to set up the Mid$
  166.         Exit For
  167.         End If
  168.     End If
  169.     Next
  170.  
  171.     If BeginPos% > 1 Then BeginPos% = BeginPos% + 1 ' This cures a bug in the next line--we don't
  172.                             ' want the delimeter included in the string,
  173.                             ' but we *do* want the first letter of the first
  174.                             ' word in the string.
  175.     
  176.     Length% = DelimeterPos% - BeginPos%             ' Length of word we want
  177.  
  178.     FioSlice = Mid$(InString$, BeginPos%, Length%)  ' Extract the word we want, and quit.
  179.  
  180. End Function
  181.  
  182. Sub CenterForm (TheForm As Form)
  183.     TheForm.WindowState = 0
  184.     TheForm.left = (Screen.Width - TheForm.Width) / 2
  185.     TheForm.top = (Screen.Height - TheForm.Height) / 2
  186.  
  187. End Sub
  188.  
  189. Sub SioMsgBox (Msg$, MsgType%, Title$)
  190. ' Replacement for VB MsgBox command.  Adds sound support.
  191. ' Same parameters and return values as VB command.
  192. ' Sound event is determined from MsgType%
  193.  
  194.     If MsgType% < 16 Then
  195.     MsgSound% = 0
  196.     ElseIf MsgType% < 32 Then
  197.     MsgSound% = MB_ICONHAND
  198.     ElseIf MsgType% < 48 Then
  199.     MsgSound% = MB_ICONQUESTION
  200.     ElseIf MsgType% < 64 Then
  201.     MsgSound% = MB_ICONEXCLAMATION
  202.     ElseIf MsgType% < 80 Then
  203.     MsgSound% = MB_ICONASTERISK
  204.     Else MsgSound% = 0
  205.     End If
  206.     MessageBeep MsgSound%
  207.     MsgBox Msg$, MsgType%, Title$
  208. End Sub
  209.  
  210. Function FioMsgBox (Msg$, MsgType%, Title$) As Integer
  211. ' Replacement for VB MsgBox function.  Adds sound support.
  212. ' Same parameters and return values as VB function.
  213. ' Sound event is determined from MsgType%
  214.     If MsgType% < 16 Then
  215.     MsgSound% = 0
  216.     ElseIf MsgType% < 32 Then
  217.     MsgSound% = MB_ICONHAND
  218.     ElseIf MsgType% < 48 Then
  219.     MsgSound% = MB_ICONQUESTION
  220.     ElseIf MsgType% < 64 Then
  221.     MsgSound% = MB_ICONEXCLAMATION
  222.     ElseIf MsgType% < 80 Then
  223.     MsgSound% = MB_ICONASTERISK
  224.     Else MsgSound% = 0
  225.     End If
  226.     MessageBeep MsgSound%
  227.     FioMsgBox = MsgBox(Msg$, MsgType%, Title$)
  228.  
  229. End Function
  230.  
  231. Function DirExists (TheDirectory As String) As Integer
  232. ' Returns True if Directory exists, False if it doesn't
  233.  
  234. OldDir$ = CurDir$
  235. On Error Resume Next
  236. ChDir TheDirectory
  237. If Err = 76 Then
  238.     Err = 0
  239.     DirExists = FALSE
  240. Else
  241.     DirExists = TRUE
  242. End If
  243. ChDir OldDir$
  244. On Error GoTo 0
  245.  
  246.  
  247. End Function
  248.  
  249. Function SelectItem (Ctl As Control, SearchString As String)
  250.     lbhWnd = ControlhWnd(Ctl)
  251.     Index = SendMessage(lbhWnd, LB_SELECTSTRING, -1, SearchString)
  252.     SelectItem = Index
  253. End Function
  254.  
  255. Sub ClearList (LB As Control)
  256.     If TypeOf LB Is ComboBox Then
  257.     Msg% = CB_RESETCONTENT
  258.     ElseIf TypeOf LB Is ListBox Then
  259.     Msg% = LB_RESETCONTENT
  260.     End If
  261.     lbhWnd = ControlhWnd(LB)
  262.     Index = SendMessage2(lbhWnd, Msg%, 0, 0)
  263.  
  264. End Sub
  265.  
  266.