CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2002
    Location
    WA
    Posts
    223

    Question NEWBIE: Filling CListBox with string on class's constructor?

    I have this dialog box, and it has a CListBox in it.. And it want to make it so that when the constructor is called, the box get filled with some strings.. but it gives me an assertion error whenever I try to use AddString method.. Here's what the constructor looks like::

    PHP Code:
    CTemplatesDialog::CTemplatesDialog(CWndpParent /*=NULL*/)
        : 
    CDialog(CTemplatesDialog::IDDpParent)
    {
        
    //{{AFX_DATA_INIT(CTemplatesDialog)
            // NOTE: the ClassWizard will add member initialization here
        //}}AFX_DATA_INIT

    // List all the loaded templates
        
    AfxMessageBox("hi"); //WORKS FINE
        
    m_list.AddString("hi"); //ASSERTION FAILED    


    what would u suggest? And why is it happening?
    Last edited by Silver Ghost; October 14th, 2002 at 12:48 PM.

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757
    Move the code to OnInitDialog().

    Kuphryn

  3. #3
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715
    Can't do it in the constructor this is because the window's object isn't actually ready to recieve input at that stage. All the hinky stuff Microsoft has to do like setting up the windows message queue hasn't completed yet..

    Alternatives would be to edit the Dialog resource file and you can actually add the strings right in the resource editor, dialog editor or whatever they're calling it now...

    Another Idea would be to add the strings programatically in another function in the object and then just call that function in the dialog's OnInit windows message...

    Hope this helps..

  4. #4
    Join Date
    Oct 2001
    Location
    lake of fire and brimstone
    Posts
    1,628
    You can only call AddString when the listbox is created already. I would therefore suggest calling AddString in your dialog's OnInitDialog function.
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞

  5. #5
    Join Date
    Jan 2002
    Location
    WA
    Posts
    223
    Ehh.. how should I put it? AppWizard didnt generate any InitInstance functions... so should I create a virtual function OnInitInstance? Anything I should know about?

  6. #6
    Join Date
    Sep 2002
    Posts
    1,747

    use classwizard

    Go to the class wizard and add a handler for WM_INITDIALOG for your dialog's class, or you can insert it by hand into the message handling list and add the function by hand. But you want to do this in the dialog, not the app's intializing of its instance.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449
    In "AppWizard", there is a WM_INITDIALOG handler that you select to add the OnInitDialog function stub in your code.

    Anyway, why rely on a "Wizard"? The function OnInitDialog is documented in all the help files. Just look at the prototype and add it to your .H and .CPP files for your dialog. There will be a time when AppWizard will not have the function you're looking for and you will have to sit down and code the old fashioned way -- editing and compiling

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured