Solved

Dynamic CStatics in a dialog

Posted on 1998-05-20
4
528 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
Norbert earned 100 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expand to include initial dialog with two choices. 9 86
notReplace  challenge 53 151
fizzArray2 challenge 1 103
unix example issues 18 106
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.

730 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