Tips-A2Z home page


Sub-categories

Algorithms (3)
Delphi Code snippets (9)
Releasing
Video tutorials (1)

Category  |   Discussion (0)Delphi (General)

Main > Technology > Computers > Programming > Delphi
Vista-ready: Old Delphi Applications can be upgraded for use with Windows Vista through the use of a Task Dialog unit ( link ), a tool like "xptheme", and, if necessary, a manifest file to instruct Vista's User Account Control system what kind of privileges your application requires. More info on Vista and Delphi is here: link .   [guest]

– –– ——— –– –

Main > Technology > Computers > Programming > Delphi
Auto-complete: To automatically create an implementation stub for newly declared type procedures, hover the mouse over the procedure's parameter until it shows as a link, then press Shift-Ctl-C. E.g. procedure SetFilename(const Value: string) // 'Value' is a parameter.   [guest]

– –– ——— –– –

Main > Technology > Computers > Programming > Delphi
Save run-time memory: Under Tools/Options/VCL Explorer, uncheck 'Autocreate forms'. In the program (*.exe) delete any other Application.CreateFormX other than the MainForm. Now declare any unit variables in the Main Form as global variables, instead of in child units, and create/destroy them in the Main Form. This means instances are created only when needed. Otherwise, they'll all be created at run-time, then continue to suck up resources while remaining hidden.   [guest]

– –– ——— –– –

Main > Technology > Computers > Programming > Delphi
To make non-resizeable dialog boxes, change the Object Explorer BorderStyle property to BsDialog.   [guest]

– –– ——— –– –

Main > Technology > Computers > Programming > Delphi
Child classes inherit parent functionality, unless the common function ends with '; virtual;' in the parent, and '; override;' in the child.   [guest]

– –– ——— –– –

Main > Technology > Computers > Programming > Delphi
A variable of a parent class can actually be created in implementation as a child class. This is called polymorphing. For example,
var SomePerson: TPerson;
// where TPerson is the parent class
begin
SomePerson := TEmployee.Create
// where TEmployee is the child class
But the polymorph variable cannot use child functions that the parent doesn't have.   [guest]

– –– ——— –– –

Main > Technology > Computers > Programming > Delphi
Grandchildren without 'overrides' take the child's functionality. The rendering works up the inheritance chain from youngest to the base class (the parent).   [guest]

– –– ——— –– –

Main > Technology > Computers > Programming > Delphi
Visibility: classes' members are divided into private, public, protected, and published:
  • Private: visible to other members in the same class or other classes in the same unit;

  • Public: visible to other members in the same class, descendents of the same class, and other users of the unit;

  • Protected: visible to members in the same class, and descendents only;

  • Published: run-time type information, visible only at run-time, e.g. what the IDE can see in the object inspector.
   [guest]

– –– ——— –– –

Main > Technology > Computers > Programming > Delphi
Additional visibility: 'strict private' and 'strict protected'. This means no friendship between classes in the same unit.   [guest]

– –– ——— –– –

Main > Technology > Computers > Programming > Delphi
Object Inspector: arrange items alphabetically by right-clicking on OI and choosing arrange by name.   [guest]

– –– ——— –– –

Main > Technology > Computers > Programming > Delphi
Right-click the executable program in file structure menu to view source.   [guest]

– –– ——— –– –

Main > Technology > Computers > Programming > Delphi
When making dialog boxes modal, make sure the Object Inspector property 'ModalResult' is set to mrOk.   [guest]

– –– ——— –– –

Main > Technology > Computers > Programming > Delphi
Scroll-bars: add these to a form by selecting ssBoth in the Object Inspector ScrollBars property.   [guest]

– –– ——— –– –

Main > Technology > Computers > Programming > Delphi
Menu Item dropdown list separator bar: Using the ActionList component, click on a new item space in the mini-designer, then enter a dash (-) in the Object Inspector Caption property.   [guest]

– –– ——— –– –

Main > Technology > Computers > Programming > Delphi
To help tell the difference between objects, prefix names with letters:
T = Type
F = Field
A = Parameter
U = Unit
frm = form   [guest]

– –– ——— –– –

Main > Technology > Computers > Programming > Delphi
Another short-cut: Shift between interface and implementation declarations with Ctl+Shift+arrows.   [guest]

– –– ——— –– –

Main > Technology > Computers > Programming > Delphi
Dynamic components: You don't have to drop components on your form, but can create them dynamically. e.g., in the general case, component := tcomponent.create(self);
or in the specific case,
btn := tbutton.create(self);
This provides advantages when you want to compile your program on different machines, or when you want to use different versions of the same component. This method can also be used to access third party components in Turbo Delphi (ie, the free version of Delphi), which doesn't allow the installing of components into the IDE.   Kevin Solway (173)

