Solved

Dynamic CStatics in a dialog

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now