Home > How-To Library > Forms

How to enable/disable submenus of Edit menu (Copy, Cut, Clear, Undo, and so on) based on text selection

**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * ****************************************************************
'The state of most of the commands in a typical Edit menu (Copy, Cut, Clear, 'Undo, and so on) depends on whether any text is currently selected in the active 'control. In this case, changing the menu state any time a condition changes '(because the user selects or deselects text in the active control, for example) 'is a waste of time, and it also requires a lot of code. Therefore, it's 'preferable to set the state of those menu commands in the parent menu's Click 'event just before displaying the menu: Private Sub mnuEdit_Click() ' The user has clicked on the Edit menu, ' but the menu hasn't dropped down yet. On Error Resume Next ' Error handling is necessary because we don't know if ' the Active control actually supports these properties. mnuEditCopy.Enabled = (ActiveControl.SelText <> "") mnuEditCut.Enabled = (ActiveControl.SelText <> "") mnuEditClear.Enabled = (ActiveControl.SelText <> "") End Sub

If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.