Link to home
Start Free TrialLog in
Avatar of qualvis
qualvis

asked on

LIstbox Question -> adding items after Initialization

Hi,

How can I add something to a listbox, AFTER it's initialized?
First, I use this code:

BOOL CDialog1::OnInitDialog()
{
      CDialog::OnInitDialog();
      m_ListBox.AddString("jef");
      return true      ;
}

and this works fine. But I want to be able to add other items afterwards (to be precise, I would like to be able to show the user which webcams are available.)

So  I made a function:

void CCamDialog::AddListBox()
{
      m_CamList->AddString("camera");
}


But this gives an error (debg assertion failed).

any ideas?

thanks
Tim Magnus

Avatar of lakshman_ce
lakshman_ce

What is m_CamList?
If this is a pointer to list box
Plz do this in your OnInitDialog
m_CamList = (CListBox*)GetDlgItem(IDC_LIST1);
//IDC_LIST1 is the id for your list box
else if it is added through class wizard as a control member
m_CamList.AddString("camera") will work (as you do in m_ListBox.AddString)

-Lakshman
Avatar of qualvis

ASKER

Sorry for that, Guess I copied it wrong (I was trying to make it work in an other application)
It should be

BOOL CDialog1::OnInitDialog()
{
CDialog::OnInitDialog();
m_ListBox.AddString("jef");
return true ;
}

and


void CDialog1::AddListBox()
{
m_ListBox.AddString("camera");
}
If you added m_ListBox member through the class wizard as control variable the code in OnInitDialog() should work fine.

If you are calling AddListBox() after dialog initialization that also should work fine.
I hope you are not calling this method in  OnCreate method or Constructor.

-Lakshman
Avatar of AndyAinscow
How is the AddListBox called?  After your dialog is visible?
There is nothing wrong with your code as such, it should work.

However if you are trying something like
CDialog1 dlg(this);
dlg.AddListBox();
dlg.DoModal();
then it will fail as your listbox doesn't exist at the time you call the AddListBox.

In an instance like this you would need to add the strings to an array in CDialog1 and the list box is filled with the contents of the array in the OnInitDialog fn.
Avatar of qualvis

ASKER

Addlistbox was indeed being called like this:

void CDialogTestApp::OnTest1()
{
      CDialog1 d1;      
      d1.AddString();
      d1.DoModal();      
}

(with ontest1 being an eventhandler when i press a menuitem)

But how should I do it then? Should I make an extra constructor which receives an array, and passes it on OnInitDialog?

thanks for your help btw! It's very much appreciated...
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of qualvis

ASKER

They should put an edit button on this forum

it's called like this obviously:

void CDialogTestApp::OnTest1()
{
     CDialog1 d1;    
     d1.AddListBox();   // AddLIstbox instead of Addstring of course
     d1.DoModal();    
}
My previous comment still applies.
Avatar of qualvis

ASKER

Yes, that should do the trick I think. (why couldn't I come up with that)

Thank you very very much!

When I become a very talented and Professional programmer, I hope I can help you out once too ;-)

Tim Magnus
Avatar of qualvis

ASKER

Brilliant, it works!

Thank you!
To close a question you need to either accept a comment as an answer or split the points between a number of experts who made comments.