HOME > > TUTORIALS TABLE OF CONTENTS - - - / - - / - - / - - / - - - Other material for programmers

Delphi tutorial: Re-creating parts of Windows Explorer. Multiple windows. (Level 4)

This has good information, and there's a search button at the bottom of the page.

Please don't dismiss it because it isn't full of graphics, scripts, cookies, etc!

Click here if you want to know more about the source and format of these pages.

IGNORE ANY PERIODS (.) AT THE START OF ANY LINE


Dt4d: A start towards a Windows Explorer-like program

The ideas in this are not especially arcane. Some of the matters raised are relatively central to Delphi programming. Even so, I've put it in Level 4. We are going to see some inter-action between objects, which might be a little alarming for people who are just getting started.

This tutorial was written using Delphi 2. SOme of the components used may not be available to Delphi 1 programmers. Some unusual good news for you: The source code for this will be posted in a zip file.

It is in two parts. The first shows you how to build Windows Explorer-like access to your backing store, and how to pick a single file from it. Part Two shows a way display the file's contents. It also shows the use of multiple windows.

Part One: Navigating Your Backing Store

I once upon a time used the classic Compuserve email software. (Too) many people (still) send their emails twice, once as plain text, and a second time in an HTML version. I don't like the HTML version, rarely look at it, and I don't often open attachments. Thus, under Compuserve, I ended up with dozens of mystery files. I would drag them into Notepad, see if I see anything important, and then delete 99% of them. This was tedious! I wanted a better way of weeding my disc. What follows is not a complete solution to that problem, but maybe if you know where I was going, it will help you follow the tutorial.

The program's main form is similar to Windows Explorer, but not as good. It lets you see what is on a floppy or hard disc. You can then select files an open them up to see their contents. (In the tutorial, the examination of the contents is not very sophisticated, but the basic idea is covered.) After you've looked at a file, you can add it to a list of files to be deleted later, or simply go on to check another file. When you're done for the day, you can delete the files whose names you've been accumulating.

Start a new application in the usual way. Name the form DD13f1 (Delphi Demo). From the System tab of the component pallette, add to your form one each of the following:
DirectoryListBox FileListBox