– –– ——— –– –

Main > Technology > Computers > Programming > Delphi
Creating short-cuts:
- Prefix the desired short-cut letter of a caption with the symbol '&' , in the Object Inspector.
E.g. Button1 / Caption: &Delete
or
- Using an ActionList (non-visual component), choose a ShortCut for the list item from the drop-down list in the Object Inspector, or type in one of your own creation.   kellyjones00 (593)

– –– ——— –– –

Main > Technology > Computers > Programming > Delphi > Algorithms
Shell sort: An efficient way to sort up to a couple of hundred items.
Procedure ShellSort(var a: array of Word);

var

bis, i, j, k: LongInt;

h: Word;

begin

bis := High(a);

k := bis shr 1;// div 2

while k > 0 do

begin

for i := 0 to bis - k do

begin

j := i;

while (j >= 0) and (a[j] > a[j + k]) do

begin

h := a[j];

a[j] := a[j + k];

a[j + k] := h;

if j > k then

Dec(j, k)

else

j := 0;

end; // {end while]

end; // { end for}

k := k shr 1; // div 2

end; // {end while}



end;
   Kevin Solway (173)

– –– ——— –– –

Main > Technology > Computers > Programming > Delphi > Algorithms
Quick sort: Efficient sorting of large numbers of items.
procedure QuickSort(var A: array of Integer; 

iLo, iHi: Integer) ;

var

Lo, Hi, Pivot, T: Integer;

begin

Lo := iLo;

Hi := iHi;

Pivot := A[(Lo + Hi) div 2];

repeat

while A[Lo] < Pivot do Inc(Lo) ;

while A[Hi] > Pivot do Dec(Hi) ;

if Lo <= Hi then

begin

T := A[Lo];

A[Lo] := A[Hi];

A[Hi] := T;

Inc(Lo) ;

Dec(Hi) ;

end;

until Lo > Hi;

if Hi > iLo then QuickSort(A, iLo, Hi) ;

if Lo < iHi then QuickSort(A, Lo, iHi) ;

end;
   Kevin Solway (173)

– –– ——— –– –

Main > Technology > Computers > Programming > Delphi > Delphi Code snippets
Converting strings to and from Pchars:
Use"StrPCopy" to convert a string into a pchar.
function StrPCopy(Dest: PChar; 

const Source: string): PChar;

Use "StrPas" to convert a pchar into a string.
function StrPas(Str: PChar): string;
   Kevin Solway (173)

– –– ——— –– –

