You may want to adjust pop-up menu items before displaying the menu, just as you may want to enable or disable items on a regular menu. With a regular menu, you can handle the OnClick event for the item at the top of the menu.
With a pop-up menu, however, there is no top-level menu bar, so to prepare the pop-up menu commands, you handle the event in the menu component itself. The pop-up menu component provides an event just for this purpose, called OnPopup.
procedure TEditForm.Edit1Click(Sender: TObject); var HasSelection: Boolean; begin Paste1.Enabled := Clipboard.HasFormat(CF_TEXT); Paste2.Enabled := Paste1.Enabled;{Add this line} HasSelection := Editor.SelLength <> 0; Cut1.Enabled := HasSelection; Cut2.Enabled := HasSelection;{Add this line} Copy1.Enabled := HasSelection; Copy2.Enabled := HasSelection;{Add this line} Delete1.Enabled := HasSelection; end;
void __fastcall TMainForm::EditEditClick(TObject *Sender) { // enable or disable the Paste menu item Paste1->Enabled = Clipboard()->HasFormat(CF_TEXT); Paste2->Enabled = Paste1->Enabled; // add this line bool HasSelection = (RichEdit1->SelLength > 0); // true if text is selected Cut1->Enabled = HasSelection; // enable menu items if HasSelection is true Cut2->Enabled = HasSelection; // add this line Copy1->Enabled = HasSelection; Copy2->Enabled = HasSelection; // add this line Delete1->Enabled = HasSelection; }
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
|
What do you think about this topic? Send feedback!
|