Board index » cppbuilder » TMenuItem->Add() changes Caption property of item?

TMenuItem->Add() changes Caption property of item?

I am trying to add items to a menu at runtime.  I have followed the example
in the help file, and it seems to work fine except...

The TMenuItem->Caption property I set gets changed after Add()ing the item!

Specifically, I set the Caption property to something like "Open &Form" and
it gets changed to "&Open Form".  Is this normal for the underline '&' to
get moved around?

 

Re:TMenuItem->Add() changes Caption property of item?


What does your exact code look like?

Gambit

Quote
"Terry C" <tch...@wyoming.com> wrote in message news:3d08feba$1_2@dnews...
> Specifically, I set the Caption property to something like
> "Open &Form" and it gets changed to "&Open Form".  Is this
> normal for the underline '&' to get moved around?

Re:TMenuItem->Add() changes Caption property of item?


This is the event handler for a TAction.  The application is an MDI one.
The TMenuItem I am adding to is miWindow.  The goal is to have a current
list of open MDI child windows on the bottom of the miWindow menu.

I have since changed the Caption property to have *NO* underline ('&') at
all, yet the Add() function puts one in on the first character anyway!
(Confirmed during application run.  Hit the Alt key and the 'A' is
underlined.)

void __fastcall TfrmMain::DBAddCompsExecute(TObject *Sender)
{
 if(!frmAddBatch){
  frmAddBatch = new TfrmAddBatch(this);

  TMenuItem *NewItem = new TMenuItem(miWindow);

  NewItem->Caption = "Add Computers";
  NewItem->ImageIndex = 4;
  NewItem->OnClick = RaiseToTop;

  mnuMain->Items->Items[2]->Add(NewItem);
 }

Quote
}

"Remy Lebeau [TeamB]" <gambi...@yahoo.com> wrote in message
news:3d090eb6$1_1@dnews...
Quote
> What does your exact code look like?

> Gambit

> "Terry C" <tch...@wyoming.com> wrote in message news:3d08feba$1_2@dnews...

> > Specifically, I set the Caption property to something like
> > "Open &Form" and it gets changed to "&Open Form".  Is this
> > normal for the underline '&' to get moved around?

Re:TMenuItem->Add() changes Caption property of item?


Quote
"Terry C" <tch...@wyoming.com> wrote in message news:3d091250$1_1@dnews...
> This is the event handler for a TAction.  The application is an MDI
> one. The TMenuItem I am adding to is miWindow.  The goal is to
> have a current list of open MDI child windows on the bottom of
> the miWindow menu.

Is mnuMain->Items->Items[2] the same item as miWindow?  If so, then you
could just use miWindow directly instead:

    miWindow->Add(NewItem);

In any event, MDI already handles all of that automatically for you.  You
don't need to do it yourself manually.  Just assign miWindow to the
WindowMenu property of the MDI parent form using the Object Inspector at
design time.  Then any MDI child forms that you open during runtime will
automatically be added to the WindowMenu as needed, and the internal OnClick
handler will automatically handle bring windows to the front when you click
on the menu items for them.

Quote
> I have since changed the Caption property to have *NO* underline ('&')
> at all, yet the Add() function puts one in on the first character anyway!

Add() itself never directly modifies the Caption of menu items, all it does
is add the menu pointer to an internal list of pointers, that's all.
Something else must be changing the Caption behind the scenes.

Gambit

Re:TMenuItem->Add() changes Caption property of item?


You are right about the mnuMain->Items->Items[2] == miWindow.

However, I tried commenting out the code that manually adds the open MDI
children to the menu, and the application did not automatically do that.  I
set the WindowMenu property to "miWindow" and it didn't add the child
windows to the menu.  I then completely cleared out the contents of miWindow
in the menu editor, since the help file suggests that an MDI app will
automatically create the contents of the "WindowMenu" menu item.  When I ran
the app, this simply resulted in an empty menu.

