I have 2 problems, that event TComboBox.OnEditingDone is fired to often:
unit Unit1;
{$mode objfpc}{$H+}
{$apptype console} {neccessary for writeln}
interface
uses
Classes, SysUtils, Forms, Controls, Dialogs, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
ComboBox1: TComboBox;
ListBox1: TListBox;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
procedure ComboBox1EditingDone(Sender: TObject);
procedure FormActivate(Sender: TObject);
private
public
end;
var Form1: TForm1;
implementation
{$R *.lfm}
procedure ComboBox_add_History(var X: TComboBox; max: integer);
{adds the current value of the ComboBox to the history}
var s: ansistring;
i: integer;
begin
s:=X.Text; {get aktuellen Wert der ComboBox}
if s='' then exit;
i:=X.Items.IndexOf(s); {sucht 's' in der History, not found => -1}
if i = 0 then exit; {wenn schon als jüngstes vorhanden}
if i > 0 then {wenn schon vorhanden, aber nicht als jüngstes: }
begin
X.Items.Delete(i); {delete current item}
X.Text:=s; {muß nach Delete() wiederholt werden}
end;
while X.Items.Count >= max do {lösche ggf. ältere Einträge: }
begin
i:=X.Items.Count-1; {oldest item}
X.Items.Delete(i); {delete oldest item}
end;
X.Items.Insert(0,s); {neues item am Anfang einfügen}
end;
procedure TForm1.ComboBox1EditingDone(Sender: TObject);
const nr: integer = 1;
begin
writeln('OnEditingDone', nr);
inc(nr);
ComboBox_add_History(ComboBox1,12);
ListBox1.SetFocus; // get the focus away from ComboBox1 (does not help)
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
ListBox1.SetFocus; // get the focus away from ComboBox1 (does not help)
end;
end.
Problem #1:
a) compile and start the attached project
b) click on TabSheet2
c) click on Button 2
d) click on TabSheet1 (result: ComboBox1 becomes blue)
e) click on Button 1
Result: event TComboBox.OnEditingDone is fired, although this component was NOT "edited".
Looks for me like a focus problem, that the ComboBox became blue in d) and was "quit" in e)
Is that correct?
To avoid this I used ListBox1 to place the focus there (lines 64 and 69), but this did not help and evoked problem #2.
Problem #2:
- compile and start the attached project
- enter something via keybord in ComboBox1 and press ENTER
Result: event TComboBox.OnEditingDone is fired twice, although this component was "edited" only once.
Why does this happen?
I would like to understand this behavior, if possible.
What I want is ONE event in case of a) the ComboBox has got another content or b) the same content as last time is selected a 2nd time via Mouse or ENTER. How can this be done?
I use Lazarus 1.8.4 on Windows 7 32 bit. Thanks a lot in advance.
[EDIT]
There is a summary about this Topic in reply #24.