Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why does GetDlgItem always return to me NULL?

439 views
Skip to first unread message

Cecil A. Galbraith

unread,
Aug 7, 1996, 3:00:00 AM8/7/96
to gspe...@magmacom.com

Geoffrey Spencer wrote:
>
> I created in the AppStudio a (very) basic dialog. In fact, I asked
> AppStudio to create a new dialog and I did not make any changes to the
> layout. In this case, I should not need to create a class through the
> ClassWizard when I want to change the text of the buttons. Let us say
> that I want to change the button with text "Cancel" to "&Abort". I do
> the following, I got from the MSVC books which makes absolute sense to
> me. I step through the code and I discover that I create the dialog
> (theDialog) without any problem but the cancel button (cancel) returns
> an error. What did I screw up here?
>
> example of primitive code:
> CDialog theDialog( IDD_TEST_DIALOG );
> CButton *cancel = (CButton *)theDialog.GetDlgItem( IDCANCEL );
>
> Thanks for any help,
>
> Geoffrey Spencer
> gspe...@fed.ism.ca

I don't understand what you are asking. Can you be more specific
(include more code)?

--
Cecil Galbraith
cgal...@concentric.net
CustomSoft

Geoffrey Spencer

unread,
Aug 7, 1996, 3:00:00 AM8/7/96
to

Geoffrey Reynolds

unread,
Aug 7, 1996, 3:00:00 AM8/7/96
to

On 7 Aug 1996 14:22:10 GMT, gspe...@fed.ism.ca (Geoffrey Spencer)
wrote:

Until you call theDialog.DoModal (), GetDlgItem will return NULL
because the control is not created yet. DoModal () does not return
until the dialog box is exited, so you will need to customize the
controls from within the dialog itself. To do this, you will have to
create your own class for the dialog (using Class Wizard) derived from
CDialog. Inside the class, handle the ON_WM_INITDIALG message
yourself (Class Wizard will name the handler OnInitDialog), and after
the call to the base class's OnInitDialog, you can then access the
controls and do whatever you want with them. While you're at it, you
may want to create member variables for the controls using app wizard,
which eliminates the need for the GetDlgItem call. Again, the member
variables will not be usable until the call to OnInitDialog in the
base class.

Hope this helped,
Jeff

Geoffrey Spencer

unread,
Aug 7, 1996, 3:00:00 AM8/7/96
to

"Cecil A. Galbraith" <cgal...@concentric.net> wrote:

>Geoffrey Spencer wrote:
>>
>> I created in the AppStudio a (very) basic dialog. In fact, I asked
>> AppStudio to create a new dialog and I did not make any changes to the
>> layout. In this case, I should not need to create a class through the
>> ClassWizard when I want to change the text of the buttons. Let us say
>> that I want to change the button with text "Cancel" to "&Abort". I do
>> the following, I got from the MSVC books which makes absolute sense to
>> me. I step through the code and I discover that I create the dialog
>> (theDialog) without any problem but the cancel button (cancel) returns
>> an error. What did I screw up here?
>>
>> example of primitive code:
>> CDialog theDialog( IDD_TEST_DIALOG );
>> CButton *cancel = (CButton *)theDialog.GetDlgItem( IDCANCEL );
>>
>> Thanks for any help,
>>
>> Geoffrey Spencer
>> gspe...@fed.ism.ca
>

>I don't understand what you are asking. Can you be more specific
>(include more code)?
>

Sorry about that. I wanted a simpler way of modifying a CStatic
text's field (by using SetWindowText) before the window is displayed
to the user without having to create a new class for the dialog using
ClassWizard. Am I still not expressing myself clearly (one of my
faults.).

Thanks

Geoffrey Spencer
gspe...@fed.ism.ca

Eric Lamontagne

unread,
Aug 7, 1996, 3:00:00 AM8/7/96
to

Geoffrey Spencer <gspe...@fed.ism.ca> wrote in article
<4uahac$r...@nr1.ottawa.istar.net>...

Hi Geoffrey,

The Dialog is not created, only the class is. The dialog is created after
you call "Create" or "DoModal". In the first case, you have a modeless
dialog and the DoModal do what it says :-). If you want a modal dialog,
you can change text of control in InitDialog (You will have to create your
own class derived from CDialog!)

Hope it help you!

Eric Lamontagne
elamo...@clinidata.com

Jeff Rosen

unread,
Aug 8, 1996, 3:00:00 AM8/8/96
to

gspe...@fed.ism.ca (Geoffrey Spencer) wrote:

> <snip>


>
>example of primitive code:
>CDialog theDialog( IDD_TEST_DIALOG );
> CButton *cancel = (CButton *)theDialog.GetDlgItem( IDCANCEL );

