Solved

Having trouble inserting into ListBox...

Posted on 1998-03-08
3
336 Views
Last Modified: 2013-11-20
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()}
0
Comment
Question by:tdiamond
[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
3 Comments
 
LVL 1

Expert Comment

by:perrizo
ID: 1316828
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
0
 

Author Comment

by:tdiamond
ID: 1316829
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).
0
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 150 total points
ID: 1316830
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.....");
pLB->UpdateData(FALSE);
}

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

635 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