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
Solved

Having trouble inserting into ListBox...

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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
ODBC Connection Logging, ADO.NET 6 69
WinWaitActive parameters 12 31
wordappend challenge 8 200
Trying to run powershell  in a batch file. How do I do this? 8 105
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

840 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