?
Solved

Dynamic CStatics in a dialog

Posted on 1998-05-20
4
Medium Priority
?
532 Views
Last Modified: 2013-11-20
I'm porting an app from another system and have a function that adds strings to a stringlist then builds a dialog and displays it to the user (the original app is text based so it just does a setxy() then printf() to do the output). I figured I could do this in a dialog for the MFC version. So I created a dialog with a function to add a string to a stringlist. When it comes to displaying the dialog, I loop through the number of strings in the stringlist and display them (and resize the dialog based on the number of strings I have, 1 string per line in the dialog). Since I don't know up front how many lines of text I have, I assume I should create a CStatic for each string dynamically just before the dialog is created. I've tried doing this in InitDialog but it doesn't show up when the dialog is displayed. Any code samples would be appreciated. Thanks.
0
Comment
Question by:bsimser
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
Norbert earned 400 total points
ID: 1313327
Hi, I have just created a dialog based app
and this worked fine
BOOL CStaticsDlg::OnInitDialog()
{
      CDialog::OnInitDialog();
      CRect r;
      GetClientRect(&r);
      
      r.bottom=20;
      CStatic      *Text=new CStatic;
      Text->Create( "SomeText",WS_CHILD|WS_VISIBLE,r, this,0x4711/*Unique ID for later reference*/);
      r.top+=20;
      r.bottom+=20;
      Text=new CStatic;
      Text->Create( "Some other Text",WS_CHILD|WS_VISIBLE,r, this,0x4712/*Unique ID for later reference*/);
 

      // Add "About..." menu item to system menu.

      // IDM_ABOUTBOX must be in the system command range.
      ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
      ASSERT(IDM_ABOUTBOX < 0xF000);

      CMenu* pSysMenu = GetSystemMenu(FALSE);
      CString strAboutMenu;
      strAboutMenu.LoadString(IDS_ABOUTBOX);
      if (!strAboutMenu.IsEmpty())
      {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
      }

      // Set the icon for this dialog.  The framework does this automatically
      //  when the application's main window is not a dialog
      SetIcon(m_hIcon, TRUE);                  // Set big icon
      SetIcon(m_hIcon, FALSE);            // Set small icon
      
      // TODO: Add extra initialization here
      
      return TRUE;  // return TRUE  unless you set the focus to a control
}
 See followin comment
0
 
LVL 3

Expert Comment

by:Norbert
ID: 1313328
Hi again
as I said I created a Dialog base App from the scratch and in
the OnInitDialog I created dynamicly 2 CStatics with different
strings
The same should should work for you.
In the sample above I don't care about memory leaks
I simple create the CStatics without storing any reference them
you should hold the references to them to delete if your app
terminates
because you know the number of strings you can also
create the CStatics as
CStatic * MyTextStatics=new CStatic[NumberOfStrings];
and create them with
for(int i=0;i<NumberOfStrings;i++)
{
     MyTextStatics[i].Create(String[i],WS_CHILD,WS_VISIBLE,PositionRect,this,BaseId+i)
    CalcNextPosition;
}

hope that helps
    Norbert
0
 
LVL 1

Author Comment

by:bsimser
ID: 1313329
Thanks! I used your second idea of creating an array of CStatics and calling create for each one. Works perfectly.
0
 
LVL 1

Author Comment

by:bsimser
ID: 1313330
Thanks! I used your second idea of creating an array of CStatics and calling create for each one. Works perfectly.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question