• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1336
  • Last Modified:

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
0
henryreynolds
Asked:
henryreynolds
1 Solution
 
2266180Commented:
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now