[SOLVED] SQLite search edit background color lazarus / 16-Databases ---------------------------------------------------------------------- Pe3s · 2022-05-11 16:00 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Edit1Change(Sender: TObject);begin  with ZQuery1 do begin  SQL.Clear;  SQL.Text := 'SELECT * FROM baza WHERE Nazwisko LIKE :Nazwisko';  Active := False;  ParamByName('Nazwisko').AsString := Edit1.Text + '%';  Active := False;  Open;end;end; ---------------------------------------------------------------------- dseligo · 2022-05-12 00:02 Try this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Edit1Change(Sender: TObject);begin  If (Sender as TEdit).Text = '' then begin    (Sender as TEdit).Color := clDefault;    Exit;  end;   with ZQuery1 do begin    // SQL.Clear; // you don't need this if you directly set SQL.Text property    SQL.Text := 'SELECT * FROM baza WHERE Nazwisko LIKE :Nazwisko'; // If you use ZEOSdbo you don't need Active := False at beginning    // Active := False; // don't need this here    ParamByName('Nazwisko').AsString := (Sender as TEdit).Text + '%';    // Active := False; // don't need this here    Open;    If Eof then (Sender as TEdit).Color := clRed           else (Sender as TEdit).Color := clGreen;  end;end; ---------------------------------------------------------------------- Zvoni · 2022-05-12 06:15 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 ---------------------------------------------------------------------- dseligo · 2022-05-12 12:28 Quote from: Zvoni on May 12, 2022, 08:15:31 amWouldn'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? Quote from: Zvoni on May 12, 2022, 08:15:31 ambtw: 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 · 2022-05-12 13:02 Quote from: dseligo on May 12, 2022, 02:28:15 pmWhy, 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.... ---------------------------------------------------------------------- Pe3s · 2022-05-12 16:57 Thanks for your help and advice ---------------------------------------------------------------------- dseligo · 2022-05-12 17:18 Quote from: Zvoni on May 12, 2022, 03:02:39 pmQuote from: dseligo on May 12, 2022, 02:28:15 pmWhy, 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. Quote from: Zvoni on May 12, 2022, 03:02:39 pmNow 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 · 2022-05-12 20:29 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) ---------------------------------------------------------------------- dseligo · 2022-05-12 23:51 Quote from: Zvoni on May 12, 2022, 10:29:19 pmHave 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.