dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: menu on a button click event

  1. #1
    Join Date
    Feb 2007
    Posts
    258

    menu on a button click event

    Hi all,

    I am having a main exe dialog box from which another dialog box opens up which is having a button and a edit box. On clicking button my menu appears. what i want is when i click on menu item particular text should be written in edit box. when i open class wizard for menu items and choose my dlg class and write OnCommand code over there a debug error is given and when i choose second class as base class nothing happens on click event

    Can anybody help me in this

    Thanks in advance

  2. #2
    Join Date
    May 1999
    Location
    DELAWARE, USA
    Posts
    9,917

    Re: menu on a button click event

    To clarify:
    Unless you are using some language other than C++, you do not handle button click event, you handle command message that system generated as the result of clicking on the button.
    Several things are unclear:
    Quote Originally Posted by VCProgrammer
    when i open class wizard for menu items and choose my dlg class and write OnCommand code over there . . .
    Could you explain what do you mean by that? Why do you need OnCommand override if you use wizard to map command message?

    Quote Originally Posted by VCProgrammer
    a debug error is given and when i choose second class as base class nothing happens on click event
    What debug error? What do you mean by second class?
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  3. #3
    Join Date
    Feb 2007
    Posts
    258

    Re: menu on a button click event

    by OnCommand i mean the COMMAND function which we use using Class wizard and by second class i mean class for second dialog box.

  4. #4
    Join Date
    May 1999
    Location
    DELAWARE, USA
    Posts
    9,917

    Re: menu on a button click event

    You do not need OnCommand.
    Use class wizard to map menu item ID to a message handler in a dialog class you want to handle message.
    How you invoke a menu?
    Consider posting your project; this will be the fastest way of resolving your problem.
    Do not include debug/release directories.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  5. #5
    Join Date
    Feb 2007
    Posts
    258

    Re: menu on a button click event

    I am doing that only...Check my code
    Attached Files Attached Files

  6. #6
    Join Date
    May 1999
    Location
    DELAWARE, USA
    Posts
    9,917

    Re: menu on a button click event

    res folder is missing.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  7. #7
    Join Date
    May 1999
    Location
    DELAWARE, USA
    Posts
    9,917

    Re: menu on a button click event

    Your post is misleading, that is why I have asked you to post your project.
    You are not using OnCOmmand, wizard is mapping WM_COMMAND message to a handler using ON_COMMAND macro.
    You have to read more about MFC message maps.
    Also, next time you post your problem try to explain it precisely using exact terms and functions/macros names.
    Reading MSDN description of the TrackPopupMenu, would tell you that last parameter is a pointer to a window that owns the menu (will receive command messages).
    Never use something like
    Code:
     menu.GetSubMenu(0)->
    without checking a return value. Code like this is a recipe for disaster. If GetSubMenu returns NULL your application crashes.
    To fix your problem:
    Code:
     void CDir::OnButton1() 
    {
    	CPoint pt;
    	CRect rect;
    	GetDlgItem(IDC_BUTTON1)->GetWindowRect(&rect);
    
    	CMenu menu;
    	menu.LoadMenu( IDR_MENU1);
    
    	CMenu *pSubMenu = menu.GetSubMenu(0);  
    	ASSERT(pSubMenu);
    
    	if(NULL == pSubMenu)
    	{
    		return;
    	}
    
    
    	pSubMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, 
    									rect.left, rect.bottom + 2, AfxGetMainWnd());
    }
    always check validity of pointers and return function values and handle errors to prevent your app from crashing.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)