If this is really all the code there is, then it doesn't work because
the Windows objects for theDialog and its controls haven't been
created, ie don't exist yet.

It's a little unorthodox but maybe something like the following will
help...
CDialog theDialog( IDD_TEST_DIALOG );
theDialog.Create(appropriate parameters...);


CButton *cancel = (CButton *)theDialog.GetDlgItem( IDCANCEL );

theDialog.DestroyWindow();

Geoffrey Reynolds

unread,
Aug 8, 1996, 3:00:00 AM8/8/96
to

On Thu, 08 Aug 1996 02:30:52 GMT, jro...@metronet.com (Jeff Rosen)
wrote:

So the dialog box will be up for all of 2 milliseconds :). The
Create, GetDlgItem, and DestroyWindow functions will all be called
without delays, so the user will probably never see the dialog box.
See my previous post for an explanation of how to do what this guy
wants, and why a class is required to accomplish what he wants.

>>
>>Thanks for any help,
>>
>>
>>Geoffrey Spencer
>>gspe...@fed.ism.ca

Jeff

Angel E. Brisighelli

unread,
Aug 8, 1996, 3:00:00 AM8/8/96
to

In <4ua8qi$j...@nr1.ottawa.istar.net> gspe...@fed.ism.ca (Geoffrey Spencer)
wrote:

>I created in the AppStudio a (very) basic dialog. In fact, I asked
>AppStudio to create a new dialog and I did not make any changes to the
>layout. In this case, I should not need to create a class through the
>ClassWizard when I want to change the text of the buttons. Let us say
>that I want to change the button with text "Cancel" to "&Abort". I do
>the following, I got from the MSVC books which makes absolute sense to
>me. I step through the code and I discover that I create the dialog
>(theDialog) without any problem but the cancel button (cancel) returns
>an error. What did I screw up here?
>

>example of primitive code:
>CDialog theDialog( IDD_TEST_DIALOG );
> CButton *cancel = (CButton *)theDialog.GetDlgItem( IDCANCEL );
>

>Thanks for any help,
>
>
>Geoffrey Spencer
>gspe...@fed.ism.ca


Hola:

You must call DoModal(), in order to create the DialogBox.

Chau
--
//--------------------------------------------------------------------
// Angel E. Brisighelli Soft Data S.A.
// abr...@softdata.satlink.net Malabia 853
// Buenos Aires, Argentina Tel. (54) 1-777-0444
//--------------------------------------------------------------------
//


David Spooner

unread,
Aug 8, 1996, 3:00:00 AM8/8/96
to


Geoffrey Reynolds <grey...@internet.kronos.com> wrote in article
<3209e52c...@news.kronos.com>...


> On Thu, 08 Aug 1996 02:30:52 GMT, jro...@metronet.com (Jeff Rosen)
> wrote:
>
> >gspe...@fed.ism.ca (Geoffrey Spencer) wrote:
> >
> >> <snip>
> >>

> >>example of primitive code:
> >>CDialog theDialog( IDD_TEST_DIALOG );
> >> CButton *cancel = (CButton *)theDialog.GetDlgItem( IDCANCEL );

> >If this is really all the code there is, then it doesn't work because
> >the Windows objects for theDialog and its controls haven't been
> >created, ie don't exist yet.
> >
> >It's a little unorthodox but maybe something like the following will
> >help...
> > CDialog theDialog( IDD_TEST_DIALOG );
> > theDialog.Create(appropriate parameters...);

> > CButton *cancel = (CButton *)theDialog.GetDlgItem( IDCANCEL );

> > theDialog.DestroyWindow();
>
> So the dialog box will be up for all of 2 milliseconds :). The
> Create, GetDlgItem, and DestroyWindow functions will all be called
> without delays, so the user will probably never see the dialog box.
> See my previous post for an explanation of how to do what this guy
> wants, and why a class is required to accomplish what he wants.
>

could he install a message hook filter, catch the WM_INITDIALOG msg and
hack it that way, granted the performance would take a bit of a hit, and
its a nasty way of doing what he wants, but it would work., It probably
would be simpler/quicker/compacter to just make a new class.

There's always an alternative, even if its a stupid one ;-)

David

Geoffrey Reynolds

unread,
Aug 9, 1996, 3:00:00 AM8/9/96
to

On 8 Aug 1996 18:31:04 GMT, "David Spooner" <dol...@dircon.co.uk>
wrote:

By the time he installs a message hook filter and all that, he
probably has more code than if he created a new class. He also would
have something that I wouldn't want to maintain. I really think we
should encourage him to do it the correct way rather than try and
figure out how to hack it up :).

Jeff

0 new messages