Pages

Saturday, December 31, 2011

How to create close confirmation in delphi

This post will discuss about how to create a dialog box confirmation when user want to close a form in delphi. Below is the code :
  if MessageDlg('Are you sure you want to close the form?', mtConfirmation,
    [mbOk, mbCancel], 0) = mrCancel then
      CanClose := False;
Paste that code on event OnCloseQuery of form.

Friday, December 30, 2011

How to format strtodatetime in delphi

Because of regional setting computer that depend on each computer, it will output format time as regional setting. If we want to format datetime as we want regardless regional setting, we can write the script as below :
var
  MySettings: TFormatSettings;
  s: string;
  d: TDateTime;
begin
  GetLocaleFormatSettings(GetUserDefaultLCID, MySettings);
  MySettings.DateSeparator := '-';
  MySettings.TimeSeparator := ':';
  MySettings.ShortDateFormat := 'mm-dd-yyyy';
  MySettings.ShortTimeFormat := 'hh:nn:ss';

  s := DateTimeToStr(Now, MySettings);
  ShowMessage(s);
  d := StrToDateTime(s, MySettings);
  ShowMessage(DateTimeToStr(d, MySettings));
end;

Thursday, December 29, 2011

How to convert tstringlist to comma separated string

For some reasons, we may need to convert TListString to a comma separated String. for example to create query that use 'in'. Below is the routine to do that :
var
   oSL : TStringlist;
   sBuffer : String;
begin
   oSL := TStringlist.Create;
   oSL.Add('A');
   oSL.Add('B');
   oSL.Add('C');
   sBuffer := Stringreplace(oSL.Text,Char(13)+Char(10),',',[rfReplaceAll]);
   Showmessage( sBuffer );
   oSL.Free;
end;
Don't Forget To Join Our Community
×
Widget