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