![]() |
I DIDN'T WRITE THESE TIPS,
I'VE JUST FOUND THEM IN AN HELP FILE, BUT THEY'RE VERY COOL!!
|
1) How to make your computer beep:
messageBeep(0);
2) How to pause your program for at least NumSec seconds:
var
NumSec SmallInt;
StartTime: LongInt;
begin
StartTime := now;
repeat
Application.ProcessMessages;
until Now > StartTime + NumSec * (1/24/60/60);
end;
3) How to display the mouse's hourglass (and then go back to an arrow):
try
Screen.Cursor := crHourGlass;
{do something here...}
finally
Screen.Cursor := crDefault;
end;
Application.ProcessMessages;
4) How to take control of the <Enter> keypress:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
{put this in the OnKeyPress event of any control you want...}
begin
{#13 is the RETURN key, or use #9 for the TAB key}
if Key = #13 then begin
Key := #0; {supress the bell}
{do what you want to do here};
end;
end;
5) How to change the colour of text in a DBGrid field depending cell contents:
First, double click the TTable component on your form and add the field you are interested in, in this example the field "Client" is used, and if that field value is "XXXX" then the colour changes. This code is placed in the OnDataDrawCell event:
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect; Field:
TField; State: TGridDrawState);
begin
if Table1Client.AsString = 'XXXX' then begin
DBGrid1.Canvas.Brush.Color := clRed;
DBGrid1.Canvas.Font.Color := clSilver;
DBGrid1.Canvas.FillRect(Rect); {paint in the background}
{now write the field value on top of the background...}
DBGrid1.Canvas.TextOut(Rect.Left+2, Rect.Top+1, Field.AsString);
end;
end;
6) How to run another program from yours (3 different ways!):
WinExec('C:\windows\notepad.exe', SW_SHOWNORMAL);
WinExec('C:\windows\notepad.exe', SW_SHOWMAXIMIZED);
WinExec('C:\windows\notepad.exe', SW_SHOWMINIMIZED);
7) How to scan an entire data table:
Table1.First;
x := 0;
while not(Table1.Eof) do
begin
{do what you have to do here}
Table1.Next;
end;
Also, by setting a range before the line Tbl1.First, you can scan all records within that range using the above.
8) How to intercept the function keys:
First, set the KeyPreview property of your form to TRUE, then put this code in your form's OnKeyDown event:
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if Key = VK_F5 then
showMessage('I pressed the F5 key');
end;
Also, you can use VK_F1 through to VK_F12 for the other function keys.
9) How to copy all the field values from one record into another:
In this example from table TableSource to TableDest, both having the same field structure:
var
Num: SmallInt;
begin
for Num := 0 to TableSource.FieldCount-1 do
begin
TableDest.Edit;
TableDest.Fields[Num].Assign(TableSource.Fields[Num];
TableDest.Post;
end;
end;
10) How to see if an integer is even or odd:
function TestForEven(TestInt: Integer): Boolean;
begin
if (TestInt div 2) = (TestInt/2) then
result := True
else result := False;
end;
11) How to see if a string looks like an integer:
function IsInteger(TestThis: String): Boolean;
begin
try
StrToInt(TestThis);
except
on EConvertError do
result := False;
else
result := True;
end;
end;