Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enabling a Menu

Status
Not open for further replies.

mpsoutine

Programmer
Jan 6, 2003
87
US
Hi,

I'm stuck on problem. When I click on an item in list control I want to enable a menu item. I have a menu selection: option|Add|Edit|Delete. When the program starts Edit and Delete should be diabled. When I select an item in the list control, Edit and Delete should be enabled.

Question: Should I select the "grayed" option from menu item properties?

Question: Since I want to enable the menu item when I click on the list control it makes sense to put the code within "OnClickListPerson" Is this going in the right direction?

Thanks.
 
You have to implement the ON_UPDATE_COMMAND_UI command+handler for those menu items that you want to be enabled/disabled depending on the run time conditions.

Use Resource Editor ->Menu->ID_..-> Option->Add (menu item)->ClassWizard to generate in the right place
the YourClass::OnUpdateAdd(CCmdUi* pCmdUI), OnUpdateDelete() and OnUpdateEdit() handlers;
In each of these handlers you have to put the code to enable/disable the corresponding menu item:
void YourClass::OnUpdateEdit(CCmdUi* pCmdUI)
{
bool bEnable = ...;
pCmdUi->Enable(bEnable);
}

Here I put an example with two menu items : Receipt and Invoice which are enabled/disabled if there is a selection item in a ListView.

BEGIN_MESSAGE_MAP(CPaymentResultView, CAppListView)

ON_COMMAND(ID_ACTIONS_RECEIPT, OnActionsReceipt)
ON_COMMAND(ID_ACTIONS_INVOICE, OnActionsInvoice)
END_MESSAGE_MAP()

// This methods is called when the "Receipt" menu item associated to ID_ACTIONS_RECEIPT is clicked
void CPaymentResultView::OnActionsReceipt()
{
HandleReceipt(CCMPayment::Receipt);
}
// This method is called when the the "Invoice" menu item associated to ID_ACTIONS_INVOICE is clicked
void CPaymentResultView::OnActionsInvoice()
{
HandleReceipt(CCMPayment::Invoice);
}
BEGIN_MESSAGE_MAP(CPaymentSearchFrame, CApp2WaySplitterFrame)
ON_UPDATE_COMMAND_UI( ID_ACTIONS_RECEIPT , OnUpdateActionsReceipt)
ON_UPDATE_COMMAND_UI( ID_ACTIONS_INVOICE , OnUpdateActionsInvoice)
END_MESSAGE_MAP()


//This method is called automatically when the user click on the popup menu but before
// displaying it. So the menu item (Receipt) will disabled or enabled if there is an item
// selected in the result view. So, you should not call such function directly.

void CPaymentSearchFrame::OnUpdateActionsReceipt(CCmdUI* pCmdUI)
{
CPaymentResultView* pResultView = GetResultView();
BOOL bEnable = (pResultView != NULL ? pResultView->GetSelectedItem() >= 0 : FALSE);
pCmdUI->Enable( bEnable );
}

I hope this help you.

-obislavu-
 
Hi -obislavu-,

It did help. It pointed me in the right direction. I did have the ON_COMMAND for each menu item. I figured out what I needed to do for each of the UPDATE_COMMAND_UI functions. Here is a sample code.


void CBirthRecordView::OnUpdateDeletePerson(CCmdUI* pCmdUI)
{
CListCtrl* pList;
pList = &m_lcPerson;


BOOL bEnable = (-1 != m_lcPerson.GetNextItem(-1,LVNI_SELECTED)?TRUE:FALSE);
pCmdUI->Enable(bEnable);
}




void CBirthRecordView::OnUpdateEditPerson(CCmdUI* pCmdUI)
{
CListCtrl* pList;
pList = &m_lcPerson;


BOOL bEnable = (-1 != m_lcPerson.GetNextItem(-1,LVNI_SELECTED)?TRUE:FALSE);
pCmdUI->Enable(bEnable);

}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top