Capture Key press of user

Good Day

I got a grid on my form, but what I want to do is when the user type a 3 digit code e.g 301 or 901 from his keybord and press enter, then I want to seacrh a record in my grid and display the record.

Can someone show me a example on how to capture the key stroke on the form when the user type 301 and press enter. When the user press enter, then I must do my part pf searching the grid and try and locate the record.

Now if the type 307 again and press enter then it must do my search again.

thanx
henryreynoldsAsked:
Who is Participating?
 
2266180Connect With a Mentor Commented:
1) set the forms keypreview property to true.
2) put a string grid on the form (not necessary for this demo :) )
3) run the below code

not that the label is there so that you can see that when pressing enter, you see what you search for. you can delete the label and it will still work as long as you replace
label1.caption:=s;
 with your find function.
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids;
 
type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
    s:string;
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  s:='';
end;
 
procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if key=#13 then
  begin
    label1.caption:=s;// this is the line where you do the actual search. 
    s:='';
  end else
    s:=s+key;
end;
 
end.

Open in new window

0
 
henryreynoldsAuthor Commented:
Hi Ciuly

Thank you very much, it is perfect.

Keep well

Henry
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.