Discussion:
Setting value to static text control at runtime
(too old to reply)
SM
2010-05-14 14:32:52 UTC
Permalink
Hi,
I have a dialog box(About dialog) and it contains three static text
controls.
Now I need to set the value to the one static control in run time, so
I changed the ID from IDC_STATIC to IDC_Test
CSimpleDialog<IDD_TESTDLG> dlgTest;
//dlgTest.SetDlgItemTextA(IDC_Test,_T("Test"));
//GetDlgItem(IDC_SOC_Version).SetDlgItemTextA(IDC_Test,_T("Test"));
dlgTest.DoModal(m_hWnd);
GetDlgItem(IDC_SOC_Version).SetDlgItemTextA(IDC_SOC_Version,_T("Test"));

while running the application i'm getting assertion as
---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Assertion Failed!

Program: ...\AllVCLanguageSamples\C++\MFC\ole\TstCon\Release
\TstCon32.exe
File: d:\Microsoft Visual Studio 9.0\VC\atlmfc\include\atlwin.h
Line: 1320

Expression: ::IsWindow(m_hWnd)

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)
---------------------------
Abort Retry Ignore
---------------------------
I tried moving the getdlgitem before domodal and it also throw error.
Tried SetDlgItemTextA , same error only.
How can I set value to static text control?

Thanks,
Sony
David Lowndes
2010-05-14 15:03:48 UTC
Permalink
Post by SM
I have a dialog box(About dialog) and it contains three static text
controls.
Now I need to set the value to the one static control in run time, so
I changed the ID from IDC_STATIC to IDC_Test
CSimpleDialog<IDD_TESTDLG> dlgTest;
//dlgTest.SetDlgItemTextA(IDC_Test,_T("Test"));
//GetDlgItem(IDC_SOC_Version).SetDlgItemTextA(IDC_Test,_T("Test"));
dlgTest.DoModal(m_hWnd);
GetDlgItem(IDC_SOC_Version).SetDlgItemTextA(IDC_SOC_Version,_T("Test"));
while running the application i'm getting assertion as
You can only alter the control's while they exist - do your code in
the dialog's WM_INITDIALOG handler.

Dave
Victor Bazarov
2010-05-14 15:09:28 UTC
Permalink
Post by SM
Hi,
I have a dialog box(About dialog) and it contains three static text
controls.
Now I need to set the value to the one static control in run time, so
I changed the ID from IDC_STATIC to IDC_Test
CSimpleDialog<IDD_TESTDLG> dlgTest;
//dlgTest.SetDlgItemTextA(IDC_Test,_T("Test"));
//GetDlgItem(IDC_SOC_Version).SetDlgItemTextA(IDC_Test,_T("Test"));
dlgTest.DoModal(m_hWnd);
GetDlgItem(IDC_SOC_Version).SetDlgItemTextA(IDC_SOC_Version,_T("Test"));
while running the application i'm getting assertion as
---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Assertion Failed!
Program: ...\AllVCLanguageSamples\C++\MFC\ole\TstCon\Release
\TstCon32.exe
File: d:\Microsoft Visual Studio 9.0\VC\atlmfc\include\atlwin.h
Line: 1320
Expression: ::IsWindow(m_hWnd)
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
---------------------------
Abort Retry Ignore
---------------------------
I tried moving the getdlgitem before domodal and it also throw error.
Tried SetDlgItemTextA , same error only.
How can I set value to static text control?
You need to add 'OnInitDialog' to your class (RTFM about it). If you
don't have a class, you need to make one (subclass your 'CSimpleDialog'
for that), and set the text in that function. That's the place that
gets executed *before* the dialog shows up on the screen and *after* all
controls have been created from the dialog template. Essentially you're
going to be "tapping" into 'DoModal' that way.

Another possibility is to generate the dialog template dynamically and
use 'DialogBoxIndirect' function, but I think this is more work than the
former approach.

