Tips-A2Z home page


Sub-categories

C
CSS (2)
Delphi (33)
Html (6)
Javascript (1)
PHP (2)

Category  |   Discussion (0)Programming (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 > PHP
Variable functions: In some cases you might want a user to choose which function to apply on some values. You can use a variable to call the function. e.g.,

<?php 

function sum($v1, $v2) { return ($v1 + $v2); }

function minus($v1, $v2) { return ($v1 - $v2); }



$function = "sum";

print $function(10,5).'<br>';

$function = "minus";

print $function(10,5).'<br>';

?>

It's essential that you use the same number of parameters.   Kevin Solway (173)

– –– ——— –– –

Main > Technology > Computers > Programming > Javascript > Cross-browser compatibility
When setting an attribute of an html element (e.g. one created using document.createElement()) do not use the syntax:
element.attribute = value;
because it fails on some browsers, notably Konqueror as of 21/04/2007. Instead use the syntax:
element.setAttribute(attribute, value);   Laird Shaw (4)

– –– ——— –– –

Main > Technology > Computers > Programming > Html
- Use the alt="" attribute on the img tag.
- Do not use the <blink> tag, it is annoying.
- Always Specify a !DOCTYPE.
- Always have some sort of contact ie. email address.
- Use the same colours/layout for each page
(so people know they are on the same site).
- Always check all your links after you create
them to avoid having broken links.   [guest]

– –– ——— –– –

Main > Technology > Computers > Programming > Html
- Avoid using HTML Frames, they can be annoying
and search engines don't like them.
- Make sure you can get to the home page of your
site from anywhere on your site.
- Try not to use any popups for advertisment,
everyone hates them.
- Always keep you site nice and simple to use.   [guest]

– –– ——— –– –

Main > Technology > Computers > Programming > Html
The best way to learn HTML is by example, and your browser is your best teacher! Whenever you are surfing and see something you like, click on your browser's choice to View Source.   [guest]

– –– ——— –– –

Main > Technology > Computers > Programming > Html
Validate your html to make sure it conforms to the standards by using an online service. link    [guest]

– –– ——— –– –

Main > Technology > Computers > Programming > Html
The best way to control your layout and spacing of elements is to use CSS (style sheets).   [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 > PHP
Slashes and quotes: Make sure you thoroughly test your PHP code when it displays text that may contain quotes and slashes (such as "John's/Jonathan's"). This should be tested both with "magic_quotes" turned on, and also with it turned off. Very few sites get this right, and you will often see displayed text, such as product names, which contain any number of slashes that shouldn't be there.   Kevin Solway (173)

– –– ——— –– –

Main > Technology > Computers > Programming > Html
Use percentages as widths in order to influence the layout in a manner compatible with variable screen sizes. For example, set a table to a width of 90%, rather than a width of, say, 600 (pixels).   Kevin Solway (173)

– –– ——— –– –

Main > Technology > Computers > Programming
Virtual machines: Modern computers are now fast enough, and have enough memory, to do your programming inside a virtual machine on your computer. This helps to isolate your programming environment from anything else you are doing on your computer, which can make your life easier. A bug in an experimental program on the virtual machine will not be able to wreak havoc on the rest of your files.   Kevin Solway (173)

– –– ——— –– –

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 > CSS
General CSS tips:
  • Keep it Simple: complex code will cause you a lot of trouble in the long-run. Concentrate on usability rather than fancy colors.
  • Margins and padding: take care with these on all elements that you use, as different browsers will use different default amounts of margin and padding.
  • Don't use "divs" for everything when a HTML element will do (eg, e.g. p, h1, h2, h3, h4, h5, h6, ul, ol, dl etc...)
  • Validate your CSS code: link    Kevin Solway (173)
  • – –– ——— –– –

    Main > Technology > Computers > Programming > CSS
    Useful snippets:
  • Fancy table borders: <table style=”border: 2px blue”>   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 48 users online :: 0 registered and 48 guests (based on users active over the past 5 minutes)