Solved

LIstbox Question -> adding items after Initialization

Posted on 2004-04-07
11
383 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

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 44

Accepted Solution

by:
AndyAinscow earned 400 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 44

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 44

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeatEnd java challenge 42 85
Define unique primary key 9 99
ffmpeg - "rtsp://...... Operation not permitted" 4 92
wait notify demo infinite loop 3 149
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 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