Adam
Adam

Reputation: 357

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.

Upvotes: 7

Views: 6287

Answers (3)

Ravaut123
Ravaut123

Reputation: 2808

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;

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 613302

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.

Upvotes: 8

Ville Krumlinde
Ville Krumlinde

Reputation: 7131

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);

Upvotes: 4

Related Questions