Delphi: Difference between revisions
Jump to navigation
Jump to search
Line 66: | Line 66: | ||
* 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. |
* See help on ''QDialogs routines'' for more. |
||
* Add a ''Don't show this message again'' to a standard dialog box: |
|||
<div style="padding-left:2em;"><source lang="delphi"> |
|||
procedure TForm1.Button1Click(Sender: TObject) ; |
|||
var |
|||
AMsgDialog: TForm; |
|||
ACheckBox: TCheckBox; |
|||
begin |
|||
AMsgDialog := CreateMessageDialog('This is a test message.', mtWarning, [mbYes, mbNo]) ; |
|||
ACheckBox := TCheckBox.Create(AMsgDialog) ; |
|||
with AMsgDialog do |
|||
try |
|||
Caption := 'Dialog Title' ; |
|||
Height := 169; |
|||
with ACheckBox do |
|||
begin |
|||
Parent := AMsgDialog; |
|||
Caption := 'Don''t show me again.'; |
|||
Top := 121; |
|||
Left := 8; |
|||
end; |
|||
if (ShowModal = ID_YES) then |
|||
begin |
|||
if ACheckBox.Checked then |
|||
//do if checked |
|||
else |
|||
//do if NOT checked |
|||
end; |
|||
finally |
|||
Free; |
|||
end; |
|||
end; |
|||
</source></div> |
|||
== TStringGrid, TDBGrid == |
== TStringGrid, TDBGrid == |
Revision as of 14:05, 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.
- Add a Don't show this message again to a standard dialog box:
procedure TForm1.Button1Click(Sender: TObject) ;
var
AMsgDialog: TForm;
ACheckBox: TCheckBox;
begin
AMsgDialog := CreateMessageDialog('This is a test message.', mtWarning, [mbYes, mbNo]) ;
ACheckBox := TCheckBox.Create(AMsgDialog) ;
with AMsgDialog do
try
Caption := 'Dialog Title' ;
Height := 169;
with ACheckBox do
begin
Parent := AMsgDialog;
Caption := 'Don''t show me again.';
Top := 121;
Left := 8;
end;
if (ShowModal = ID_YES) then
begin
if ACheckBox.Checked then
//do if checked
else
//do if NOT checked
end;
finally
Free;
end;
end;
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