Delphi: Difference between revisions

From miki
Jump to navigation Jump to search
(New page: A meager attempt to recover my old knowledge on Delphi 7... == References == * The ultimate reference on Delphi with lots of examples at [http://delphi.about.com] == Keyboard shortcuts =...)
 
Line 56: Line 56:


== Dialog box ==
== Dialog box ==
* Use '''InputBox''' to bring up an input dialog box ready for the user to enter a string in its edit box.
* Use '''InputBox''' to bring up an input dialog box ready for the user to enter a string in its edit box. '''InputQuery''' offers similar functionality.
<div style="padding-left:2em;"><source lang="delphi">
<div style="padding-left:2em;"><source lang="delphi">
procedure TForm1.Button1Click(Sender: TObject);
procedure TForm1.Button1Click(Sender: TObject);
Line 65: Line 65:
* Use '''MessageDlg''' to display a message dialog box in the center of the screen
* Use '''MessageDlg''' to display a message dialog box in the center of the screen
* Use '''ShowMessage''' to display a message box with an OK button.
* Use '''ShowMessage''' to display a message box with an OK button.
* See help on ''QDialogs routines'' for more.


== TStringGrid, TDBGrid ==
== TStringGrid, TDBGrid ==

Revision as of 11:07, 9 March 2009

A meager attempt to recover my old knowledge on Delphi 7...

References

  • The ultimate reference on Delphi with lots of examples at [1]

Keyboard shortcuts

Some frequently used keyboard shortcuts that any decent programmer should always use:

Shortcut Description
Ctrl+Shift+↑ Go to method declaration
Ctrl+Shift+↓ Go to method definition
Ctrl+Shift-C Auto-complete class (eg. use it when declaring new class method, and cursor still on method declaration)
Ctrl+J Insert code template
Ctrl+Space Code auto-complete
Ctrl+K+I Indent code
Ctrl+K+U Unindent code
Ctrl+left click Go to identifier definition

Basics

Some very basic stuff, but which are particular to Delphi language.

  • Class object are not created by default. One must call the constructor explicitly...
type
  TMyClass = class
  public
    constructor Create;
  end;

var
  myObject: TMyClass;

procedure TFrmForm1.FormCreate(Sender: TObject);
begin
  myObject := TMyClass.Create();        // objects are assigned result of constructor call!
end;

Dialog box

  • Use InputBox to bring up an input dialog box ready for the user to enter a string in its edit box. InputQuery offers similar functionality.
procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption:=InputBox('Question', 'Enter string', 'Default');
end;
  • Use MessageDlg to display a message dialog box in the center of the screen
  • Use ShowMessage to display a message box with an OK button.
  • See help on QDialogs routines for more.

TStringGrid, TDBGrid

  • Check this great post to know how to use colors in TDBGrid [2].
  • Here an example on how to format cells in TStringGrid. Use the OnDrawCell event. Note that property DefaultDrawing is set to True so that cells are drawn with default formatting (incl. focus and background).
procedure TFrmMultiply.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  if(ACol=5) then
  begin
    if(StringGrid1.Cells[6,ARow]='') then
    begin
      StringGrid1.Canvas.Font.Color := clGreen;
      StringGrid1.Canvas.Font.Style := [fsBold,fsStrikeOut]
    end;
    //Redraw the current cell - code took from TStringGrid.DrawCell
    StringGrid1.Canvas.TextRect(Rect, Rect.Left+2, Rect.Top+2, StringGrid1.Cells[ACol, ARow]);
  end;
end;

TEdit

  • Use OnKeyPress event to filter or react to some keypress
procedure TFrmForm1.Edit1KeyPress(Sender: Tobject; var Key: Char);
begin
  if( Key = #13 ) then
  begin
    //Some code here when Enter key is pressed
  end;
  if( not(Key in [#8, '0'..'9']) ) then Key:=#0;     //Ignore all key presses but Tab or digits