V
--
I do not respond to top-posted replies, please don't ask
SM
2010-05-14 15:27:55 UTC
Permalink
Post by SM
Hi,
I have a dialog box(About dialog) and it contains three static text
controls.
Now I need to set the value to the one static control in run time, so
I changed the ID from IDC_STATIC to IDC_Test
CSimpleDialog<IDD_TESTDLG>  dlgTest;
//dlgTest.SetDlgItemTextA(IDC_Test,_T("Test"));
//GetDlgItem(IDC_SOC_Version).SetDlgItemTextA(IDC_Test,_T("Test"));
dlgTest.DoModal(m_hWnd);
GetDlgItem(IDC_SOC_Version).SetDlgItemTextA(IDC_SOC_Version,_T("Test"));
while running the application i'm getting assertion as
---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Assertion Failed!
Program: ...\AllVCLanguageSamples\C++\MFC\ole\TstCon\Release
\TstCon32.exe
File: d:\Microsoft Visual Studio 9.0\VC\atlmfc\include\atlwin.h
Line: 1320
Expression: ::IsWindow(m_hWnd)
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
---------------------------
Abort   Retry   Ignore
---------------------------
I tried moving the getdlgitem before domodal and it also throw error.
Tried SetDlgItemTextA , same error only.
How can I set value to static text control?
You need to add 'OnInitDialog' to your class (RTFM about it).  If you
don't have a class, you need to make one (subclass your 'CSimpleDialog'
for that), and set the text in that function.  That's the place that
gets executed *before* the dialog shows up on the screen and *after* all
controls have been created from the dialog template.  Essentially you're
going to be "tapping" into 'DoModal' that way.
Another possibility is to generate the dialog template dynamically and
use 'DialogBoxIndirect' function, but I think this is more work than the
former approach.
V
--
I do not respond to top-posted replies, please don't ask
Hi,
I don't have class for that dialog and only resource file.
I'm using VS2008, how to add class for that dialog, any wizards
avaliable, or simply class file i need to add?

Thanks,
Sony
Victor Bazarov
2010-05-14 15:42:26 UTC
Permalink
Post by SM
Post by Victor Bazarov
Post by SM
Hi,
I have a dialog box(About dialog) and it contains three static text
controls.
Now I need to set the value to the one static control in run time, so
I changed the ID from IDC_STATIC to IDC_Test
CSimpleDialog<IDD_TESTDLG> dlgTest;
//dlgTest.SetDlgItemTextA(IDC_Test,_T("Test"));
//GetDlgItem(IDC_SOC_Version).SetDlgItemTextA(IDC_Test,_T("Test"));
dlgTest.DoModal(m_hWnd);
GetDlgItem(IDC_SOC_Version).SetDlgItemTextA(IDC_SOC_Version,_T("Test"));
while running the application i'm getting assertion as
---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Assertion Failed!
Program: ...\AllVCLanguageSamples\C++\MFC\ole\TstCon\Release
\TstCon32.exe
File: d:\Microsoft Visual Studio 9.0\VC\atlmfc\include\atlwin.h
Line: 1320
Expression: ::IsWindow(m_hWnd)
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
---------------------------
Abort Retry Ignore
---------------------------
I tried moving the getdlgitem before domodal and it also throw error.
Tried SetDlgItemTextA , same error only.
How can I set value to static text control?
You need to add 'OnInitDialog' to your class (RTFM about it). If you
don't have a class, you need to make one (subclass your 'CSimpleDialog'
for that), and set the text in that function. That's the place that
gets executed *before* the dialog shows up on the screen and *after* all
controls have been created from the dialog template. Essentially you're
going to be "tapping" into 'DoModal' that way.
Another possibility is to generate the dialog template dynamically and
use 'DialogBoxIndirect' function, but I think this is more work than the
former approach.
V
--
I do not respond to top-posted replies, please don't ask
Hi,
I don't have class for that dialog and only resource file.
I'm using VS2008, how to add class for that dialog, any wizards
avaliable, or simply class file i need to add?
There are different ways. Get yourself a book on MFC programming, and
study, it's not something that can be fully explained in a newsgroup
posting.

In the mean time, you could try doing

/// file MyAboutDlg.h

#include <....> // not sure what you need here, RTFM

class MyAboutDlg : public CSimpleDialog<IDD_TESTDLG>
{
public:
MyAboutDlg(HWND hParent)
: CSimpleDialog<IDD_TESTDLG>(hParent) {}

protected:
BOOL OnInitDialog( ??? don't remember, RTFM );
};

// file MyAboutDlg.cpp

#include <MyAboutDlg.h>

BOOL MyAboutDlg::OnInitDialog(...
{
... // I think you need to call the base class, RTFM

// do your 'SetDlgItemText' here:
this->SetDlgItemText(IDC_Test, _T("Test"));

return ... // don't remember, RTFM
}

V
--
I do not respond to top-posted replies, please don't ask
Loading...