Delphi32.com - Home!
| Home/News | Downloads | Forums | D32 Magazine | Resources | Info and Facts |  
 
 DriveComboBox


May 6th

Ed Duffe
http://www.duffe.freeserve.co.uk

If you've ever used the DriveComboBox component from the Win3.1 page of the Component Palette, then you know what a pain it is to try to avoid an exception when you change to a drive that doesn't have a disk in it, such as A:\. Below is a function that I've found that overcomes the problem

UMain.pas

unit UMain;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, FileCtrl;

type
  TForm1 = class(TForm)
    DriveComboBox1: TDriveComboBox;
    procedure DriveComboBox1Change(Sender: TObject);
  private
    { Private declarations }
    function DiskInDrive(Drive: Char): Boolean;
  public
    { Public declarations }
  end;

const
  DefaultDrive = 'C:';

var
  Form1: TForm1;

implementation

{$R *.DFM}

{ TForm1 }

uses StrUtils;

function TForm1.DiskInDrive(Drive: Char): Boolean;
var
  ErrorMode: Word;
begin
  if Drive in ['a'..'z'] then
    Dec(Drive, $20);
    //make the drive uppercase
  if not (Drive in ['A'..'Z']) then
  //make sure it's a letter and, if it's not
    raise  //...raise an exception
      EConvertError.Create(Drive + ' is not a valid drive ID');

  ErrorMode := SetErrorMode(SEM_FAILCRITICALERRORS);
  //turn off critical errors

  try
    if DiskSize(Ord(Drive) - $40) = - 1 then
    //DiskSize(1) = a, DiskSize(2) = b etc..., DiskSize(- 1) = not a
                //valid drive
      Result := False else
      Result := True;
    finally
      ErrorMode := SetErrorMode(ErrorMode);
      //reset original error mode
  end;
end;

procedure TForm1.DriveComboBox1Change(Sender: TObject);
var
  CurrentDrive: String;
begin
  if DriveComboBox1.Drive = '' then
    CurrentDrive := DefaultDrive else
    CurrentDrive := DriveComboBox1.Drive + ':';

  if not (DiskInDrive(CurrentDrive[1])) then
  begin
    MessageDlg('There is no disk in drive ' + ChangeCase(CurrentDrive),
                mtError, [mbOK], 0);
    DriveComboBox1.Drive := 'C';
  end;
end;

end.




 
 Hits/month  2,500,000+ 
 Downloads
 (Since May 2000)
 7,393,709 
 Total Files  6,023 
 Forum msgs  7,670 
 Articles/FAQs  70+/900+ 
Kylix
Tips n Tricks
FAQs
Knowledge Base
Bug Listings
Articles
Books
Newsgroups
Links
Submissions
Testimonials
Advertising
Contact Us
About Us
Search Amazon:
Top Selling Software at Amazon

| Home/News | Downloads | Forums | Resources | Info and Facts | Testimonials |
  Site Search:
 


Comments/Problems: Webmaster@delphi32.com
Copyright © 1998-2006, Delphi32.com. All rights reserved.
Terms of Use