Link to home
Start Free TrialLog in
Avatar of tdiamond
tdiamond

asked on

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 !!!");
      UpdateData(FALSE);
}


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.....");
      pLB->UpdateData(FALSE);
}

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.  

Thanks...

{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
);

Parameters

hDlg
Identifies the dialog box that contains the control.

nIDDlgItem
Specifies the identifier of the control to be retrieved.

{End of C++ help description of GetDlgItem()}
Avatar of perrizo
perrizo

Hi,

  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
Avatar of tdiamond

ASKER

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).
ASKER CERTIFIED SOLUTION
Avatar of Tommy Hui
Tommy Hui

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial