Forum > General

[SOLVED] TMainMenu how to get selected option text?

(1/2) > >>

Aruna:
Hi how do I check which item is highlighted/selected on a TMainMenu, please? Let's say we have a top-level menu with three options and I have moved the mouse cursor onto option 2 now how do I test this through pascal code?

ASerge:

--- Quote from: aruna.hewapathirane on November 22, 2022, 04:52:38 pm ---Hi how do I check which item is highlighted/selected on a TMainMenu, please? Let's say we have a top-level menu with three options and I have moved the mouse cursor onto option 2 now how do I test this through pascal code?

--- End quote ---

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Menus, StdCtrls, LCLType; type  TMenuItem = class(Menus.TMenuItem)  public    procedure IntfDoSelect; override;  end;   TForm1 = class(TForm)    Label1: TLabel;    MainMenu1: TMainMenu;    MenuItem1: TMenuItem;    MenuItem2: TMenuItem;    MenuItem3: TMenuItem;    MenuItem4: TMenuItem;  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} procedure TMenuItem.IntfDoSelect;begin  inherited;  if Assigned(Form1) then    Form1.Label1.Caption := Caption;end; end.

Aruna:

--- Quote from: ASerge on November 22, 2022, 06:40:53 pm ---
--- Quote from: aruna.hewapathirane on November 22, 2022, 04:52:38 pm ---Hi how do I check which item is highlighted/selected on a TMainMenu, please? Let's say we have a top-level menu with three options and I have moved the mouse cursor onto option 2 now how do I test this through pascal code?

--- End quote ---

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Menus, StdCtrls, LCLType; type  TMenuItem = class(Menus.TMenuItem)  public    procedure IntfDoSelect; override;  end;   TForm1 = class(TForm)    Label1: TLabel;    MainMenu1: TMainMenu;    MenuItem1: TMenuItem;    MenuItem2: TMenuItem;    MenuItem3: TMenuItem;    MenuItem4: TMenuItem;  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} procedure TMenuItem.IntfDoSelect;begin  inherited;  if Assigned(Form1) then    Form1.Label1.Caption := Caption;end; end.
--- End quote ---

Holy wow! This is sweet and absolutely amazing. Thank you so much!

I just wish I 'understood' what you have done to make this work. So thank you so much for taking the time and for this incredible piece of code but I am now going to start asking questions because I really want to understand what you did and someday hopefully be able to do similar voodoo magic when needed :-)
 
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---StdCtrls, LCLType You added StdCtrls, LCLType could you please explain in simple layman noob terms the reason why?

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---public    procedure IntfDoSelect; override; end;And you overrode IntfDoSelect again I have no clue where you found this or why we override it? Where does one find documentation on IntfDoSelect and such? I am very new to this object-oriented stuff so be gentle. So to my simple mind, it seems you identified a 'method' or 'property' then overrode it and plugged in your own procedure to do what is needed?

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TMenuItem.IntfDoSelect;begin  inherited;  if Assigned(Form1) then    Form1.Label1.Caption := Caption;end;And what does inherited do in the procedure above?

If we are able to hook into components like this and change the behavior then we are free to literally do anything we want? Of course within sane defaults, nothing too crazy! I am still in shock trying to understand your code. It works and works well but I want to know the how and why please?

KodeZwerg:
He does intercept the original TMenuItem with his own, so codeflow still works same as usual beside the "IntfDoSelect" method that he override with his own.

Aruna:

--- Quote from: KodeZwerg on November 23, 2022, 04:23:41 am ---He does intercept the original TMenuItem with his own, so codeflow still works same as usual beside the "IntfDoSelect" method that he override with his own.

--- End quote ---
Thank you KodeZwerg.This is what I also thought. So does this mean we can intercept any object and override any method with our own ?

Navigation

[0] Message Index

[#] Next page

Go to full version