[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 813
  • Last Modified:

Additional TCustomMemo popup menu items

I am working with a straightforward customised TLogMemo class (inherited from TCustomMemo) which shows the set of standard windows menu item options in a right-click popup menu (Undo,Cut,Copy,Paste,Delete,Select All).

I would like to add more MenuItems to these existing options and catch the notify event in my main form (containing the TLogMemo component), however am not sure how to achieve this. Even when I add a newly created TMenuItem (TLogMemo.Popupmenu.Add) I get an access violation.  Any advice would be appreciated.
0
brenlex
Asked:
brenlex
  • 4
  • 2
  • 2
1 Solution
 
2266180Commented:
the way to do it is like this (provided that the popup menu is created by the logmemo in it's create procedure and not because of some other external action (like at right cick (which doesn't make sense, but it is possible):


TNewMemo=class(TOldMemo)
public
  constructor Create(AOwner:TComponent); override;
end;
 
implementation
 
constructor TNewMemo.create(AOwner:TComponent);
begin
  inherited;// first create the parent class with everything
  popupmenu.items.add(whatever);
end;

Open in new window

0
 
brenlexAuthor Commented:
That's pretty much how I have been trying to do it thus far.  I am working with an existing component used extensively across the company's forms, whereby the popupmenu is not created in TLogMemo... it is created within one of the parent (StdCtrls) classes.  I was hoping that I might be able to just add a new menuitem to the parent's popup object, but I guess (as I still suffer from the access violation ) that the popup object has not yet been created, and is probably created when the event behind the mouse right-click is processed.

I have to presume therefore that I need to use my own popup menu instead, and recreate the code behind the existing popup menuitems ... unless anyone has any additional ideas...?
0
 
2266180Commented:
creating your own popupmenu won't help. because even so, when the original is created and assigned, your instance is being pushed away (which might lead to memory leak if you create the popupmenu without an owner).

so, you need to find out what part of the code is creating the popumenu AND what part and when is it assigning it to the memo.

there is an easier solution if you have access to the original code: you will have to modify the log memo and (re)introduce your own popupmenu property, that way when the poroperty is being set, you have control and you can add items to it ;) this is the easiest solution :)
for anything else, I need to know when and who is creating and assigning the popupmenu.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
brenlexAuthor Commented:
Currently the TLogMemo component already has a published property PopupMenu (redeclared from base class). The hierarchy of the component is derived from the base VCL source as follows TLogMemo < TCustomMemo < TCustomEdit < TWincontrol < TControl < TComponent (where the Popupmenu is declared, though I am unsure when it is actually created during runtime - certainly not on creation of TLogMemo).

I am not looking to create my own popupmenu (as in example http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_23112011.html), but somehow utilise my own menuitem with the existing windows default menuitems -- is this possible?

0
 
leeiCommented:
The local menu that you see (by default) for both the TCustomMemo and TCustomEdit classes is part of the underlying Windows EDIT control that these VCL controls wrap (nothing to do with VCL). It may be possible to extend this menu working directly with low-level windows messages directed to the underlying EDIT control, but that would require much more work than just creating and assigning your own TPopupMenu.

  MyPopup := TPopupMenu.Create(nil);
  mi := TMenuItem.Create(MyPopup);
  mi.Caption := 'Cut';
  mi.OnClick := method_to_perform_cut;
  MyPopup.Items.Add(mi);
  mi := TMenuItem.Create(MyPopup);
  mi.Caption := 'Copy';
  mi.OnClick := method_to_perform_copy;
  MyPopup.Items.Add(mi);
  ...

The PopupMenu property of any VCL control is _not_ assigned by default, so attempting to use it without first assigning an instance of TPopupMenu will generate errors (as you've seen). It's relatively easy to emulate the actions of the default popup menu in code (cut, copy, paste, delete, etc.). If you need help with that let me know. After creating and assigning an instance of TPopupMenu you can then add or remove menu items pragmatically to customize your custom descendant as needed.

 
0
 
leeiCommented:
Spell checker got away from me - that last sentence should say: After creating and assigning an instance of TPopupMenu you can then add or remove menu items _ programmatically _ to customize your custom descendant as needed.
0
 
brenlexAuthor Commented:
Thanks leei -- I just needed confirmation that there was no other means of accessing the windows generated messages.  I have already created my own popup menu programatically and written my own methods to emulate the previously available standard windows menu items.
0
 
brenlexAuthor Commented:
Thanks for confirmation.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now