Recent

Author Topic: MenuItem Caption  (Read 2723 times)

Zath

  • Sr. Member
  • ****
  • Posts: 391
MenuItem Caption
« on: February 18, 2019, 12:33:23 am »
How do I get a MenuItem caption and concatenate it to the main form title ?
Currently I have DefaultCaption and the actual item name as a string.
The menu item has the name itself and I was wondering if I can get that as a caption and have a generic line for all menu clicks.
Something like
Form1.Caption :=DefaultCaption+Sender.Caption.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.MenuItem21Click(Sender: TObject);
  2. begin
  3.   ItmNo:=2;
  4.   Form1.Caption :=DefaultCaption+' : BigMap';
  5.   MemoGeneral.Lines.LoadFromFile(drName+'\M2Server\Envir\BigMap.txt');
  6. end;
  7.  

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: MenuItem Caption
« Reply #1 on: February 18, 2019, 01:51:37 am »
How about:
Code: Pascal  [Select][+][-]
  1. Caption := DefaultCaption;
  2. if Sender.InheritsFrom(TMenuItem) then
  3.   Caption := Caption + (Sender as TMenuItem).Caption;
or just
Code: Pascal  [Select][+][-]
  1. Caption := DefaultCaption + (Sender as TMenuItem).Caption;
« Last Edit: February 18, 2019, 01:53:22 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: MenuItem Caption
« Reply #2 on: February 18, 2019, 09:54:23 am »
How about:
Code: Pascal  [Select][+][-]
  1. Caption := DefaultCaption;
  2. if Sender.InheritsFrom(TMenuItem) then
  3.   Caption := Caption + (Sender as TMenuItem).Caption;
or just
Code: Pascal  [Select][+][-]
  1. Caption := DefaultCaption + (Sender as TMenuItem).Caption;

Thanks lucamar, both look like options. My lack of familiarity with some of the object methods clearly needs improving.

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: MenuItem Caption
« Reply #3 on: February 19, 2019, 12:39:38 am »
I created a small procedure for your first example and it works fine.
The second option works fine too but gives me less saving in code.

Thanks again.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: MenuItem Caption
« Reply #4 on: February 19, 2019, 01:17:44 am »
Glad to be of help! :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

furious programming

  • Hero Member
  • *****
  • Posts: 858
Re: MenuItem Caption
« Reply #5 on: February 19, 2019, 02:08:18 pm »
The TMenuItem.OnClick event always provides in the parameter an instance of the TMenuItem class, so checking the parameter type is not needed. Of course unless someone calls the event from the code level and gives the object another of class, but this should not be done, because TMenuItem has the Click method.

So you can confidently treat the parameter as TMenuItem:

Code: Pascal  [Select][+][-]
  1. procedure TForm.MenuItemClick(ASender: TObject);
  2. var
  3.   MenuItem: TMenuItem absolute ASender;
  4. begin
  5.   Caption := DefaultCaption + MenuItem.Caption;
  6. end;
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: MenuItem Caption
« Reply #6 on: February 19, 2019, 03:06:16 pm »
The TMenuItem.OnClick event always provides in the parameter an instance of the TMenuItem class, so checking the parameter type is not needed. Of course unless someone calls the event from the code level and gives the object another of class, but this should not be done, because TMenuItem has the Click method.

You're right, of course; in fact that's what I do always: just call AMenuItem.Click if needed--same with the other classes that have a Click method, like TButton.

It's just that, after having been burned quite a few times, now I always check Sender against the needed class. Beter safe than sorry, I always say :)

The one thing I don't like about your proposal is the unneccessary use of absolute. It's maybe a knee-jerk reaction nut since soft-casting with as is readily available, I wouldn't use it. If anything, to avoid extra typing if Sender is referenced many times, I would just do:
Code: Pascal  [Select][+][-]
  1. procedure TForm.MenuItemClick(ASender: TObject);
  2. var
  3.   AMenuItem: TMenuItem;
  4. begin
  5.   AMenuItem := Sender as TMenuItem;
  6.   Caption := DefaultCaption + AMenuItem.Caption;
  7.   {... etc ...}
  8. end;

RE . setting the form's caption, maybe it would be better to generalize the process with something like:
Code: Pascal  [Select][+][-]
  1. TForm1.BuildCaption(AnObject: TObject): String;
  2. begin
  3.   Result := DefaultCaption;
  4.   if Assigned(AnObject) then begin
  5.     if AnObject.InheritsFrom(TControl) then
  6.       Result := Result + (AnObject as TControl).Caption
  7.     else if AnObject.InheritsFrom(TMenuItem) then
  8.       Result := Result + (AnObject as TMenuItem).Caption;
  9.     { add other components with Caption which don't inherit from TControl }
  10.   end;
  11. end;
« Last Edit: February 19, 2019, 03:23:39 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: MenuItem Caption
« Reply #7 on: February 22, 2019, 12:59:43 am »
Interesting ideas that I can try.
Thanks !

 

TinyPortal © 2005-2018