Avatar of sydneyguy
sydneyguy
Flag for Australia asked on

c++ aadding data to a list box vs2005 mfc project

have the below code am using vs2005 mfc project, can add to edit box and set radio buttons but cannot see how to add the data to the listbox have been searching the web but they just give solutions that just do not exist in the intellisence text any ideas of how to do this on initialization

         CSdialog  dlg;
      dlg.Rbtwo = TRUE;
      dlg.CSedit = "hello";
      CString msg;
      msg = dlg.CSedit;

      dlg.LBlistbox = "ccccccccccccc";

// *********************************
      //  ???????  dlg.LBlistbox.

      dlg.DoModal();
C++* MFCMicrosoft Visual Studio

Avatar of undefined
Last Comment
Zoppo

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Zoppo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
sydneyguy

ASKER
thanks for that i throws up a
c:\c++ aab using visual c++\add list box 2\add list box 2.cpp(68): error C2039: 'AddString' : is not a member of 'ATL::CStringT<BaseType,StringTraits>'

guess i am missing a header file but cannot find what it is any ideas
Zoppo

Hm - maybe I did something wrong ...

I assumed LBlistbox is a member of CSdialog and declared as a CListBox, but from the error you posted it seems it is declared as a CString - in this case my code can't work as it is, you either should change it in a way LBlistbox is declared as a CListBox and bound to the list box via DDX_Control, the other possibility is to retrieve a pointer to the control using GetDlgItem wherever you need it, i.e.:
  ...
  CListBox* pListBox = (CListBox*)GetDlgItem( IDC_LISTBOX );
  pListBox->AddString(...);
...

Open in new window


I myself prefer the first, because it's easy to do using ClassWizard and there's no need for ugly (because not type-safe) casting from CWnd* to CListBox*.

ZOPPO
sydneyguy

ASKER
as you suggested set the dialog box to CListBox  the below loaded all ok but the data does not display
how do i refresh the screen please

     dlg.m_listboxcontrol.AddString( "Item" );
     dlg.m_listboxcontrol.AddString("Biology");
    dlg.m_listboxcontrol.AddString("Accounting");
    dlg.m_listboxcontrol.AddString("Art Education");
    dlg.m_listboxcontrol.AddString("Finance");
    dlg.m_listboxcontrol.AddString("Computer Science");
      int idx = dlg.m_listboxcontrol.AddString( "Selected Item" );
    dlg.m_listboxcontrol.SetCurSel( idx );
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Zoppo

Hm ... where do you call this? Doesn't seem to be inside the dialog's OnInitDialog since you use 'dlg.'

As told: It is not possible to fill the list box control directly before DoModal is called.
sydneyguy

ASKER
found the onintdialog section and placed the code in there and it now loads and displays so thanks for that

BOOL Caddlistbox2Dlg::OnInitDialog()
{
      CDialog::OnInitDialog();
      m_listboxcontrol.AddString( "Item" );
      m_listboxcontrol.AddString("Biology");
    m_listboxcontrol.AddString("Accounting");
    m_listboxcontrol.AddString("Art Education");
    m_listboxcontrol.AddString("Finance");
    m_listboxcontrol.AddString("Computer Science");
      int idx = m_listboxcontrol.AddString( "Selected Item" );
    m_listboxcontrol.SetCurSel( idx );
Zoppo

You're welcome, I'm glad I could help,

have a nice day,

best regards,

ZOPPO
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.