Board index » cppbuilder » Click event for a TMenuItem created at runtime?

Click event for a TMenuItem created at runtime?

I have an MDI application. When I open a new form, I add a
TMenuItem to the Windows menu to display the form's caption. A
user can go to the Windows menu, see all the open forms, click
on one and have it become the active form (standard Windows
stuff here).

I can't figure out how to capture the click event of this
dynamically created TMenuItem. If I add three new menu items:

Name      Caption
===========================
MdiForm1  Caillouet, Darryl
MdiForm2  Doe, John
MdiForm3  Smith, Bob

When I click on "Doe, John", how do I capture the fact that
"MdiForm2" has been clicked since the "MdiForm2Click" procedure
does not exist like the ones created at design time?

Thanks,
Darryl

 

Re:Click event for a TMenuItem created at runtime?


Quote
"Darryl Caillouet" <darryl.caillo...@alltel.net> wrote:
> [...] I can't figure out how to capture the click event of
> this dynamically created TMenuItem. If I add three new menu items:

Follow this thread:

http://newsgroups.borland.com/cgi-bin/dnewsweb?utag=&group=borland.pu...

~ JD

Re:Click event for a TMenuItem created at runtime?


Quote
"Darryl Caillouet" <darryl.caillo...@alltel.net> wrote in message

news:3eca360e$1@newsgroups.borland.com...

Quote
> I have an MDI application. When I open a new form, I
> add a TMenuItem to the Windows menu to display the
> form's caption.

Any particular reason why you are handling all of that manually instead of
letting the VCL handle it for you?  Have you looked at the
TCustomForm::WindowMenu property yet?  Simply set the MainForm's WindowMenu
property to the appropriate TMenuItem, and then the MDI child windows will
be displayed in and removed from that menu automatically.

Gambit

Re:Click event for a TMenuItem created at runtime?


"Remy Lebeau \(TeamB\)" <gambi...@yahoo.com> wrote:

Quote
>Any particular reason why you are handling all of that
>manually instead of letting the VCL handle it for you?  

I have a very good reason: ignorance. ;-)

Being new to BCPPB, I didn't know that feature was available.

Thanks for the tip,

Darryl

Re:Click event for a TMenuItem created at runtime?


(I post this question here in many places but get no response)
if I put the menu in a control bar, then the TMenuItem doesn't work.

"Remy Lebeau (TeamB)" <gambi...@yahoo.com> wrote in message
news:3eca6760$1@newsgroups.borland.com...

Quote

> "Darryl Caillouet" <darryl.caillo...@alltel.net> wrote in message
> news:3eca360e$1@newsgroups.borland.com...

> > I have an MDI application. When I open a new form, I
> > add a TMenuItem to the Windows menu to display the
> > form's caption.

> Any particular reason why you are handling all of that manually instead of
> letting the VCL handle it for you?  Have you looked at the
> TCustomForm::WindowMenu property yet?  Simply set the MainForm's
WindowMenu
> property to the appropriate TMenuItem, and then the MDI child windows will
> be displayed in and removed from that menu automatically.

> Gambit

Re:Click event for a TMenuItem created at runtime?


I do understand. Needed to do the same myself today. Had to create code which created TMenuItems for
a PopupMenu dynamicly whithin it's OnPopup event. But anyway, if it's usefull for anyone, here is my
code that I used:

//---------------------------------------------------------------------------

void __fastcall TFormLayouts::PopupMenuPopup(TObject *Sender)
{
        // Get my stringlist from the selected row of the ListBox:
        TStringList *strlstTemporary = (TStringList*)LB_Gfx->Items->Objects[LB_Gfx->ItemIndex];

        // Fill the Popup Menu with Items:
        for (int iItems = 0; iItems < slTemp->Count; iItems++)
                {
                // Create the item:
                TMenuItem *miItem = new TMenuItem(this);
                miItem->Caption   = strlstTemporary->Strings[iItems];
                miItem->MenuIndex = iItems;
--->         miItem->OnClick   = MenuItemClick;

                // Add the Item to the Menu:
                dynamic_cast<TPopupMenu*>(Sender)->Items->Add(miItem);
                }

Quote
}

//---------------------------------------------------------------------------

void __fastcall TMyForm::MenuItemClick(TObject *Sender)
{
        // Get the Index in the Menu:
        int iIndex = dynamic_cast<TMenuItem*>(Sender)->MenuIndex;

       // Your code handling the Click:

Quote
}

//---------------------------------------------------------------------------

Good luck,

Peter

Other Threads