Code Samples

Various Code Samples that can be used in applications





Find any component on a Form or other control


function FindAnyControl(obj: TWinControl; s: string): TComponent;
var x: integer;
begin
Result := nil;
try
for x := 0 to obj.ComponentCount - 1 do begin
if (AnsiCompareText(s, obj.Components[x].Name) = 0) then
Result := obj.Components[x]
else if obj.Components[x] is TWinControl then
Result := FindAnyControl(TWinControl(obj.Components[x]), s);
if (Result <> nil) then
break;
end;
except
{ eat the exception because we don't care about it }
end
end; 
 
Go back to the top



Get width of a text in pixels


  Function GetWidthText(const Text:String; Font:TFont) : Integer;
var
  LBmp: TBitmap;
begin
  LBmp := TBitmap.Create;
  try
   LBmp.Canvas.Font := Font;
   Result := LBmp.Canvas.TextWidth(Text);
  finally
   LBmp.Free;
  end;
end;
Go back to the top



Get drives letters


procedure GetDriveLetters(AList: TStringList);
var
  vDrivesSize: Cardinal;
  vDrives    : array[0..128] of Char;
  vDrive     : PChar;
begin
  AList.BeginUpdate;
  try
    // clear the list from possible leftover from prior operations
    AList.Clear;
    vDrivesSize := GetLogicalDriveStrings(SizeOf(vDrives), vDrives);
    if vDrivesSize=0 then Exit;  // no drive found, no further processing needed

    vDrive := vDrives;
    while vDrive^ <> #0 do
    begin
      AList.Add(StrPas(vDrive));
      Inc(vDrive, SizeOf(vDrive));
    end;
  finally
    AList.EndUpdate;
  end;
end;
Go back to the top



Date for SQL - conversion of a regular TDate to SQL string date

function DateForSQL(const date : TDate) : string;
var
y, m, d : word;
begin
DecodeDate(date, y, m, d) ;
result := Format('#%.*d-%.*d-%.*d#',[4, y, 2, m, 2, d]) ;
end;
Go back to the top



If string contains white space

function ContainsWhiteSpace(const S: string): Boolean;
const
  cWhiteSpace = ' '#9#10#11#12#13;  // white space characters
begin
  Result := ContainsDelims(S, cWhiteSpace);
end;
Go back to the top



If string contains delimiters

function ContainsDelims(const S, Delimiters: string): Boolean;
var
  DelimIdx: Integer;  // loops thru delimiters
begin
  Result := False;
  for DelimIdx := 1 to Length(Delimiters) do
    if SysUtils.AnsiPos(Delimiters[DelimIdx], S) > 0 then
    begin
      Result := True;
      Break;
    end;
end;
Go back to the top



Capitalize the first letter in a string

function Capitalize(Texto:String): String;
var
OldStart: Integer;
begin
if Texto <> '' then
begin
Texto := UpperCase(Copy(Texto,1,1))+LowerCase(Copy(Texto,2, Length(Texto)));
Result := Texto;
end;
end;
Go back to the top


1 comment:

  1. Good,
    haow are you, thankyou for your big help
    juste a question, haw to see a time of VLCPlugin2 with Delphi
    thankyou

    ReplyDelete

Popular Posts

Recent Posts

Unordered List

Text Widget

Pages

Search This Blog

Powered by Blogger.

Contributors

Text Widget