Solved

Having trouble inserting into ListBox...

Posted on 1998-03-08
3
308 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
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
notReplace  challenge 53 102
has77  challenge 9 68
canBalance challenge 34 65
Fibonacci challenge 11 84
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now