Solved

LIstbox Question -> adding items after Initialization

Posted on 2004-04-07
11
351 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 44

Accepted Solution

by:
AndyAinscow earned 400 total points
Comment Utility
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
Comment Utility
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 44

Expert Comment

by:AndyAinscow
Comment Utility
My previous comment still applies.
0
 

Author Comment

by:qualvis
Comment Utility
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
Comment Utility
Brilliant, it works!

Thank you!
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now