(You can leave them with the names assigned by Delphi). Save (in it's own folder) what you have so far. Call the current unit DD13u1, and the project DD13.

Create an OnDirectoryListBox1Change event handler consisting simply of:
FileListBox1.directory:=DirectoryListBox1.directory;

.... and try running the embryonic application. If you click in the DirectoryListBox, you should be able to change which directory you are in, and when you do, the list of files in the FileListBox should change. (Don't worry if folders (directories) are not showing in the FileListBox). This is pretty cool. If you don't believe me, try programming the functionality we have so far without using either of the components I've specified. The more I work with Delphi, the more I find that it is really easy (that's to good news) IF (that's the bad news) you can find (amongst the hundreds... thousands?... of available bits) the thing you need. I'm also learning not to fight Redmond. Don't try to do things YOUR way. Try to figure out how Bill wants it done, and then find the tools to do it that way.... and when you've managed those two hard things, you'll find you've reached the place where things are easy!!

Now we're going to more or less repeat the above. First put a formatted floppy in your A: drive. Now, also from the System tab, add a DriveComboBox. To make it work, give it an OnDriveComboBox1Change handler consisting of...
DirectoryListBox1.drive:=DriveComboBox1.drive; FileListBox1.drive:=DriveComboBox1.drive;

The above works fine, and is not "too" clever. If you like being clever, there is a property of the DirectoryListBox you may want to use: FileList. If you set that to FileListBox1, then you can leave out the FileListBox1.drive... line in the OnDriveComboBox1Change code above. No doubt there are other such tricks available if you sek them out.

An aside: The program, as developed so far, doesn't cope well if you click on a drive which doesn't have a disc in it. I'm afraid I haven't sorted out what you'd need to add to a commercial application.

Also from the System tab, add a FilterComboBox1, and, surprise, surprise an event handler:
FileListBox1.mask:=FilterComboBox1.mask;

The Filter property of the FilterComboBox determines what filters are available to your user. Doubleclick on the cell beside "Filter" in the Object Inspector to access an editor for the Filter property. As an exercise: Try to add to the list an entry with name: "Text files (*.txt)", filter: "*.txt"

So far so good? This would be a good place to save the project, by the way.

"Yes, but it doesn't DO anything", I hear you cry! ("Yes, yes, but you don't go"... who said that?)

Add a label to the form. Then add an OnFileListBox1Click event handler
label1.caption:=FileListBox1.filename;

Now when you click on, or use the cursor keys to move between, file names, the name of the file appears in the label.

You do what you like with the file! I've given you access to your backing store, and showed you how to pick a file from it. The rest of this tutorial addresses just one of many things you might do with what you have so far.

Part Two: Multiple Windows, Reading Files



This application doesn't NEED multiple Windows, but I wanted to have them. We're going to add one window for the display of files, and another to hold a list of files which we have decided to delete in a minute. The "in a minute" approach allows the user to be put through an "Are you sure?" check, but only one, not one for each file to be deleted.

So: create two new windows for the application by....

....using File|New Form. Name the new form FileViewer. Save the project, naming the new unit DD13FVu

Repeat the process, naming the third form ToDelete. Adjust sizes and positions to taste. Resave the project, naming the third unit DD13TDu.

As we now have three forms (windows), and each has their own code (unit), the screen can get a bit cluttered! The code is in a nice tabbed window. If you can get to the code for one, you can switch to another just by clicking on the relevant tab. If you are looking at the code for a form you want to access, just press F12. If you are looking at a form and need its code: F12 will work for that, too.

Put a label across the top of the FileView window. "Label1" will do for a name. (In general, using meaningful names is wise, but where you know there are going to be few of any particular type of component, you can be lazy.)

Go back to the already existing (DD13f1) OnFileListBox1Click event handler. Add
FileViewer.show; FileViewer.label1.caption:=FileListBox1.filename;

You also need to add DD13FVu to the Uses clause near the top of DD13u1.

It is inefficient to do a FileViewer.show every time you change the file selected, but I couldn't find a better place to put it!

To FileViewer add a button. Call it buExamine. Also add a large memo. Add the following as buExamine's OnClick handler:
procedure TFileViewer.buExamineClick(Sender: TObject);
var dfFile:file of byte;
  c1,c2:integer;
  bTmp:byte;
  sTmp:string;
begin
assignfile(dfFile,Label1.caption);
reset(dfFile);
memo1.clear;
for c1:=0 to 15 do begin(*c1*)
sTmp:='';
for c2:=0 to 60 do begin
if not(eof(dfFile)) then begin
  read(dfFile,bTmp);
  if (bTmp<>31) and (bTmp<127) then
   sTmp:=sTmp+chr(bTmp);
  end;
end;
memo1.lines.add(sTmp);
end;(*c1*)
closefile(dfFile);
end;
That should "work", but adding FileViewer.memo1.clear; to the OnFileListBox1Click handler in DD13f1 is a good idea. Don't, by the way, attempt to use this program to view the contents of DD13.exe while it is running. It will raise an error.

The file viewer is nothing special!! It is just something basic you can refine to meet your needs!

Add another button to DD13FileViewer. Call it buAddToDeleteList. In minute, we'll give it an OnClick handler, but first add a memo to ToDelete. Make it quite wide. Set its Wrd Wrap property to false. (Explained later.) Go to the new memo's Lines peoperty via the Object Inspector. Double click on the cell beside "Lines" to bring up the String List Editor. Remove everything from the memo... be sure to achieve "0 lines" in the upper left of the window. Also, add ToDelete.show to DD13f1.FileListBox1Click. It can go just before the FileViewer.show; which is there already. Add DD13TDu to DD13f1's Uses clause.

An aside. (You can skip this if you already make a lot of use of CtrlC/ Ctrl-V.) Use Copy/ Paste a lot. It avoids typos, takes the pain out of long names, etc. To add DeleteList.show to DD13f1.FileListBox1Click, all you need to do is...
Put your cursor just in front of FileViewer.show
Press and keep down the shift key
Press the down arrow key once
(The effect of the previous two lines can also be accomplished by a drag of the mouse.) Release the shift key
Do Ctrl-C, Ctrl-V, Ctrl-V
Select the first FileViewer
Type DeleteTo

Odd at first, but eventually your fingers will fly! (End of aside)

Add DD13TDu to FileViewer's Uses clause. Now you can build FileViewer.buAddToDeleteListClick, which is simply...
ToDelete.memo1.lines.add(Label1.caption);

That takes care of almost everything! All that is left is a way to delete whatever files have been listed on ToDelete.

Put a buDeleteThese button on ToDelete. The following is suitable for the OnClick:
begin
if MessageDlg('Last chance! Abort delete?',
    mtInformation, [mbNo, mbYes], 0) = mrNo
      (*Reversing mbNo/Yes does not resquence them
        in message box*)
then begin (*Delete the files*)
   while memo1.lines.count>0 do begin
     deletefile(memo1.lines[0]);
     memo1.lines.delete(0);
     end;(*while..*)
   end; (*Delete the files*)
end;

BE CAREFUL!!!! You now have a "live" file delete capability. I used Notepad to create a bunch of files I could safely examine and/or delete.


That should do it!

One little chuckle for you. The final part of this, the buDeleteTheseClick handler, was simple copied from a working elaboration of the program in this tutorial. I was late for getting away to something, but just wanted to get the job DONE! Pasted the code in, ran the program, it "worked"... but the files weren't deleted! No error messages. The memo was cleared properly. So why no file deletes? Eventually, I noticed that my memo was narrow and I had not set WordWrap to false. In the original, the memo was wide. Thus, before I fixed things, the program was trying to delete things like...

C:\Program Fil
es\Borland\aTmpFi
le.txt

... which the memo had split into three lines, and, not surprisingly, attempting Deletefile('C:\Program Fil') resulted in no deletion!

Oh, the joys of programming....

There is a crudeness in this: After you've deleted the files, they are still listed in the FileListBox. I'm sure there's some way to overcome that, but I leave that to you.... :-)

Anyway... I hope that was useful, and that you won't hesitate to bring errors to my attention.

At one point, I had a zip file with the necessary sourcecode. That is for the moment lost (Compuserve discontinued its web hosting, and just dumped what I had on their server. Of course, that is no exuse for the lapse in my local backup procedures.) I will try to replace the zip one day. Anyone really interested?

            powered by FreeFind
  Site search Web search
Site Map    What's New    Search This search merely looks for the words you enter. It won't answer "Where can I download InpOut32?"
The search engine is not intelligent. It merely seeks the words you specify. It will not do anything sensible with "What does the 'could not compile' error mean?" It will just return references to pages with "what", "does", "could", "not".... etc.

Also: This search only searches the material on one of my websites.

      Click here to visit another of my sites.

      Click here to visit my third site.




Click here if you're feeling kind! (Promotes my site via "Top100Borland")
Ad from page's editor: Yes.. I do enjoy compiling these things for you. I hope they are helpful. However... this doesn't pay my bills!!! Sheepdog Software (tm) is supposed to help do that, so if you found this stuff useful, (and you run a Windows or MS-DOS PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.

Link to Tutorials main page
Here is how you can contact this page's author, Tom Boyd.


Valid HTML 4.01 Transitional Page WILL BE tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org


If this page causes a script to run, why? Because of things like Google panels, and the code for the search button. Why do I mention scripts? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....