1

my application has 350 edit fields and all of them shall have an OnMouseMove event. I have generated this code for all of them:

... type ... procedure Edit1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); procedure Edit2MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); ... implementation {$R *.dfm} ... procedure TForm1.Edit1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); begin Edit1.SetFocus(); end; procedure TForm1.Edit2MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); begin Edit2.SetFocus(); end; ... 

But I didn't go to the object inspector to doubleclick OnMouseMove. Is there a way to make this work without the object inspector. Do you have an example line of code that would make it work for the first edit field?

1
  • 3
    350 edit fields on the same form? Have you considered using a gird? Commented Feb 17, 2013 at 6:00

1 Answer 1

9

You can create it once and assign it in code yourself:

type TForm1=class(TForm) procedure EditMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); procedure FormCreate(Sender: TObject); //... end; implementation procedure TForm1.EditMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); var CurrEdit: TEdit; begin if (Sender is TEdit) then begin CurrEdit := TEdit(Sender); // Do whatever with CurrEdit end; end; procedure TForm1.FormCreate(Sender: TObject); begin Edit1.OnMouseMove := EditMouseMove; Edit2.OnMouseMove := EditMouseMove; Edit3.OnMouseMove := EditMouseMove; end; 

If you want to assign the same one to every TEdit on the form:

procedure TForm1.FormCreate(Sender: TObject); var i: Integer; begin for i := 0 to ControlCount - 1 do if Controls[i] is TEdit then TEdit(Controls[i]).OnMouseMove := EditMouseMove; end; 
Sign up to request clarification or add additional context in comments.

3 Comments

this is pure magic and saved us a lot of hard work, thank you
@MichaelMoeller: not to diminish Ken's answer, but this isn't magic at all, this is knowing the language and the IDE that you are working in. An attitude of "this shouldn't be so much hard work, there ought to be an easier way" and investigating/googling that goes a long way to acquiring that knowledge.
Or Ken will write it for ya. :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.