Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an alternative to the OnChange event that is raised on any action in Delphi?

From the Delphi XE documentation:-

Note: OnChange only occurs in response to user actions. Changing the Text property programmatically does not trigger an OnChange event.

Are there any other events available for TComboBox that are raised when any change happens (by the user or programmatically)? When changing the ItemIndex property of the TComboBox no event is raised.

like image 525
Adam Avatar asked Nov 30 '11 15:11

Adam


3 Answers

The combo box control is sent a CM_TEXTCHANGED when the text is modified. The VCL control chooses not to surface an event here, but you could. There's many ways to do so. Here I illustrate the quick and dirty interposer class:

TComboBox = class(Vcl.StdCtrls.TComboBox)
  procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
end;

procedure TComboBox.CMTextChanged(var Message: TMessage);
begin
  inherited;
  Beep;
end;

Naturally you would want to do this in a less hacky way in your production code.

like image 149
David Heffernan Avatar answered Nov 10 '22 21:11

David Heffernan


You could always trigger the onchange-method yourself if that's what you want.

Edit1.Text := 'hello';  //Set a value
Edit1.OnChange(Edit1);  //..then trigger event

Edit: David is right, a TEdit calls OnChange on all updates. If it is a combobox you want to trigger then use something like: Combobox1.OnChange(Combobox1);

like image 37
Ville Krumlinde Avatar answered Nov 10 '22 20:11

Ville Krumlinde


Create a new component from TComboBox

TMyCombo= class(TComboBox)
private
  procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
end;

{ TMyCombo }
procedure TMyCombo.CMTextChanged(var Message: TMessage);
begin
 inherited;
 Change;
end;

TForm1 = class(TForm)
  procedure MyChange(sender: TObject);
...
private
 FCombo: TMyCombo;
...

procedure TForm1.FormCreate(Sender: TObject);
begin
 FCombo:= TMyCombo.Create(self);
 FCombo.Parent:= self;
 FCombo.OnChange:=  MyChange;
end;

procedure TForm1.MyChange(Sender: TObject);
begin
  self.Edit1.Text:= FCombo.Text;
end;

destructor TForm1.Destroy;
begin
  FreeAndNil(FCombo);
  inherited;
end;
like image 1
Ravaut123 Avatar answered Nov 10 '22 20:11

Ravaut123