Recent

Author Topic: [SOLVED] SQLite search edit background color  (Read 713 times)

Pe3s

  • Full Member
  • ***
  • Posts: 206
[SOLVED] SQLite search edit background color
« on: May 11, 2022, 06:00:57 pm »
Hello, is it possible to make such a search engine functionality
1. empty edit control white
2. if found green
3. if not found color red
4. if we clear the control it returns to white

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1Change(Sender: TObject);
  2. begin
  3.   with ZQuery1 do begin
  4.   SQL.Clear;
  5.   SQL.Text := 'SELECT * FROM baza WHERE Nazwisko LIKE :Nazwisko';
  6.   Active := False;
  7.   ParamByName('Nazwisko').AsString := Edit1.Text + '%';
  8.   Active := False;
  9.   Open;
  10. end;
  11. end;
« Last Edit: May 12, 2022, 06:58:10 pm by Pe3s »

dseligo

  • Hero Member
  • *****
  • Posts: 648
Re: SQLite search edit background color
« Reply #1 on: May 12, 2022, 02:02:37 am »
Try this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1Change(Sender: TObject);
  2. begin
  3.   If (Sender as TEdit).Text = '' then begin
  4.     (Sender as TEdit).Color := clDefault;
  5.     Exit;
  6.   end;
  7.  
  8.   with ZQuery1 do begin
  9.     // SQL.Clear; // you don't need this if you directly set SQL.Text property
  10.     SQL.Text := 'SELECT * FROM baza WHERE Nazwisko LIKE :Nazwisko'; // If you use ZEOSdbo you don't need Active := False at beginning
  11.     // Active := False; // don't need this here
  12.     ParamByName('Nazwisko').AsString := (Sender as TEdit).Text + '%';
  13.     // Active := False; // don't need this here
  14.     Open;
  15.     If Eof then (Sender as TEdit).Color := clRed
  16.            else (Sender as TEdit).Color := clGreen;
  17.   end;
  18. end;

Zvoni

  • Hero Member
  • *****
  • Posts: 1261
Re: SQLite search edit background color
« Reply #2 on: May 12, 2022, 08:15:31 am »
Wouldn't use the Change-Event, rather KeyUp
OnChange is only useful in this context if you Copy/Paste your search-Text into the TEdit

btw: Don't you have to close the Query first?
With this code you'd open the query every time you type a new letter without having closed it

And don't use SELECT * FROM
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

dseligo

  • Hero Member
  • *****
  • Posts: 648
Re: SQLite search edit background color
« Reply #3 on: May 12, 2022, 02:28:15 pm »
Wouldn't use the Change-Event, rather KeyUp
OnChange is only useful in this context if you Copy/Paste your search-Text into the TEdit

Why, what's wrong with OnChange event? Wouldn't OnKeyUp react also to arrow keys and similar, causing unnecessary queries to the database?

btw: Don't you have to close the Query first?
With this code you'd open the query every time you type a new letter without having closed it

If you use ZEOSdbo and assign to SQL.Text, you don't need to close query first. I assume he uses ZEOS because query is named ZQuery1.
Although, I would rather use prepared query and only change parameter (then query should be closed first).

Zvoni

  • Hero Member
  • *****
  • Posts: 1261
Re: SQLite search edit background color
« Reply #4 on: May 12, 2022, 03:02:39 pm »
Why, what's wrong with OnChange event? Wouldn't OnKeyUp react also to arrow keys and similar, causing unnecessary queries to the database?
Exactly because of that. You can filter "illegal" keys.
the OnChange-Event doesn't ship the Key being pressed.

Now imagine having a "Search-History" (stored in a List or whatever).
And now imagine pressing the Arrow-Down-Button to quickswitch to the searchphrase before your current one.....

EDIT: And just looking at his First Post, i'm going to throw a Spanner into the gearbox, and enter a "%" as the first Character using his code....

EDIT2: btw: Or i'd use OnKeyDown in that case....
« Last Edit: May 12, 2022, 03:46:40 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Pe3s

  • Full Member
  • ***
  • Posts: 206
Re: SQLite search edit background color
« Reply #5 on: May 12, 2022, 06:57:49 pm »
Thanks for your help and advice :)

dseligo

  • Hero Member
  • *****
  • Posts: 648
Re: SQLite search edit background color
« Reply #6 on: May 12, 2022, 07:18:37 pm »
Why, what's wrong with OnChange event? Wouldn't OnKeyUp react also to arrow keys and similar, causing unnecessary queries to the database?
Exactly because of that. You can filter "illegal" keys.
the OnChange-Event doesn't ship the Key being pressed.

But OnChange doesn't fire when you press arrow keys, Page up/down, ...
It only fires when 'Text' is changed.
I don't understand the advantage of OnKeyUp if one only wants to know when content changes.

Now imagine having a "Search-History" (stored in a List or whatever).
And now imagine pressing the Arrow-Down-Button to quickswitch to the searchphrase before your current one.....

He asked about TEdit. But you can still use OnKeyDown for 'Search-History',.

Zvoni

  • Hero Member
  • *****
  • Posts: 1261
Re: [SOLVED] SQLite search edit background color
« Reply #7 on: May 12, 2022, 10:29:19 pm »
Have you read my edit?
Entering ‚%‘ as the first character (and OnChange you can‘t filter out keystrokes) would result in loading the whole table (PacketRecord not withstanding)
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

dseligo

  • Hero Member
  • *****
  • Posts: 648
Re: [SOLVED] SQLite search edit background color
« Reply #8 on: May 13, 2022, 01:51:52 am »
Have you read my edit?
Entering ‚%‘ as the first character (and OnChange you can‘t filter out keystrokes) would result in loading the whole table (PacketRecord not withstanding)

Maybe he wants that. Or maybe he wants to search for actual '%' character in the table, and he will escape this characters in the text prior to running query.

He could also check text these characters and remove them prior to running query. Or he could use OnKeyUp for filtering, and OnChange for running query.

 

TinyPortal © 2005-2018