home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / bitmap / fopen.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-05-08  |  13.5 KB  |  445 lines

  1. VERSION 2.00
  2. Begin Form FOpenForm 
  3.    BorderStyle     =   3  'Fixed Double
  4.    Caption         =   "File Open"
  5.    ClientHeight    =   3150
  6.    ClientLeft      =   3000
  7.    ClientTop       =   2460
  8.    ClientWidth     =   4980
  9.    Height          =   3555
  10.    Icon            =   FOPEN.FRX:0000
  11.    Left            =   2940
  12.    LinkMode        =   1  'Source
  13.    LinkTopic       =   "Form1"
  14.    MaxButton       =   0   'False
  15.    MinButton       =   0   'False
  16.    ScaleHeight     =   3150
  17.    ScaleWidth      =   4980
  18.    Top             =   2115
  19.    Width           =   5100
  20.    Begin ListBox List1 
  21.       Height          =   1785
  22.       Left            =   1965
  23.       Sorted          =   -1  'True
  24.       TabIndex        =   1
  25.       Top             =   1170
  26.       Width           =   1575
  27.    End
  28.    Begin FileListBox File1 
  29.       Height          =   1785
  30.       Left            =   165
  31.       TabIndex        =   3
  32.       Top             =   1170
  33.       Width           =   1575
  34.    End
  35.    Begin CommandButton Command3 
  36.       Caption         =   "Load &Null"
  37.       Height          =   375
  38.       Left            =   3705
  39.       TabIndex        =   10
  40.       Top             =   1095
  41.       Width           =   1095
  42.    End
  43.    Begin CommandButton Command2 
  44.       Caption         =   "&Cancel"
  45.       Height          =   375
  46.       Left            =   3705
  47.       TabIndex        =   7
  48.       Top             =   615
  49.       Width           =   1095
  50.    End
  51.    Begin CommandButton Command1 
  52.       Caption         =   "&Open"
  53.       Default         =   -1  'True
  54.       Height          =   375
  55.       Left            =   3705
  56.       TabIndex        =   6
  57.       Top             =   135
  58.       Width           =   1095
  59.    End
  60.    Begin TextBox Text1 
  61.       Height          =   315
  62.       Left            =   1140
  63.       TabIndex        =   5
  64.       Text            =   "FileName"
  65.       Top             =   165
  66.       Width           =   2400
  67.    End
  68.    Begin Label DirLabel 
  69.       Caption         =   "&Directories:"
  70.       Height          =   195
  71.       Left            =   1970
  72.       TabIndex        =   0
  73.       Top             =   900
  74.       Width           =   1035
  75.    End
  76.    Begin Label FilesLabel 
  77.       AutoSize        =   -1  'True
  78.       Caption         =   "&Files:"
  79.       Height          =   195
  80.       Left            =   170
  81.       TabIndex        =   2
  82.       Top             =   915
  83.       Width           =   465
  84.    End
  85.    Begin Label Label1 
  86.       Caption         =   "Label1"
  87.       Height          =   255
  88.       Left            =   1155
  89.       TabIndex        =   9
  90.       Top             =   580
  91.       Width           =   2310
  92.    End
  93.    Begin Label Label4 
  94.       Caption         =   "Directory:"
  95.       Height          =   255
  96.       Left            =   170
  97.       TabIndex        =   8
  98.       Top             =   580
  99.       Width           =   855
  100.    End
  101.    Begin Label FNameLabel 
  102.       Caption         =   "File &Name:"
  103.       Height          =   255
  104.       Left            =   170
  105.       TabIndex        =   4
  106.       Top             =   210
  107.       Width           =   975
  108.    End
  109. 'You are welcome to use FOPEN in your programs free of charge.
  110. 'If you make any improvements send me a copy at CIS-MAL 73667,1755
  111. 'Costas Kitsos
  112. DefInt A-Z
  113. Dim TheFocus%                   ' Handle for Drive/Subdirectory ListBox
  114. Dim List1Flag%                  ' Flag for Drive/Subdirectory ListBox 0 or 1
  115. Dim Text1Flag%                  ' Flag for EM_LIMITTEXT
  116. Dim TheDrive$                   ' The selected drive
  117. Dim LastChange As Integer       ' Flag used when processing selections
  118. Function BuildSpec (fpath As String) As String
  119.  ' builds the spec for SendMessage
  120.     If Right$(fpath, 1) <> "\" Then
  121.     s$ = fpath + "\*.*"
  122.     Else
  123.     s$ = fpath + "*.*"
  124.     End If
  125.     BuildSpec = s$
  126.     s$ = ""
  127. End Function
  128. Sub ChangeDir (b$)
  129. ' change to the new directory and update List1
  130.  List1.SetFocus
  131.  TheFocus% = GetFocus()
  132.  If InStr(b$, ":") Then b$ = Right$(b$, Len(b$) - 2)
  133.  If Left$(b$, 1) <> "\" Then b$ = "\" + b$
  134.  On Error Resume Next
  135.    File1.Path = TheDrive$ + b$
  136.    Label1.caption = File1.Path
  137.    y& = SendMessage(TheFocus%, LB_RESETCONTENT, 0, ByVal "")
  138.    x& = SendMessage(TheFocus%, LB_DIR, &HC010, ByVal BuildSpec((File1.Path)))
  139.  If Err Then
  140.  ' you may add a MsgBox error message here if you think it's
  141.  ' necessary.
  142.  End If
  143. End Sub
  144. Sub ChangeDrive (a$, ErrState%)
  145.     OldPath$ = File1.Path
  146.     List1.SetFocus
  147.     TheFocus% = GetFocus()
  148.  ' try to change to the new drive
  149.     On Error Resume Next
  150.         File1.Path = a$ + ":"
  151.         y& = SendMessage(TheFocus%, LB_RESETCONTENT, 0, ByVal "")
  152.         s$ = a$ + ":*.*"
  153.         x& = SendMessage(TheFocus%, LB_DIR, &HC010, ByVal s$)
  154.         Label1.caption = File1.Path
  155.         TheDrive$ = a$ + ":"
  156.         ErrState% = False
  157.  ' if an error occurred go back to the way things were
  158.     If Err Then
  159.         MsgBox (Error$ + Chr$(13) + Chr$(10) + TheDrive$), 16, FormTitle
  160.         TheDrive$ = Left$(OldPath$, 2)
  161.         File1.Path = OldPath$
  162.         If Right$(File1.Path, 1) <> "\" Then
  163.         s$ = File1.Path + "\*.*"
  164.         Else
  165.         s$ = File1.Path + "*.*"
  166.         End If
  167.         y& = SendMessage(TheFocus%, LB_RESETCONTENT, 0, ByVal "")
  168.         x& = SendMessage(TheFocus%, LB_DIR, &HC010, ByVal s$)
  169.         Label1.caption = File1.Path
  170.         Text1.Text = ThePattern
  171.         ErrState% = True    'change the flag so Text1 knows
  172.     End If
  173. End Sub
  174. Sub Command1_Click ()
  175.     Select Case LastChange
  176.       Case 1  'process Text1 entry
  177.         Text1_Keypress (13)
  178.     Case 2  'we have a file, put together the FullName
  179.         ThePath = File1.Path
  180.         TheFileName = File1.FileName
  181.         FullName = Left$(BuildSpec((File1.Path)), Len(BuildSpec((File1.Path))) - 3) + File1.FileName
  182.         FOpenForm.Hide
  183.     Case 3  'let List1 know
  184.         List1_Dblclick
  185.     Case 4  'we have a file and a FullName
  186.         FOpenForm.Hide
  187.     Case 5  'we have a file, put together the FullName
  188.         ThePath = File1.Path
  189.         FullName = Left$(BuildSpec((File1.Path)), Len(BuildSpec((File1.Path))) - 3) + TheFileName
  190.         FOpenForm.Hide
  191.     Case Else
  192.     End Select
  193. End Sub
  194. Sub Command2_Click ()
  195.  ' did the user press cancel?
  196.  ' Change FullName to "CANCEL" so the Parent knows.
  197.     FullName = "CANCEL"
  198.     FOpenForm.Hide
  199. End Sub
  200. Sub Command3_Click ()
  201. ' The user pressed Load Null, so set FullName to ""
  202. FullName = ""
  203. FOpenForm.Hide
  204. End Sub
  205. Sub File1_Click ()
  206. ' update the textbox and the lastchange flag
  207.   Text1.Text = File1.FileName
  208.   LastChange = 2
  209. End Sub
  210. Sub File1_DblClick ()
  211. ' Good, we have a file, let's tell Command1
  212.     LastChange = 2
  213.     Command1_Click
  214. End Sub
  215. Sub File1_KeyPress (KeyAscii As Integer)
  216. ' if Return, select File1_DblClick
  217.     If KeyAscii = 13 Then
  218.     If File1.Listindex > -1 Then File1_DblClick
  219.     End If
  220. End Sub
  221. Sub Form_GotFocus ()
  222.     If List1Flag% = 0 Then
  223.     List1.SetFocus          ' Set the Focus on List1 to fill the ListBox
  224.     End If
  225. End Sub
  226. Sub Form_Load ()
  227.  ' Set the flags for List1 and Text1
  228.     List1Flag% = 0  ' Update Drive/Subdirectory listbox
  229.     Text1Flag% = 0  ' Limit text length
  230.  ' If the Parent didn't specify a FormTitle use the one that's built in.
  231.     If FormTitle = "" Then
  232.     FOpenForm.caption = "File Open"
  233.     FormTitle = FOpenForm.caption
  234.  ' otherwise honor the Parent's specification
  235.     Else
  236.     FOpenForm.caption = FormTitle
  237.     End If
  238.  ' If there is a path specification use it, otherwise use the default.
  239.     If ThePath <> "" Then
  240.     If Right$(ThePath, 1) = "\" Then
  241.         ThePath = Left$(ThePath, (Len(ThePath) - 1))
  242.         If Right$(ThePath, 1) = ":" Then ThePath = ThePath + "\"
  243.     End If
  244.     File1.Path = ThePath
  245.     End If
  246.     If ThePath = "" Then ThePath = File1.Path
  247.  ' If the Parent specified a new pattern then use it.
  248.     If ThePattern <> "" Then
  249.     File1.Pattern = ThePattern
  250.     End If
  251.  ' Finish up loading the form.
  252.     Text1.Text = File1.Pattern
  253.     TheDrive$ = Left$(File1.Path, 2)
  254.     Label1.caption = File1.Path
  255. End Sub
  256. Sub List1_Click ()
  257.  ' let Command1 know
  258.     LastChange = 3
  259. End Sub
  260. Sub List1_Dblclick ()
  261. ' List1 holds both drives and subdirectories
  262.  If List1.Listindex > -1 Then
  263.     curnt$ = List1.List(List1.Listindex)        'get the current selection
  264.     OldPath$ = File1.Path                       'save the old path in case of error
  265.  ' if the user chose a drive parse it and change to it
  266.     If Left$(curnt$, 2) = "[-" And Len(curnt$) = 5 Then
  267.     If Right$(curnt$, 2) = "-]" Then
  268.         a$ = Mid$(curnt$, 3, 1)
  269.         ChangeDrive a$, ErrState%
  270.     End If
  271.  ' if the user chose a subdirectory change to it
  272.     Else
  273.     On Error Resume Next
  274.     b$ = Mid$(curnt$, 2, Len(curnt$) - 2)
  275.     File1.Path = TheDrive$ + b$
  276.     Label1.caption = File1.Path
  277.  ' LB_RESETCONTENT clears the list fast
  278.  ' LB_DIR specifies the type of listbox, &HC010 specifies drives and subdirectories only.
  279.     y& = SendMessage(TheFocus%, LB_RESETCONTENT, 0, ByVal "")
  280.     x& = SendMessage(TheFocus%, LB_DIR, &HC010, ByVal BuildSpec((File1.Path)))
  281.  ' if there was an error let the user know.
  282.     If Err Then
  283.     MsgBox Error$ + Chr$(13) + Chr$(10) + File1.Path, 16, FormTitle
  284.     Text1.Text = File1.Pattern
  285.     End If
  286.     End If
  287.  End If
  288. End Sub
  289. Sub List1_GotFocus ()
  290. If List1Flag% = 0 Then
  291. ' get the handle of the ListBox
  292.     TheFocus% = GetFocus()
  293. ' fill it with the Drive/Subdirectory listing
  294.     x& = SendMessage(TheFocus%, LB_DIR, &HC010, ByVal BuildSpec((File1.Path)))
  295. ' update the flag so we don't go through this every time
  296.     List1Flag% = 1
  297. ' highlight Text1
  298.     Text1.selstart = 0
  299.     Text1.sellength = Len(Text1.Text)
  300.     If visible Then Text1.SetFocus
  301. End If
  302. TheFocus% = GetFocus()
  303. End Sub
  304. Sub List1_KeyPress (KeyAscii As Integer)
  305.  ' if the user presses the Return key while on a valid entry, invoke a List1_DblClick
  306.     If KeyAscii = 13 Then
  307.     If List1.Listindex > -1 Then
  308.         List1_Dblclick
  309.     End If
  310.     End If
  311. End Sub
  312. Function ProcessEntry (a$, b$) As Integer
  313.  ' we'll use this if/when we have to use OpenFile
  314.   Dim TheStruct As OfStruct
  315.  ' if a$(newd$) isn't empty change to it.
  316.  If a$ <> "" Then
  317.     If Len(a$) > 1 Then
  318.     a$ = Left$(a$, Len(a$) - 1)
  319.     ChangeDir (a$)
  320.     End If
  321.  End If
  322.  'if b$(tempo$) isn't empty let's see what it could be.
  323.  If Len(b$) > 0 Then
  324.  'if it's a new pattern then let File1.List know
  325.     If InStr(b$, "*") > 0 Or InStr(b$, "?") > 0 Then
  326.     File1.Pattern = b$
  327.     Text1.Text = File1.Pattern
  328.     ProcessEntry = True
  329.     Exit Function
  330.     Else
  331.  'otherwise could it be a file?
  332.     If Right$(File1.Path, 1) <> "\" Then
  333.         test$ = File1.Path + "\"
  334.     Else
  335.         test$ = File1.Path
  336.     End If
  337.     test$ = test$ + b$
  338.  'use OpenFile to see if it exists.  If so x% will return a file handle.
  339.  'This is a very useful API function.
  340.     x% = OpenFile(test$, TheStruct, OF_EXIST)
  341.  'close the file
  342.     i% = lclose(x%)
  343.  'if we have a file handle, we're done let's tell Command1
  344.         If x% > 0 Then
  345.         ThePath = File1.Path
  346.         TheFileName = b$
  347.         FullName = test$
  348.         LastChange = 4
  349.         Command1_Click
  350.         Else
  351.  'no handle? The user probably specified a subdirectory.
  352.         ChangeDir (test$)
  353.         End If
  354.     ProcessEntry = True
  355.     End If
  356.  End If
  357. End Function
  358. Function RemoveSpaces (TheText$) As String
  359. ' squeezes out spaces
  360.     t$ = TheText$
  361.     i% = 1
  362.     Do
  363.       i% = InStr(i%, t$, " ")
  364.       If i% = 0 Then
  365.          Exit Do
  366.       End If
  367.       t$ = Left$(t$, i% - 1) + Mid$(t$, i% + 1)
  368.       i% = i%
  369.     Loop
  370.     RemoveSpaces = t$
  371.     t$ = ""
  372. End Function
  373. Sub Text1_Change ()
  374.  'needed by Command1
  375.     LastChange = 1
  376. End Sub
  377. Sub Text1_GotFocus ()
  378.  If Text1Flag% = 0 Then
  379.   ' Do this only once
  380.   ' send the message to limit the text entry to not more than 127 characters
  381.     maxi% = 127
  382.     TextLimit& = SendMessage(GetFocus(), EM_LIMITTEXT, maxi%, ByVal "")
  383.     Text1Flag% = 1
  384.  End If
  385.  Text1Flag% = 1
  386. End Sub
  387. Sub Text1_Keypress (KeyAscii As Integer)
  388. ' process whatever the user typed in Text1
  389.  If KeyAscii = 13 Then KeyAscii = 0   ' get rid of the beep (Mark, thanks for the tip)
  390.  If KeyAscii = 0 Then
  391. ' remove any space characters.  Some users put a space after a period out of habit.
  392.     temp$ = RemoveSpaces((Text1.Text))
  393. ' see if the file is in File1.List. If it is, we're done so let's tell Command1.
  394.     For ind% = 0 To File1.Listcount
  395.     If File1.List(ind%) = temp$ Then
  396.         TheFileName = temp$
  397.         Foundit = True
  398.         LastChange = 5
  399.         Command1_Click
  400.         Exit For
  401.     End If
  402.     Next ind%
  403.  ' if the file is not in File1.List let's see what the user is trying to do.
  404.     If Foundit <> True Then
  405.     cp = InStr(temp$, ":")  'drive?
  406.     bp = InStr(temp$, "\")  'subdirectory?
  407.     sp = InStr(temp$, "*")  'wildcards?
  408.     qp = InStr(temp$, "?")
  409.     ErrState% = False       'flag used by ChangeDrive
  410.     If cp Then      'if we found a drive change to it
  411.        ChangeDrive (Mid$(temp$, cp - 1, 1)), ErrState%
  412.     End If
  413.  ' If changing to the drive didn't cause any errors or if a drive wasn't specified
  414.     If Not ErrState% Then
  415.  ' if a subdirectory was specified
  416.     If bp Then
  417.         tempo$ = temp$
  418.         While InStr(tempo$, "\")
  419.             newd$ = newd$ + Left$(tempo$, InStr(tempo$, "\"))
  420.             tempo$ = Right$(tempo$, Len(tempo$) - InStr(tempo$, "\"))
  421.         Wend
  422.  ' newd$ holds everything to the left of the last backslash
  423.  ' tempo$ hold the rest.  Now, process them.
  424.         pe% = ProcessEntry(newd$, tempo$)
  425.     End If
  426.  ' did the user specify only a new pattern?
  427.     If pe% = False Then
  428.         If sp Or qp Then
  429.         If cp Then
  430.             File1.Pattern = Right$(temp$, Len(temp$) - 2)
  431.         Else
  432.             File1.Pattern = temp$
  433.         End If
  434.         Text1.Text = File1.Pattern
  435.         End If
  436.     End If
  437.     End If
  438.  End If
  439.  ' highlight the text
  440.  Text1.selstart = 0
  441.  Text1.sellength = Len(Text1.Text)
  442.  If visible Then Text1.SetFocus
  443. End If
  444. End Sub
  445.