Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to start a search upon an [Enter] key press in a TEdit Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
27-Aug-02
Category
VCL-General
Language
Delphi 2.x
Views
84
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

I have a series of 6 edit boxes that users type info in that are then passed to 
params in my SQL TQuery. The search query is started by clicking on a button. 
However, users have asked that if they type in one of the edit boxes and then press 
'Enter' that the system searches. I can use Key Press event to trigger it and then 
if key = #13 to make sure its the enter key but then i want it to trigger the 
procedure that does the search, usually triggered by the tool button. Any ideas?

Answer:

Solve 1:

The best solution is to use actions, which I'll describe below. But if you don't 
want to use actions, do this:

Move your search procedure into a separate procedure, and then call that from both 
the toolbutton OnClick and edit OnKeyPress events, like this:

1   unit Unit1;
2   
3   interface
4   
5   uses
6     Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
7     ComCtrls, ToolWin, StdCtrls;
8   
9   type
10    TForm1 = class(TForm)
11      Edit1: TEdit;
12      ToolBar1: TToolBar;
13      ToolButton1: TToolButton;
14      procedure ToolButton1Click(Sender: TObject);
15      procedure Edit1KeyPress(Sender: TObject; var Key: Char);
16    private
17      {Private Declarations}
18      procedure PerformSearch;
19    public
20      {Public declarations}
21    end;
22  
23  var
24    Form1: TForm1;
25  
26  implementation
27  
28  {$R *.DFM}
29  
30  procedure TForm1.ToolButton1Click(Sender: TObject);
31  begin
32    PerformSearch;
33  end;
34  
35  procedure TForm1.PerformSearch;
36  begin
37    { Do search here }
38    ShowMessage('Search performed');
39  end;
40  
41  procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
42  begin
43    if Key = #13 then
44    begin
45      PerformSearch;
46      Key := #0;
47    end;
48  end;
49  
50  end.


To use actions, place a TActionList component onto your form, then create an action 
called something like "SearchAction". Then assign SearchAction to the ToolButton's 
Action property. Finally, call the action's Execute method from the edit, like this:

51  unit Unit1;
52  
53  interface
54  
55  uses
56    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
57    ComCtrls, ToolWin, StdCtrls, ActnList;
58  
59  type
60    TForm1 = class(TForm)
61      Edit1: TEdit;
62      ToolBar1: TToolBar;
63      ToolButton1: TToolButton;
64      ActionList1: TActionList;
65      SearchAction: TAction;
66      procedure Edit1KeyPress(Sender: TObject; var Key: Char);
67      procedure SearchActionExecute(Sender: TObject);
68    private
69      { Private declarations }
70    public
71      { Public declarations }
72    end;
73  
74  var
75    Form1: TForm1;
76  
77  implementation
78  
79  {$R *.DFM}
80  
81  procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
82  begin
83    if Key = #13 then
84    begin
85      SearchAction.Execute;
86      Key := #0;
87    end;
88  end;
89  
90  procedure TForm1.SearchActionExecute(Sender: TObject);
91  begin
92    { Do search here }
93    ShowMessage('Search performed');
94  end;
95  
96  end.


Solve 2:

I'll go one further than Rick. All event handlers should only delegate (unless 
they're one line long in which case they are delegating). In other words if you have
97  
98  procedure TForm1btnSearch.Click(Sender: TObject);
99  begin
100   {...many lines of code that actually implement the search}
101 end;
102 
103 //Change this to:
104 
105 procedure TForm1btnSearch.Click(Sender: TObject);
106 begin
107   FindInformation;
108 end;
109 
110 procedure TForm1.FindInformation;
111 begin
112   {...many lines of code that actually implement the search.}
113 end;


There are, of course, exceptions to this rule, however, for the greater part, you 
will not do wrong to treat an event handler as a proxy rather than placing the code 
directly in it. For one thing, it makes it easier to move the domain code into a 
separate object, so you could end up with:
114 
115 procedure TForm1btnSearch.Click(Sender: TObject);
116 begin
117   MyInformationFinder.Execute;
118 end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC