[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 346
  • Last Modified:

Having trouble inserting into ListBox...

I am trying to add/insert an item into a list box that I have on a Dialog Box (pretty simple right).  The problem
is, I am putting the code to add the item in a function that I created, instead of calling it from within one of the
object methods for that Dialog Box.  It works fine when I do this for instance:

void CV_TrieDlg::OnOk2()
      CListBox* pLB = (CListBox*) GetDlgItem(IDC_LIST1);
      pLB->AddString("This is a test !!!");

But, it dies when I try to do this:

void PrintDictionary(TrieNode* ptrIndex)       //Regular function that I created
                 char c;                               //index into pointer array
      char cWord[100];                         //the current word
      int iLen=0;                         //the end of the current word

      CListBox* pLB = (CListBox*) GetDlgItem(NULL, IDC_LIST1);
      pLB->AddString("This is a test.....");

In the second example, the compiler tells me that GetDlgItem doesn't take 1 parameter.  I found the
following description (below) in the help and tried it but I don't know what to specify for the first
parameter (hDlg  // handle of dialog box).  I tried NULL as the first parameter and it dumped core
and died.  I was wondering if somebody could tell me if the NULL parameter is messing things up
(I think it is) and how I could determine that parameter in order to make it work correctly.  


{Start of C++ help description of GetDlgItem()}

The GetDlgItem function retrieves the handle of a control
in the specified dialog box.

HWND GetDlgItem(
HWND hDlg,      // handle of dialog box
int nIDDlgItem    // identifier of control


Identifies the dialog box that contains the control.

Specifies the identifier of the control to be retrieved.

{End of C++ help description of GetDlgItem()}
1 Solution

  I think your problem lies in the order you are trying to fill the dialog box.  You cannot add a string into a dialog box until the dialog has created the list box.  The earliest you can add a string to the list box is during the OnInitDialog event handler. If you try to do this prior to the list boxes creation you should be getting an assertion error.  There are many ways to get around this problem.  A simple way to do this adding CStrings would be to have your function pass the a member variable in the dialog that is a CString.  Once the dialog is declared this variable is available to you.  But, once again the list box is not available to add to although the compiler will not catch it.  Try adding the CString you set in the dialog to the list box in the OnInitDialog function to start.  If this does not work then try another event later in the initialization process.

Let me know how it goes,
Nathan Perrizo
tdiamondAuthor Commented:
The code I described above is executed after the Dialog and also the Listbox have been created and are visible on the screen.  The code is in response to a clicking a Button which is on my Dialog (along with the ListBox). When that button is clicked, a custom function, which I wrote, is called.  From there, it jumps to the implementation of my function which is where I want it to make a call to Add a string to my ListBox.  I just don't know how to call GetDlgItem with the correct parameters (at least that is what I think is the problem).
Tommy HuiEngineerCommented:
Your function,

  PrintDictionary(TrieNode* ptrIndex)

should really be

  PrintDictionary(TrieNode* ptrIndex, CDialog* dialog)

You need to pass the pointer to your dialog into your function, otherwise, the function doesn't know which dialog to modify. Then the rest of your code should be

void PrintDictionary(TrieNode* ptrIndex, CDialog* dialog) //Regular function that I created
char c;                    //index into pointer array
char cWord[100];                    //the current word
int iLen=0;                    //the end of the current word

CListBox* pLB = (CListBox*) dialog->GetDlgItem(NULL, IDC_LIST1);
pLB->AddString("This is a test.....");


Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now