Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

LIstbox Question -> adding items after Initialization

Posted on 2004-04-07
11
Medium Priority
?
392 Views
Last Modified: 2013-11-20
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

0
Comment
Question by:qualvis
  • 5
  • 4
  • 2
11 Comments
 
LVL 15

Expert Comment

by:lakshman_ce
ID: 10776040
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
0
 

Author Comment

by:qualvis
ID: 10779041
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");
}
0
 
LVL 15

Expert Comment

by:lakshman_ce
ID: 10779528
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 45

Expert Comment

by:AndyAinscow
ID: 10781174
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.
0
 

Author Comment

by:qualvis
ID: 10781599
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...
0
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 1600 total points
ID: 10781623
Where do the choices come from?  In CDialogTestApp or CDialog1?
If they come from CDialog1 then do everything internally there.

A general method would be

in CDialog1  have a public member variable of type CStringArray m_arszListBoxContents;

void CDialogTestApp::OnTest1()
{
     CDialog1 d1;    
     d1.m_arszListBoxContents.Add("First");
     d1.m_arszListBoxContents.Add("Second");
     d1.DoModal();    
}

then in the OnInitDialog of CDialog1 loop throught the contents of the array and add them as you did before to the listbox
0
 

Author Comment

by:qualvis
ID: 10781680
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();    
}
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 10781695
My previous comment still applies.
0
 

Author Comment

by:qualvis
ID: 10781720
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
0
 

Author Comment

by:qualvis
ID: 10781788
Brilliant, it works!

Thank you!
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 10781897
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.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month8 days, 8 hours left to enroll

877 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