Recent

Author Topic: TComboBox, CharCase and KeyPress  (Read 1409 times)

rudiloos

  • Newbie
  • Posts: 2
TComboBox, CharCase and KeyPress
« on: August 09, 2024, 05:42:37 pm »
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][+][-]
  1. procedure TTestComboBox.KeyPress(var Key: Char);
  2. const
  3.   CrLf = #13#10;
  4. var
  5.   s, t, tt: string;
  6.   i, pp: Integer;
  7. begin
  8.   inherited;
  9.  
  10.   if Key < ' ' then Exit;  //Ignore control characters
  11.  
  12.   i :=  SelStart;
  13.   t :=  Text;
  14.   s :=  Copy(t, 1, i) + Key;
  15.   tt := Items.Text;
  16.   pp := Pos(CrLf+s, CrLf+tt)
  17.  
  18.   if pp > 0 then exit;    //Text found in the item list
  19.  
  20.   Key := #0;  //Text not found in the item list so ignore key
  21.  
  22. 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

  • Hero Member
  • *****
  • Posts: 2718
Re: TComboBox, CharCase and KeyPress
« Reply #1 on: August 19, 2024, 03:59:36 pm »
Use KeyDown/Up
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

Aruna

  • Sr. Member
  • ****
  • Posts: 494
Re: TComboBox, CharCase and KeyPress
« Reply #2 on: August 19, 2024, 07:09:22 pm »
Extending the ComboBox to monitor user input and make sure that what is entered, exists in the item list.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
  2. var
  3.   i: Integer;
  4.   CurrentText: string;
  5.   ItemText: string;
  6.   Found: Boolean;
  7. begin
  8.   // Accumulate the current text in the ComboBox
  9.   CurrentText := ComboBox1.Text + Key;
  10.  
  11.   // Reset the found flag
  12.   Found := False;
  13.  
  14.   // Check if the accumulated text is a substring of any ComboBox item
  15.   for i := 0 to ComboBox1.Items.Count - 1 do
  16.   begin
  17.     ItemText := ComboBox1.Items[i];
  18.     if Pos(CurrentText, ItemText) > 0 then
  19.     begin
  20.       Found := True;
  21.       Break;
  22.     end;
  23.   end;
  24.  
  25.   // Handle the case where the substring is not found
  26.   if not Found then
  27.   begin
  28.     // Optionally show a message or handle the invalid substring
  29.     ShowMessage('The substring "' + CurrentText + '" is not present in any ComboBox items.'); // If this becomes annoying maybe use writeln or simply disregard?
  30.     // Optionally clear the text or prevent the input
  31.     Key := #0; // Ignore this key press
  32.   end;
  33. end;                                        
« Last Edit: August 19, 2024, 07:27:08 pm by Aruna »

 

TinyPortal © 2005-2018