CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 1999
    Posts
    15

    [MFC] A Problem About Data Access Between Two Class

    I create a dialog with a ComboBox(CComboBox m_replace). The dialog class name is CAskDlg.
    In a member funcion of CView, I add these code:
    CAskDlg askDlg;
    askDlg.m_replace.AddString("Hello, world.");
    askDlg.DoModal();
    System show error message (abnormal terminal) while program run to AddString(). But if I put m_replace.AddString() in OnInitDialog(), it works well.
    Why that error happens? Thanks!


  2. #2
    Join Date
    Apr 1999
    Location
    Frankfurt, Germany
    Posts
    113

    Re: [MFC] A Problem About Data Access Between Two Class

    Simply calling CAskDlg askDlg is not enough to get a dialog You can work with. At this time You have a dialog object that is not connected to a window yet. See the CDialog page on the VC help.
    In this case the connection is done by DoModal. OnInitDialog is called while DoModal is working so the error doesn't happen.
    HTH
    Rudolf


  3. #3
    Join Date
    May 1999
    Location
    WA
    Posts
    65

    Re: [MFC] A Problem About Data Access Between Two Class

    The best way to solve this problem is to have a public accessor function that will actually add whatever data you want to the combo box. Here's an example that takes a CStringArray as a parameter, and adds all the strings to the combo box.

    void CAddDlg::AddAllComboBoxEntries(CStringArray& pArray)
    {
    for( int nIndex = 0; nIndex < pArray.GetCount(); nIndex++ )
    {
    m_cbxMyCombo.AddString(pArray.GetAt(nIndex));
    }
    }


    Then just fill out a CStringArray like follows:

    CStringArray CSA;
    CSA.Add( _T("This is a test") );
    CSA.Add( _T("This is another test") );

    And call your accessor function to add all the entries to the combo:

    CAddDlg dlg;
    dlg.AddAllComboBoxEntries(CSA);

    That should do it! Let me know if you have any questions.



    - Troy

  4. #4
    Join Date
    Apr 1999
    Posts
    383

    Re: [MFC] A Problem About Data Access Between Two Class

    This won't work (did you try it?) - you can't add items to the combobox until its window has been created!

    The dialog constructor doesn't create any control windows, that is done by DoModal().

    To use a CStringArray initialised outside the dialog, it should be passed to the dialog constructor and stored (as a reference or pointer) for use in OnInitDialog.

    Dave


  5. #5
    Join Date
    May 1999
    Location
    WA
    Posts
    65

    Re: [MFC] A Problem About Data Access Between Two Class

    Yep, I just created a project and tried that approach. It didn't work as you suspected. I did something very similar where I filled data, but I just filled a CStringArray that way, and then used that CStringArray in the OnInitDialog() function once inside the newly spawned dialog. So, my mistake..



    - Troy

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