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

How to Set Static Text value?

3,622 views
Skip to first unread message

Landon

unread,
Jul 9, 2008, 9:31:03 PM7/9/08
to
I use MFC VC++ 4.2.

I have a main window and a few child windows. If I click a button on the
main window, I must set the text of Static Text on the child windows.

I have tried to cast it to CWnd but it still don't work.
( ( CWnd* ) GetDlgItem( IDC_STATIC ) )->SetWindowText( "Insert Data" );

How to solve this?

Thank you.

Tom Serface

unread,
Jul 9, 2008, 10:01:26 PM7/9/08
to
You need to give the static control another ID (other than IDC_STATIC which
equates to -1) and then you will be able to set it. At that point you could
also assign a control and/or data variable to the control like you would any
other control.

The key is changing the ID to something that is unique.

Tom

"Landon" <Lan...@discussions.microsoft.com> wrote in message
news:1A770FFD-50C0-4EAA...@microsoft.com...

Joseph M. Newcomer

unread,
Jul 10, 2008, 12:11:28 AM7/10/08
to
See below...

****
IDC_STATIC is not a unique name for a static control, it's the "catch-all" name for
controls you don't care about. So the first thing you do is go to the properties of any
stack control you want to manipulate and change its ID to something useful, such as

IDC_INSERT_PROMPT

or something else meaningful (in general, you NEVER want to retain the silly names that
Visual Studio assigns, you ALWAYS want to change them to something meaningful!)

You will never use GetDlgItem for any purpose whatsoever, except in extremely rare and
exotic circumstances of which this is not an example. Instead, you will right-click on
the control, select "Add variable", and add what is called a "Control variable". I tend
to use names that start with c_ for my controls, such as

c_InsertPrompt

so you will automatically get a declaration

CStatic c_InsertPrompt;

in your header file.

You should not put literal text in an application; any string you use should be in your
STRINGTABLE (necessary if you want to support localization; develop good habits early)

CString s;
s.LoadString(IDS_INSERT_DATA);
c_InsertPrompt.SetWindowText(s);

You can also do

c_InsertPrompt.SetWindowText(_T("Insert Data"));

do not use "xxx" for literal strings; it is a bad programming habit to have in programming
MFC or C/C++ in general in Windows. Always use _T() so you are Unicode-ready at all
times.

Note that there would have been no need to cast GetDlgItem to a CWnd* because it already
IS a CWnd*; don't add gratuitous casts when they are not needed.

See my essays on dialogs on my MVP Tips site. These include the essays on Adoiding
GetDlgItem, avoiding UpdateData, dialog control management, and even dialog-based
applications. These have useful tips that I've learned over the years to manage dialogs
well.
joe
****


>
>How to solve this?
>
>Thank you.

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

neeraj sharma

unread,
Aug 27, 2010, 7:55:39 AM8/27/10
to
Dear Friend,

Try the following for VC++.Net :


CString UName=System::Environment::UserName;

GetDlgItem(IDC_STATIC)->SetWindowTextA("Welcome : "+UName);


>> On Wednesday, July 09, 2008 10:01 PM Tom Serface wrote:

>> You need to give the static control another ID (other than IDC_STATIC which
>> equates to -1) and then you will be able to set it. At that point you could
>> also assign a control and/or data variable to the control like you would any
>> other control.
>>
>> The key is changing the ID to something that is unique.
>>
>> Tom
>>
>> "Landon" <Lan...@discussions.microsoft.com> wrote in message
>> news:1A770FFD-50C0-4EAA...@microsoft.com...


>> Submitted via EggHeadCafe - Software Developer Portal of Choice
>> Changing WCF Service Implementation at Runtime
>> http://www.eggheadcafe.com/tutorials/aspnet/d9263dcc-f7ed-42f3-bc96-321461be3306/changing-wcf-service-implementation-at-runtime.aspx

Joseph M. Newcomer

unread,
Aug 27, 2010, 10:55:16 AM8/27/10
to
See below...

On Fri, 27 Aug 2010 11:55:39 GMT, neeraj sharma <neerajk...@gmail.com> wrote:

>Dear Friend,
>
>Try the following for VC++.Net :
>
>
>CString UName=System::Environment::UserName;
>
>GetDlgItem(IDC_STATIC)->SetWindowTextA("Welcome : "+UName);

****
Why are you using GetDlgItem? And you CANNOT use IDC_STATIC, because LOTS of controls
have the ID IDC_STATIC; you MUST change the ID. And you should bind it to a control
variable. You should NOT be using SetWindowTextA, but SetWindowText, and you should write
this as

c_UserName.SetWindowText(_T("Welcome: ") + UName;

and you should not even include the word "Welcome" in your source code because you cannot
localize it easily; you should get this string from your STRINGTABLE, e.g.,

CString UName = System::Environment::UserName;
CString greeting;

greeting.LoadString(IDS_WELCOME):

c_UserName.SetWindowText(greeting + UName);

If you are writing more than one GetDlgItem per year, you are not using MFC correctly. You
are obviously using Intellinonsense, which erroneously chooses SetWindowTextA or
SetWindowTextW, due to fundamental design failures. Don't trust it.
joe

*****


>
>
>> On Wednesday, July 09, 2008 9:31 PM Lando wrote:
>
>> I use MFC VC++ 4.2.
>>
>> I have a main window and a few child windows. If I click a button on the
>> main window, I must set the text of Static Text on the child windows.
>>
>> I have tried to cast it to CWnd but it still don't work.
>> ( ( CWnd* ) GetDlgItem( IDC_STATIC ) )->SetWindowText( "Insert Data" );
>>
>> How to solve this?
>>
>> Thank you.
>
>
>>> On Wednesday, July 09, 2008 10:01 PM Tom Serface wrote:
>
>>> You need to give the static control another ID (other than IDC_STATIC which
>>> equates to -1) and then you will be able to set it. At that point you could
>>> also assign a control and/or data variable to the control like you would any
>>> other control.
>>>
>>> The key is changing the ID to something that is unique.
>>>
>>> Tom
>>>
>>> "Landon" <Lan...@discussions.microsoft.com> wrote in message
>>> news:1A770FFD-50C0-4EAA...@microsoft.com...
>
>
>>> Submitted via EggHeadCafe - Software Developer Portal of Choice
>>> Changing WCF Service Implementation at Runtime
>>> http://www.eggheadcafe.com/tutorials/aspnet/d9263dcc-f7ed-42f3-bc96-321461be3306/changing-wcf-service-implementation-at-runtime.aspx

Joseph M. Newcomer

unread,
Aug 27, 2010, 10:56:51 AM8/27/10
to
It just occurred to me: IDC_STATIC is defined as -1, and the window ID is 65535, so it
can't find an window whose ID is 65535, since it is looking for one whose ID is -1. Do
not use IDC_STATIC, ever. If you had done this right, and added a variable, VS would have
complained that you cannot create a variable for IDC_STATIC as an ID.
joe

On Fri, 27 Aug 2010 11:55:39 GMT, neeraj sharma <neerajk...@gmail.com> wrote:

0 new messages