Solved

Dynamic CStatics in a dialog

Posted on 1998-05-20
4
526 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 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeateFront java challenge 31 92
Strange date stored 22 120
VBA "SendKeys" Syntax for Multiple Keystrokes 7 108
How to convert MFC::CString to UTF8 wchar_t* 10 204
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 …
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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