Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

[delphi] Drivecombobox I/O ERROR 21

Status
Not open for further replies.

Flisk

Programmer
Oct 8, 2004
11
SE
hello, i'm using a drivecombobox (the win3.1 tab) in delphi, and it report a error message if i go to a: or cdrom that doesn't have a media in it. and display 'I/O ERROR 21'. does anyone know how to remove drives like a: cdrom from drivecombobox so you never can select those , or how to skip so the message doesn't show up

any code that fix my problem are appreciated
:)

(using delphi 7.0)

sorry for my bad english, hope you understand

 
You can do this is by stopping Windows from seeing the error As in the following call and procedure

This would be in the onchange method of your drive combo box
you dont have to show the message you can just ignore it if you like.

Code:
if not DiskInDrive( currDir[1] ) then
     ShowMessage( 'No Disk in: ' + currDir )

Which calls this function, Windows errors are turned off by seterror mode call
Code:
function DiskInDrive(Drive: Char): Boolean;
// Check to see if a disk is in the drive specified.
var ErrorMode: word;
begin
   // make it upper case
   if Drive in ['a'..'z'] then Dec(Drive, $20);
   { make sure it's a letter }
   if not (Drive in ['A'..'Z']) then
      raise EConvertError.Create('Not a valid drive ID');
   // turn off critical errors
   ErrorMode := SetErrorMode(SEM_FailCriticalErrors);
   try
    { drive 1 = a, 2 = b, 3 = c, etc. }
     if DiskSize(Ord(Drive) - $40) = -1 then
        Result := False
     else
        Result := True;
   finally
      { restore old error mode }
      SetErrorMode(ErrorMode);
   end;
end;

Steve:
A Delphi Programmer
A Feersum Endjinn indeed
 
i didn't get this code to work, but i find another one when i googled on it, so thanks anyway :)

this is the code i use to get it to work like i wanted

--------------------------------------------------------

If you use drivecombobox component in your applications, probably you are in doubt with the I/O error (21). This error occurs when there is no floppy in drive (for example a:) or no disc in cd drives. To avoid this error the code below works.



First of all, if you also use directorylistbox then do not link drivecombobox and directorylistbox. Just do it by adding one extra code into the onchange method of drivecombobox .

Directorylistbox1.drive:=Drivecombobox1.drive;

With this code when you change drives, the directory list updates according to the drive letter given in the onchange method of drivecombobox.



function TForm1.DiskAvail(Drive: Char): Boolean;
var
err_mod: Word;
begin
err_mod := SetErrorMode(SEM_FAILCRITICALERRORS);
//critical errors will not displayed

try
if DiskSize(Ord(Drive) - $40) = - 1 then begin
Result := False; end
else
Result := True;
finally
err_mod := SetErrorMode(err_mod); //reset
end;
end;

After writing the diskavail function which controls whether there is a disk in the drive or not, put the code given below into the onchange method of drivecombobox

procedure TForm1.DriveComboBox1Change(Sender: TObject);
var CurrentDrive: String;
begin
CurrentDrive := DriveComboBox1.Drive + ':';
if not (DiskAvail(CurrentDrive[1])) then
begin showmessage('There is no disk in drive '+CurrentDrive);
DriveComboBox1.Drive := 'C';
end;
Directorylistbox1.drive:=Drivecombobox1.drive;
end;
 
Hmm the code I posted is from a working application and the code you found does pretty much the same thing i.e it disables the windows messages in the same way. and detects the drive status in the same way.
The only difference I can see is that my code validates the drive letter passed in.



Steve:
A Delphi Programmer
A Feersum Endjinn indeed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top