[Lazarus] Filling ListBox without triggering OnSelectionChange

Marc Santhoff M.Santhoff at web.de
Thu Jan 8 02:01:19 CET 2015


On Mi, 2015-01-07 at 10:36 +0100, A. Fortuny wrote:
> Le 7/01/2015 07:49, Marc Santhoff a écrit :
> > Hi,
> >
> > it has been a very long time since I've programmed GUIs. I have
> > forgotten how to fill a ListBox without having the event handler
> > OnSelectionChange executed.

> I've had the same problem for years, until I "invented" my method:
> - fill in the two events OnEnter and OnExit for the control
> - generate the code for the OnSelectionChange event and drop immediately 
> the event in the component explorer
> - manually fill in the following code in the three envents:
> 
> TMyForm.MyComponentEnter(Sender: TObject);
> begin
>    MyComponent.OnSelectionChange := MyComponentSelectionChange;
> // do the same for all other events you want to drive
> end
> 
> TMyForm.MyComponentSlectionChange(Sender: TObject; ...);
> begin
>    // do whatver to do when the event fires
> end
> 
> TMyForm.MyComponentExit(Sender: TObject);
> begin
>    MyComponent.OnSelectionChange := nil
> end
> 
> Of course you can control any firing event using the OnEnter and OnExit 
> events of the control. This allows the program to fire events only when 
> the user enters the control.
> I think that GUI's  lack a control property wich indicates whether the 
> events should always fire or fire only when the user enters the control.

Yes, assigning the event handler does work, if it is done in OnActivate.
When done in OnCreate of the form there must be some actions afterwards
setting the selection, And it is possible to set the ItemIndex to -1 for
deselecting the first item having the selection on start. Beautiful.

procedure TForm1.FormCreate(Sender: TObject);
var
  i: longword;
begin
  tasklist := TStringList.create;
  for i:=0 to taskmax do tasklist.add(tasks[i]);
  ListBox1.Items.Assign(tasklist);
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
  ListBox1.ItemIndex := -1;
  ListBox1.OnSelectionChange := @ListBox1SelectionChange;
end;

Thank you!

-- 
Marc Santhoff <M.Santhoff at web.de>





More information about the Lazarus mailing list