Re: TDriveComboBox and IO error 21

Giganews Newsgroups
Subject: Re: TDriveComboBox and IO error 21
Posted by:  Peter Below (TeamB) (none)
Date: 24 Sep 2006

Ciarán Ó Duibhín wrote:

> I'm writing an application where the user should be able to change
> drive, using TDriveComboBox.
>
> When the ComboBox is displayed, it shows the floppy and CD drives, as
> well as hard drives.  That is fine, but if the user selects either
> the floppy drive or the CD drive, and it happens to be empty, the
> application crashes with I/O error 21.  Presumably this results when
> the application tries to list the contents of the selected drive, in
> the TDirectoryListBox which is the value of the ComboBox' Dirlist
> property.
>
> How can I intercept this error?

If you want to keep the standard setup of connecting the components
through the DirList property of the drivecombobox you would have to
modify the TDirectoryListbox class, or find a replacement that handles
the problem better. I'm sure the JEDI VCL has something appropriate.

But you can also fix it in code. Do *not* set the DirList property to
the directorylistbox, leave the property empty. Instead you attach a
handler to the drivecomboboxes OnChange event, that looks somewhat like
this:

procedure TForm1.DriveComboBox1Change(Sender: TObject);
var
  OldErrMode: DWORD;
begin
  try
    OldErrMode := SetErrorMode(SEM_FAILCRITICALERRORS);
    try
      DirectoryListBox1.Drive := DriveCombobox1.Drive;
    finally
      SetErrorMode(OldErrMode);
    end;
  except on E: EInOutError do begin
      Display('%s: %s', [E.Classname, E.Message]);
      DriveCombobox1.Drive := 'C';
    end;
end;
end;

Display is a method of the form that show a message, in my case it is
added to a memo, but you can of course simply show a message dialog or
swallow the error completely. I would show a message, though, since it
would be confusing to the user if the controls just go back to showing
the content of C: when he tries to pick a drive with no disk inserted.

Note that hardwiring 'C' as the default drive may not be a good idea.
You may want to take the current drive at the program start instead
(the first letter of the string returned by GetDir(0, path), or the
first letter of the programs home path (Application.Exename). 'Ware UNC
pathes!).

--
Peter Below (TeamB)
Don't be a vampire http://slash7.com/pages/vampires),
use the newsgroup archives :
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be

Replies

In response to

TDriveComboBox and IO error 21 posted by Ciarán Ó Duibhín on Sat, 23 Sep 2006