MFC problem - getting buttons to work on tab dialogs

This is a discussion on MFC problem - getting buttons to work on tab dialogs within the Windows Programming forums, part of the Platform Specific Boards category; I am an MFC novice so hopefully someone will know the answer to this question. For some reason I cannot ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    3

    MFC problem - getting buttons to work on tab dialogs

    I am an MFC novice so hopefully someone will know the answer to this question. For some reason I cannot get buttons to work when they are located on a dialog which is used as a content holder for a tab. Specifically here is what I did.

    Using Visual C++ in Visual Studio 2008, I created an MFC dialog-based application. In the resource editor, I added a tab control (IDC_MAIN_TABS) to the main dialog box.

    I then created a class called TabsMain, inheriting from CTabCtrl. In the main dialog’s *.h file, I put an object of this type using the statement:

    TabsMain TabsMainDlg;

    In the main dialog’s *.cpp file, in the DoDataExchange method, I put:

    DDX_Control(pDX, IDC_MAIN_TABS, TabsMainDlg);

    And in the OnInitDialog method, I added the following statements:

    Code:
    TabsMainDlg.InsertItem(0, _T("Image"));
    TabsMainDlg.Init();
    TabsMainDlg.ShowWindow(SW_SHOW);
    This creates one tab called “Image”. The TabsMainDlg.Init function will install a dialog as its content. So…

    Again in the resource editor, I created a dialog resource (IDD_DLG_TAB_IMG) and indicated style = “child”. I put a button on that dialog (IDC_BTN_SAVE). I created a class for this dialog, called Dlg_TabImage inheriting from CDialog.

    I then added an object of this class to the TabsMain.h file,

    Dlg_TabImage DlgImageTab;

    Then, in the TabsMain.cpp file, I added these lines to the TabsMain::Init() method:

    Code:
    DlgImageTab.Create(IDD_DLG_TAB_IMG, this); 
    DlgImageTab.ShowWindow(SW_SHOW);
    At this point the dialog (with the button) shows up in the tab.

    I then went back to the resource editor, and right-clicked the button that was put on the dialog Dlg_TabImage. I create an event handler for when the button is pushed. The event handler simply puts up a message box saying the button was clicked.

    So now in Dlg_TabImage.cpp there is a handler method:

    Code:
    void Dlg_TabImage::OnBnClickedBtnCapture()
    {
    	CString msg;
    	msg.Format(_T("This button doesn't work"));
    	AfxMessageBox(msg);
    }
    However this doesn’t work. I can put a button and event handler on the main dialog using the same steps, and it works fine. But the one on the dialog which provides content for the tab doesn’t work. What am I missing?

    Thanks

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Did you try setting the TAB to have the 'Image' tab selected (and so the small dlg active)?

    Otherwise the main dlg may not be passing on the mouse click.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    3
    I had not done that, so I added this statement in the OnInitDialog method of the main dialog box, after setting up the tabs:

    Code:
    TabsMainDlg.SetCurSel(0);
    I believe this should select the one and only tab. However this did not make any difference. I set a breakpoint at the event handler of the button on the tab dialog, and run the program. The button appears to be pushed but the breakpoint never breaks.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    3
    I totally apologize - I realized what is wrong and of course, it is a stupid mistake. I did not have the event handler associated with the button in question, it was associated with a non-existent button.

    Now the question is, how do I remove my entire post so nobody will waste their time reading it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual C++ 2008 MFC Tab Control
    By guitarist809 in forum Windows Programming
    Replies: 2
    Last Post: 07-20-2008, 09:37 AM
  2. Replies: 0
    Last Post: 11-08-2005, 01:28 AM
  3. buttons won't work when in child window!?
    By psychopath in forum Windows Programming
    Replies: 5
    Last Post: 06-17-2004, 02:18 PM
  4. Getting tab stops to work
    By jverkoey in forum Windows Programming
    Replies: 4
    Last Post: 07-06-2003, 02:27 PM
  5. Tab controls - MFC (revived)
    By Robert602 in forum Windows Programming
    Replies: 1
    Last Post: 01-22-2002, 11:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21