What am I missing?

"Remy Lebeau [TeamB]" <gambi...@yahoo.com> wrote in message
news:3d092eec$1_1@dnews...

Quote

> "Terry C" <tch...@wyoming.com> wrote in message news:3d091250$1_1@dnews...
> > This is the event handler for a TAction.  The application is an MDI
> > one. The TMenuItem I am adding to is miWindow.  The goal is to
> > have a current list of open MDI child windows on the bottom of
> > the miWindow menu.

> Is mnuMain->Items->Items[2] the same item as miWindow?  If so, then you
> could just use miWindow directly instead:

>     miWindow->Add(NewItem);

> In any event, MDI already handles all of that automatically for you.  You
> don't need to do it yourself manually.  Just assign miWindow to the
> WindowMenu property of the MDI parent form using the Object Inspector at
> design time.  Then any MDI child forms that you open during runtime will
> automatically be added to the WindowMenu as needed, and the internal
OnClick
> handler will automatically handle bring windows to the front when you
click
> on the menu items for them.

> > I have since changed the Caption property to have *NO* underline ('&')
> > at all, yet the Add() function puts one in on the first character
anyway!

> Add() itself never directly modifies the Caption of menu items, all it
does
> is add the menu pointer to an internal list of pointers, that's all.
> Something else must be changing the Caption behind the scenes.

> Gambit

Re:TMenuItem->Add() changes Caption property of item?


Aha!

The trick to making this all work is the GroupIndex property of each menu
item.  The GroupIndex of the main menu items should be sequentially
numbered, as well as the GroupIndex of the menu items in each of the main
menu's menu items.  After doing this, the automatic behavior you described
works perfectly.  I did have to manually add the menu items for the
WindowMenu; it isn't as automatic as I had thought.  But the child windows
appear at the bottom now, as they should.

Thanks much!

Quote
"Terry C" <tch...@wyoming.com> wrote in message news:3d0a0ad7_1@dnews...
> You are right about the mnuMain->Items->Items[2] == miWindow.

> However, I tried commenting out the code that manually adds the open MDI
> children to the menu, and the application did not automatically do that.
I
> set the WindowMenu property to "miWindow" and it didn't add the child
> windows to the menu.  I then completely cleared out the contents of
miWindow
> in the menu editor, since the help file suggests that an MDI app will
> automatically create the contents of the "WindowMenu" menu item.  When I
ran
> the app, this simply resulted in an empty menu.

> What am I missing?

> "Remy Lebeau [TeamB]" <gambi...@yahoo.com> wrote in message
> news:3d092eec$1_1@dnews...

> > "Terry C" <tch...@wyoming.com> wrote in message

news:3d091250$1_1@dnews...

- Show quoted text -

Quote
> > > This is the event handler for a TAction.  The application is an MDI
> > > one. The TMenuItem I am adding to is miWindow.  The goal is to
> > > have a current list of open MDI child windows on the bottom of
> > > the miWindow menu.

> > Is mnuMain->Items->Items[2] the same item as miWindow?  If so, then you
> > could just use miWindow directly instead:

> >     miWindow->Add(NewItem);

> > In any event, MDI already handles all of that automatically for you.
You
> > don't need to do it yourself manually.  Just assign miWindow to the
> > WindowMenu property of the MDI parent form using the Object Inspector at
> > design time.  Then any MDI child forms that you open during runtime will
> > automatically be added to the WindowMenu as needed, and the internal
> OnClick
> > handler will automatically handle bring windows to the front when you
> click
> > on the menu items for them.

> > > I have since changed the Caption property to have *NO* underline ('&')
> > > at all, yet the Add() function puts one in on the first character
> anyway!

> > Add() itself never directly modifies the Caption of menu items, all it
> does
> > is add the menu pointer to an internal list of pointers, that's all.
> > Something else must be changing the Caption behind the scenes.

> > Gambit

Other Threads