PopupMenu (C++)
Description
This example uses two edit boxes and one pop-up menu on a form. The pop-up menu contains Cut, Copy, and Paste commands. This code makes the pop-up menu available to both edit boxes.
Code
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
PopupMenu1->AutoPopup = true;
Edit1->PopupMenu = PopupMenu1;
Edit2->PopupMenu = PopupMenu1;
}
/*
These are the cut, copy, and paste OnClick events for the commands
on the pop-up menu. Double-click the pop-up menu and add three
TMenuItems. Then set the captions to "Cut", "Copy", and "Paste".
The names will be automatically set to "Cut1", "Copy1", and
"Paste1". Then double-click the OnClick events and they will
be named "Cut1Click", "Copy1Click", and "Paste1Click" respectively.
Note: Only the selected portion of the string in the edit box will
be copied, cut, or replaced.
*/
void __fastcall TForm1::Copy1Click(TObject *Sender)
{
TComponent *pComponent = PopupMenu1->PopupComponent;
if (pComponent)
{
if (pComponent->ClassNameIs("TEdit"))
(dynamic_cast<TEdit *>(pComponent))->CopyToClipboard();
else
MessageBeep(0);
}
else
MessageBeep(0);
}
void __fastcall TForm1::Cut1Click(TObject *Sender)
{
TComponent *pComponent = PopupMenu1->PopupComponent;
if (pComponent)
{
if (pComponent->ClassNameIs("TEdit"))
(dynamic_cast<TEdit *>(pComponent))->CutToClipboard();
else
MessageBeep(0);
}
else
MessageBeep(0);
}
void __fastcall TForm1::Paste1Click(TObject *Sender)
{
TComponent *pComponent = PopupMenu1->PopupComponent;
if (pComponent)
{
if (pComponent->ClassNameIs("TEdit"))
(dynamic_cast<TEdit *>(pComponent))->PasteFromClipboard();
else
MessageBeep(0);
}
else
MessageBeep(0);
}
Uses
- Vcl.Menus.TPopupMenu.PopupComponent ( fr | de | ja )
- Vcl.Controls.TControl.PopupMenu ( fr | de | ja )
- Vcl.Menus.TMenuItem.OnClick ( fr | de | ja )