TComboBox, CharCase and KeyPress lazarus / 31-LCL ---------------------------------------------------------------------- rudiloos · 2024-08-09 15:42 Hi Looking for opinions and logic that might extend beyond mine(easy) Extending the ComboBox to monitor user input and make sure that what is entered, exists in the item list. KeyPress does not fire under certain conditions which I do not feel is right (My logic at least) 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 TTestComboBox.KeyPress(var Key: Char);const  CrLf = #13#10;var  s, t, tt: string;  i, pp: Integer;begin  inherited;   if Key < ' ' then Exit;  //Ignore control characters   i :=  SelStart;  t :=  Text;  s :=  Copy(t, 1, i) + Key;  tt := Items.Text;  pp := Pos(CrLf+s, CrLf+tt)   if pp > 0 then exit;    //Text found in the item list   Key := #0;  //Text not found in the item list so ignore key end; Findings CharCase is set to: ecNormal : KeyPress always fires ecUpperCase: Only fires when entering an UpperCase Char ecLowerCase: Only fires when entering a LowerCase Char Expected KeyPress should always fire and Key should just be in the selected Case OS Win10    (Not tested on other OS) FPC 3.2.3 Laz 3.4 Widgetset Win32/Win64 (Others not tested) ---------------------------------------------------------------------- Zvoni · 2024-08-19 13:59 Use KeyDown/Up ---------------------------------------------------------------------- Aruna · 2024-08-19 17:09 Quote from: rudiloos on August 09, 2024, 05:42:37 pmExtending the ComboBox to monitor user input and make sure that what is entered, exists in the item list. 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.ComboBox1KeyPress(Sender: TObject; var Key: Char);var  i: Integer;  CurrentText: string;  ItemText: string;  Found: Boolean;begin  // Accumulate the current text in the ComboBox  CurrentText := ComboBox1.Text + Key;   // Reset the found flag  Found := False;   // Check if the accumulated text is a substring of any ComboBox item  for i := 0 to ComboBox1.Items.Count - 1 do  begin    ItemText := ComboBox1.Items[i];    if Pos(CurrentText, ItemText) > 0 then    begin      Found := True;      Break;    end;  end;   // Handle the case where the substring is not found  if not Found then  begin    // Optionally show a message or handle the invalid substring   ShowMessage('The substring "' + CurrentText + '" is not present in any ComboBox items.'); // If this becomes annoying maybe use writeln or simply disregard?    // Optionally clear the text or prevent the input    Key := #0; // Ignore this key press  end;end;