GetDlgItem() fails with one control but works with others... - Programmer

This is a discussion on GetDlgItem() fails with one control but works with others... - Programmer ; Hello I'm using GetDlgItem() in a MFC application on a Windows 2000 PC This is my scenario. I have a dialog, on which I have a property sheet, on which I have a number of property pages. On all of ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: GetDlgItem() fails with one control but works with others...

  1. GetDlgItem() fails with one control but works with others...

    Hello

    I'm using GetDlgItem() in a MFC application on a Windows 2000 PC

    This is my scenario. I have a dialog, on which I have a property sheet, on
    which I have a number of property pages.
    On all of these property pages I have controls of various types, typically
    labels, edit boxes, combo boxes, list boxes etc etc.
    This was working fine.

    Today I got an enhancement request which meant adding two edit boxes to one
    existing property page. Simple. This existing page already has a number of
    edit boxes, spin controls, buttons on it and already works fine.

    So I went about creating the edit boxes the way you do, using the control
    toolbar and drawing them out and then giving them their own ID_NAME ID's as
    you do. I Crtl+w'd for the Classwizard and associated a member variable
    with each control e.g. m_strName both of type CString.

    My problem is that in code when I call

    HWND hWnd = ::GetDlgItem( GetSafeHwnd(), IDC_EXPORTDIR );

    I get a NULL hWnd returned as if the control is not there!!!!
    GetDlgItem() fails and returns 1421 which is the error "Control ID not
    found"

    Because of this my DoDataExchange also fails

    DDX_Text(pDX, IDC_EXPORTDIR, m_strExport);
    DDX_Text(pDX, IDC_SYSTEMFOLDER, m_strSystemFolder);

    stepping into the DDX function I can see that this too does a GetDlgItem().

    The utterly bizarre thing is that I can GetDlgItem() on an existing control
    no problem:
    HWND hWnd = ::GetDlgItem( hSafeWnd, IDC_CURRTIME );
    This works no problem!!! The styles between this control which works and the
    style of the new control which doesn't are exactly the same.

    I'm no beginner to MFC or C++ but i'm no expert either, i've tried the
    obvious build clean, rebuild all etc etc, i've checked resource.h for those
    definitions (IDC_EXPORTDIR, IDC_SYSTEMFOLDER) and they both exist and both
    appear to have valid/unique id's

    Utterly stumpted.
    Your thoughts please....
    PT





  2. Re: GetDlgItem() fails with one control but works with others...

    Paul Tomlinson wrote:

    > So I went about creating the edit boxes the way you do, using
    > the control toolbar and drawing them out and then giving them
    > their own ID_NAME ID's as you do. I Crtl+w'd for the Classwizard
    > and associated a member variable with each control e.g. m_strName
    > both of type CString.
    >
    > My problem is that in code when I call
    >
    > HWND hWnd = ::GetDlgItem( GetSafeHwnd(), IDC_EXPORTDIR );
    >
    > I get a NULL hWnd returned as if the control is not there!!!!


    This just sounds like a case of the classwizard write some rubbish
    code and this is breaking the application.

    From my experience with MFC, I found it can be dangerous to rely
    just on the IDE to write the code as these types of problems do
    have a habit of popping up quite frequently

    > GetDlgItem() fails and returns 1421 which is the error "Control ID
    > not found"


    By GetDlgItem() I assume you are refering to the MFC method which
    is part of CWnd. It has been a long time since I wrote any MFC but
    last time I did, this method was just a simple wrapper for this
    Windows Win32 API call:

    HWND GetDlgItem(
    HWND hwnd, // handle of dialog box or parent window
    int sIdentifier // identifier of control
    );

    Now for this API to fail generally only three things can go wrong:

    1) The control with the ID of sIdentifier was not created.

    2) The control was created but does not have the ID of sIdentifier

    3) The hwnd upplied does not a child with the ID of sIdentifier

    My guess would be the MFC wizard has written code that matches
    this last case

    To test for case (1) just start your application and see if you
    can see the missing control.

    To test for case (2) start the micirosft spy++ program and use
    the find window menu to highlight the control. The check to see
    what ID the control has got.

    To test for case (3) repeat the steps above but this time look
    at the parent handle of the control. Now use spy to find the
    window you think is the parent and compare the two handles. If
    the hwnds differ then that is your answer.

    > The utterly bizarre thing is that I can GetDlgItem() on an existing
    > control no problem:
    > HWND hWnd = ::GetDlgItem( hSafeWnd, IDC_CURRTIME );
    > This works no problem!!!


    If you can see this control then it must exist. My suggestion is
    to check the handles and the ID's to make sure they match the code
    that the MFC wizard has written.

    Jussi Jumppanen
    Author of: Zeus for Windows (All new version 3.92 out now)
    "The C/C++, Cobol, Java, HTML, Python, PHP, Perl programmer's editor"
    Home Page: http://www.zeusedit.com

  3. Re: GetDlgItem() fails with one control but works with others...


    --
    -------------------------------------------------------------------------
    Free software - Baxter Codeworks www.baxcode.com
    -------------------------------------------------------------------------


    "Jussi Jumppanen" wrote in message
    news:4016FD7B.7048@zeusedit.com...
    > Paul Tomlinson wrote:
    >
    > >
    > > My problem is that in code when I call
    > >
    > > HWND hWnd = ::GetDlgItem( GetSafeHwnd(), IDC_EXPORTDIR );
    > >
    > > I get a NULL hWnd returned as if the control is not there!!!!

    >
    > This just sounds like a case of the classwizard write some rubbish
    > code and this is breaking the application.
    >

    I don't recall any of the VC++ wizards writing code like "HWND hWnd =
    ::GetDlgItem". That looks pretty much hand-coding or cut-and-paste to me.

    Your conclusions are about right - but Paul may also want to see what's
    being returned by GetSafeHwnd().



+ Reply to Thread