Solved

LIstbox Question -> adding items after Initialization

Posted on 2004-04-07
11
379 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Generic progress indicator 6 117
deburging in oracle form 12 105
dog bark java program 15 112
Way to decrease size of apk file 9 88
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

807 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