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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox in menu

Status
Not open for further replies.

Jiko

Technical User
Apr 5, 2002
35
SE
We have created a menu in MFC. In our view-menu we have 3 menu-alternatives. We want to have a checkbox to each alternative.
In Set Item Properties when we created the alternatives we marked CHECKED on our third alternative. We want to be able to change this in runtime. We also want to add a variable to this checkbox.

IDR_MENU1 MENU DISCARDABLE
BEGIN
POPUP "&View"
BEGIN
MENUITEM "&Program",ID_Visa_Program
MENUITEM "&Laddningskurva",ID_Visa_Laddningskurva

MENUITEM "&Antal Celler", ID_Visa_Antalceller, CHECKED

END
END
 
add a bool variable for each of your menu alternatives(say bMenu1 for the first menu item) into your view class.
then override UPDATE_COMMAND_UI message handler of your menu item. in its handler do:
CView::OnUpdateMenu1(CCmdUI* pCmdUI)
{
if (bMenu1) pCmdUI->SetCheck(TRUE);
else pCmdUI->SetCheck(FALSE);
}

based on your program logic you can change your bool variables to make your menu items checked or unchecked.
 
what do mean with override UPDATE_COMMAND_UI message handler of your menu item.
 
then we click on an alternative we want to see that it's marked
 
then we click on an alternative in the meny we want to see that it's marked, how do we do that?
 
Use the Class Wizard to do this. Find the Menus' Resource ID add the Functions then enter the code required. When you have done this you will have something like this at the bottom of the Source File.

void CYourCalss::OnMyMenuOption()
{
// Enter your code here to take the actions you want when the user selects the menu option
}


void CYourClass::OnUpdateMyMenuOption(CCmdUI* pCmdUI)
{
// Add your code here to Check UnCheck the menu option and or to Change it's Text.
}
William
Software Engineer
ICQ No. 56047340
 
We have used this two functions but it doesn't work. Can't we use this in a dialog based program, do we have to use a SDI or MDI program (seen example on that in a book).
There they put it in a view class, but we dont have one.

void CST7kommDlg::OnVisaAntalceller()
{
// TODO: Add your command handler code here
m_Test = TRUE;

}

void CST7kommDlg::OnUpdateVisaAntalceller(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if (m_Test) pCmdUI->SetCheck(TRUE);
else pCmdUI->SetCheck(FALSE);
}
 
If you have a menu resource then I see no reason as to why it doesn't work. Try adding an AfxMessageBox("Message") in the OnVisaAntalceller() just to be sure that that it's actually being called.

If you don't see the Message Box then check your message map.

William
Software Engineer
ICQ No. 56047340
 
Yes, that works, but next time I open the popup-menu I want to see which choices I have selected before. AN empty combobox if nothing is selected and a marked if it's selected. I'm going to use the choices to make different editboxes, comboboxes and buttons active in the main window. Different user want to be able to set there own style on the window.
And i don't want this choices to be made in the mainwindow.
That's easy to be done like this:
UpdateData(TRUE);
if (m_bEnablePgm == TRUE)// a variabel connected to a checkbox (marked/unmarked)
{
GetDlgItem(IDC_PROGTORUN) -> EnableWindow(TRUE);
}
else
{
GetDlgItem(IDC_PROGTORUN) -> EnableWindow(FALSE);
}
 
Well, CDialog (and it's derivatives) does not have the CFrameWnd logic for WM_INITMENUPOPUP (the message received by a parent window before
a menu is displayed) which calls the Update handlers...
That is, you can not make the UPDATE_COMMAND_UI work directly. There are many
ways to overcome this limitation (for example, duplicate the logic for
CFrameWnd::OnInitMenuPopup() in your Dialog class...) .
So use SDI, MDI if possible. Otherwise look at for the solution.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top