Main > Technology > Computers > Programming > Delphi
Some keyboard shortcuts:
  • <ctrl><shift><i> : indent selected rows
  • <ctrl><shift><u> : unindent selected rows
  • <ctrl><shift><0..9> : set or remove the bookmark 0..9.
  • <ctrl><0..9> : jump to bookmark 0..9 in the currently opened file.
  • <ctrl><shift><g> : create a new GUID.
  • <ctrl>t : delete word
  • hold <shift> key down : then use arrows, home and end keys, to select text.
  • <ctrl><home> : jump to start of file
  • <ctrl><end> : jump to end of file
  • F12 : switch between form and code   Kevin Solway (173)
  • – –– ——— –– –

    Main > Technology > Computers > Programming > Delphi > Delphi Code snippets
    Some one-liners:
  • Display last system error message:
    ShowMessage(SysErrorMessage(GetLastError()));

  • Convert a number to currency:
    FloatToStrF(Value, ffCurrency, 15, CurrencyDecimals); // "15" is the precision, where "Value" is a "Double"




  • File name of application:
    ExtractFileName(Application.ExeName);

  • Path of application:
    ExtractFilePath(Application.ExeName);

  • Let other applications use the CPU when you're in a loop:
    Application.ProcessMessages;
       Kevin Solway (173)
  • – –– ——— –– –

    Main > Technology > Computers > Programming > Delphi > Delphi Code snippets
    Running on Vista?
    function IsWindowsVista: Boolean;   

    var VerInfo: TOSVersioninfo;

    begin

    VerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);

    GetVersionEx(VerInfo);

    Result := VerInfo.dwMajorVersion >= 6;

    end;
       Kevin Solway (173)

    – –– ——— –– –

    Main > Technology > Computers > Programming > Delphi > Algorithms
    Rounding: The built-in round function might not always return the result you want. For example: Round(25.5) = 26, but Round(26.5) = 26.

    It is called a "banker's round", and rounds to the nearest even number. For a large number of operations, there is no bias.

    The following function will round-up if the fractional part >= 0.5:
    function RoundN(x: Extended): LongInt;

    begin

    Result := Int(X) + Int(Frac(X) * 2);

    end;

    The following function will round (as per RoundN), but to a certain number of decimal places:
    function RoundD(x: Extended; d: Integer): Extended;

    // RoundD(123.456, 0) = 123.00

    // RoundD(123.456, 2) = 123.46

    // RoundD(123456, -3) = 123000

    var n: Extended;

    begin

    n := IntPower(10, d);

    x := x * n;

    Result := (Int(x) + Int(Frac(x) * 2)) / n;

    end;
       Kevin Solway (173)

    – –– ——— –– –

    Main > Technology > Computers > Programming > Delphi
    Moving and sizing a component precisely:
  • To move a component to an exact spot, select the component, then press and hold the CONTROL key, then use the cursor keys to move the component in 1 dot increments.
  • To resize a component precisely, select the component, then press and hold the SHIFT key, then use the cursor keys to resize the component in 1 pixel increments.   Kevin Solway (173)
  • – –– ——— –– –

    Main > Technology > Computers > Programming > Delphi > Video tutorials
    The best free video tutorial series for Delphi beginners is by Nick Hodges: link . It is far shorter and clearer, being more principle-driven, than the 3DBuzz series link .   kellyjones00 (593)

    – –– ——— –– –

    Main > Technology > Computers > Programming > Delphi > Delphi Code snippets
    2 ways to do 'if and, then':
    if (SentenceAge >= '18') then

    if (SentenceAge <= '20') then

    Result := IntToStr(TrQi4) + ' - ' + IntToStr(TrB);


    if (SentenceAge >= '18') and (SentenceAge <= '20') then

    Result := IntToStr(TrQi4) + ' - ' + IntToStr(TrB);
       kellyjones00 (593)

    – –– ——— –– –

    Main > Technology > Computers > Programming > Delphi > Delphi Code snippets
    Horizontal scrollbar for listbox
    listbox.Perform(LB_SETHORIZONTALEXTENT, 900, 0);
       thesource (378)

    – –– ——— –– –

    Main > Technology > Computers > Programming > Delphi > Delphi Code snippets
    Array of byte to string, and string to array of byte:
    // string to array

    StrPLCopy(pansichar(@arr), str, sizeof(arr));



    // array to str

    procedure AnsiStringFromBuf(var S: AnsiString; Buffer: PAnsiChar; Len: Integer);

    // Usage: ansistringfrombuf(str, pansichar(@arr), sizeof(arr));

    begin

    setstring(s, buffer, len);

    setlength(s, strlen(buffer));

    end;
       thesource (378)

    – –– ——— –– –

    Main > Technology > Computers > Programming > Delphi > Delphi Code snippets
    Detect when system is shutting down:
      private

    procedure EndSessionMsg(var Msg : TMessage); message WM_QUERYENDSESSION;





    procedure TForm1.EndSessionMsg(var Msg : TMessage);

    begin

    if backingupinprocess then Msg.Result := 0; // prevent system from shutting down or logging off

    Msg.result := 0;

    if Msg.lparam = 0 then

    begin

    ShowMessage('System is shutting down');

    end

    else if (DWORD(Msg.lparam) and ENDSESSION_LOGOFF) = ENDSESSION_LOGOFF then

    begin

    ShowMessage('User is logging off');

    end;

    end; // EndSessionMsg
       thesource (378)

    – –– ——— –– –

    Main > Technology > Computers > Programming > Delphi > Delphi Code snippets
    Focus a combobox:

    Normally, you can focus a component by using its "setfocus" method. However, a combobox requires the use of a different method.
    procedure TForm1.focuscombobox(combobox : tcombobox);

    var editHWnd : HWND;

    begin

    editHWnd := FindWindowEx(Combobox.handle, 0, nil, nil); // get the handle of the edit component

    if editHWnd <> 0 then windows.setfocus(editHWnd);

    end;
       thesource (378)

    – –– ——— –– –

    Main > Technology > Computers > Programming > Delphi > Delphi Code snippets
    Reading old versions of unpacked records:

    Different versions of delphi use a different number of bytes to align record fields. Using the following compiler directive may help in reading records that were used with older compilers.

    {$A4} // Set alignment to 4 bytes, for reading old format (Delphi 4) unpacked records

    As a rule, when creating the declarations for records, you should specify the use of packed records. For example:

    TPackedRecord = Packed Record
    . . .
    end;

    This future-proofs the record in the case that future versions of Delphi use a different number of bytes for field alignment.

    There may be some cost in performance, depending on the number and size of records, since unpacked records can be accessed faster.   thesource (378)

    – –– ——— –– –


     


     

    To post a new tip, sign up for a free account.
    (Unfortunately this is a necessary spam prevention measure)

    Who is online
    In total there are 45 users online :: 0 registered and 45 guests (based on users active over the past 5 minutes)