567 572




Visual Basic 6 Black Book:File Handling And File Controls
function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { var end = document.cookie.indexOf (";", j); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(j, end)); } i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } var m1=''; var gifstr=GetCookie("UsrType"); if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; } document.write(m1+m2+m3);            Keyword Title Author ISBN Publisher Imprint Brief Full  Advanced      Search  Search Tips Please Select ----------- Components Content Mgt Certification Databases Enterprise Mgt Fun/Games Groupware Hardware IBM Redbooks Intranet Dev Middleware Multimedia Networks OS Prod Apps Programming Security UI Web Services Webmaster Y2K ----------- New Titles ----------- Free Archive To access the contents, click the chapter and section titles. Visual Basic 6 Black Book (Publisher: The Coriolis Group) Author(s): Steven Holzner ISBN: 1576102831 Publication Date: 08/01/98 function isIE4() { return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') ); } function bookMarkit() { var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0"; parent.location.href=url; //var win = window.open(url,"myitk"); //if(!isIE4()) // win.focus(); } Search this book:  














Previous
Table of Contents
Next




Using The Drive List Box Control
Usually you use the Common Dialogs File Open and File Save As to get file names and file paths from the user, but sometimes that just won’t do. For example, you have a program where you want to let the user select files but don’t want to use dialog boxes. In that and similar cases, you can use the Visual Basic file controls: the drive list box, the directory list box, and the file list box. These controls are intrinsic to Visual Basic (that is, they appear in the toolbox when you start Visual Basic).

The Drive List Box Control tool appears as the seventh tool down on the right in the Visual Basic toolbox in Figure 17.9. Use this tool to draw a drive list box in a form, as shown at upper left in Figure 17.10.

Figure 17.9  The Drive List Box Control tool.

Figure 17.10  A program with a drive list box.
You get the currently selected drive in a drive list box by using its Drive property, and when the user changes the drive in that control, a Change event is generated. Here’s an example—when the user selects a new drive, we pass that new drive on to a directory list box, Dir1, using that drive as the new root directory in Dir1:


Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub


Using The Directory List Box Control
The directory list box control displays directories as a hierarchical set of folders. This control is one of the file controls that are intrinsic to Visual Basic; its tool appears as the eighth tool down on the left in Figure 17.11.


Figure 17.11  The Directory List Box Control tool.
To add a directory list box to a form, just use its tool in the toolbox. We’ve added a directory list box to the program in Figure 17.10 (see earlier), at lower left.

The important property of the directory list box is the Path property, which holds the path of the current directory. When the user changes the current path, a Change event is generated. For example, when the user makes a change in a directory list box, Dir1, we can pass the new path to a file list box, File1, this way in the Change event handler:


Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub


Using The File List Box Control
The file list box control lets you display the files in a directory as a list of names. This control’s tool appears as the eighth tool down on the right in Figure 17.12. To add this control to a form, just draw it as you want it with its tool in the toolbox.


Figure 17.12  The File List Box Control tool.
The important properties of the file list box are the Path and FileName properties. Let’s see an example using the drive, directory, and file list boxes. When the user selects a file and clicks a button labeled Display File, or double-clicks the file’s name in the file list box, we’ll display the contents of the selected file in a text box.
We start by adding the controls we’ll need: a drive list box, Drive1; a directory list box, Dir1; a file list box, File1; a command button, Command1, which is labeled Display File; and a text box with its MultiLine property set to True and its Scrollbars property set to Both (if the file you are displaying is too long for a text box, use a rich text box).
When the user changes the drive, we pass that new drive to the directory list box as the new directory in Drive1_Change():


Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub


When the user changes the directory, we pass that new path to the file list box in Dir1_Change():


Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub


When the user clicks the button, we want to display the contents of the selected file in the text box, and we’ll do that in the command button’s Click event handler, Command1_Click(). We’ll also call the button’s Click event handler to let the user open a file by double-clicking it in the file control:


Sub File1_DblClick()
Command1_Click
End Sub


When the user wants to open a file, we put together the file’s name and path this way:



Sub Command1_Click()
Dim FileName As String
On Error GoTo FileError
If (Right$(Dir1.Path, 1) = "\") Then
FileName = File1.Path & File1.FileName
Else
FileName = File1.Path & "\" & File1.FileName
End If
...


Then we simply open the file and display it in the text box, Text1:


Sub Command1_Click()
Dim FileName As String
On Error GoTo FileError
If (Right$(Dir1.Path, 1) = "\") Then
FileName = File1.Path & File1.FileName
Else
FileName = File1.Path & "\" & File1.FileName
End If

Open FileName For Input As #1
Text1.Text = Input$(LOF(1), #1)
Close #1
Exit Sub

FileError:
MsgBox "File Error!"
End Sub


That’s it—when you run the program, the user can use the file controls to open a file, as shown in Figure 17.13. Now we’re using the Visual Basic file controls.


Figure 17.13  Displaying a file using the Visual Basic file controls.
The code for this example is located in the filecontrols folder on this book’s accompanying CD-ROM.

Creating And Deleting Directories
You can create a new directory with the MkDir statement and remove a directory with the RmDir statement. For example, here’s how we create a new directory, C:\data, using MkDir when the user clicks a command button, Command1 (if the directory already exists, Visual Basic generates an error):


Private Sub Command1_Click()
MkDir "c:\data"
End Sub


Here’s another example. We remove the same directory using RmDir when the user clicks another command button, Command2:


Private Sub Command2_Click()
RmDir "c:\data"
End Sub


Changing Directories
To change the default directory (that is, the directory where Visual Basic will look for the files you want to work with if you don’t specify a path), use ChDir . Here’s an example where we change the default directory to C:\windows using ChDir when the user clicks a command button, Command1:


Private Sub Command1_Click()
ChDir "c:\windows"
End Sub






Previous
Table of Contents
Next






Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.



Wyszukiwarka

Podobne podstrony:
567 572
2014kwykład3część1Redox stud dpkid(567
572,24,artykul
01 (572)
572 (2)
demo cgi 567
Antyk kultura starożytnej Grecji i Rzymu (oraz innych d~567
565 567
2000 05 Szkoła konstruktorówid!572
567,8,artykul
567 francuskikrokdalej1

więcej podobnych podstron