Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A neater way of doing this (OnClick) 1

Status
Not open for further replies.

sggaunt

Programmer
Jul 4, 2001
8,620
GB
I have a file menu to which I add newly opened filenames
And the Onclick method (e.g openfile) assigned to the TmenuItem, can then open the file


Code:
   Submenu.Onclick := OpenFile;
   Submenu.caption := Filename;

I can then extract the filename from this in the method
Code:
procedure OpenFile(Sender: Tobject);
FName: TFilename;
begin
 FName := (Sender As TMenuItem).Caption;
  etc..
end;

But I also want to make direct calls to this Method.
I have always done this by creating a temporary 'object' to pass as 'Sender', e.g to save testing a TMenuItem.

Is there a neater way?






Steve [The sane]: Delphi a feersum engin indeed.
 
If you were going to code in the same way the VCL is done, you'd have an extra method:
Code:
[b]procedure[/b] OpenFile(Sender: Tobject);
[b]begin[/b]
  DoOpenFile((Sender As TMenuItem).Caption);
[b]end[/b];

[b]procedure[/b] DoOpenFile(AName: String);
[b]begin[/b] 
  [navy][i]// etc..
[/i][/navy][b]end[/b];
 
My 2 cents worth: I have to agree that Grif's code will make more sense to anyone else that picks up your code, or even to you, way down the road.

I've seen code with lots of processing in an OnClick, but prefer this (example):
Code:
procedure TMagForm.ShowMagReport;
begin
  //processing goes here...
end;

procedure TMagForm.ReportBtnClick(Sender: TObject);
begin
  ShowMagReport;
end;
It also allows for more reusable code.

Roo
Delphi Rules!
 
Yes of course!!! Thanks guys, mental block gone.

I do that sort of thing all the time with Designer onclicks, but for some reson it diddnt 'click' (sorry) with this.




Steve [The sane]: Delphi a feersum engin indeed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top