Solved

programatically create list box

Posted on 1998-08-26
5
296 Views
Last Modified: 2013-11-19
Please can someone help me with the following problem:

I'm just trying to create a list box programatically. My code is contained in the parent Dialog class. Here it is:


CListBox mytestbox;      

//listbox style
DWORD dwStyle = LBS_SORT | LBS_USETABSTOPS | LBS_DISABLENOSCROLL | WS_VSCROLL | WS_TABSTOP;      
//listbox dimensions
CRect myrect (9,23,216,147);
//parent window
CWnd* pParentWnd = GetParent();
//a non-used resource ID
UINT nID = 222;


BOOL didcmdwork;
//initialise value to FAILED
didcmdwork = 0;

didcmdwork = mytestbox.Create (dwStyle, myrect, pParentWnd, nID);

UpdateWindow();


The Create() function always returns a non-zero number, indicating that it has been successful, but I can't see any change on the screen ! Where am I going wrong ?

Looking forward to your comments,

Tim_men
0
Comment
Question by:tim_men
[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
  • 2
5 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 100 total points
ID: 1321288
Define CListBox mytestbox as a member variable of your dialog class or use new and delete to dynamically allocate it. The object mytestbox allocated on the stack will be destroyed as soon as the execution goes out of the scope.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1321289
The destructor of the object will call DestroyWindow which destroys the list box just created.
0
 

Author Comment

by:tim_men
ID: 1321290
Hi chensu,
thanks for your answer. I've made the CListBox mytestbox a member of the dialog's class, instead of a local variable, and at least now I can see something happening - the listbox is created just before the MyDialog dialog box opens. However, as you say, the box is destroyed as soon as the dialog box is drawn.

I have the mytestbox.Create( ) code in the MyDialog::OnInitDialog function.
Is there a way of solving the problem without resorting to techniques involving memory allocation  ? Sorry for my stupidity...

Thanks

Tim_men




0
 
LVL 23

Expert Comment

by:chensu
ID: 1321291
In the MyDialog::OnInitDialog function, it should be
mytestbox.Create (dwStyle, myrect, this, nID);
instead of
mytestbox.Create (dwStyle, myrect, pParentWnd, nID);
because the parent window of the list box is the dialog box. pParentWnd is the parent window of the dialog box.

By the way, since you are using a dialog box, why don't you just create the list box using the dialog template? That's easier.
0
 

Author Comment

by:tim_men
ID: 1321292
It's working now chensu - thanks a lot !

The reason that I didn't want to use a template is that I liked the idea of learning how to add such a control myself - thanks for explaining.

Tim_men
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Login Script to Copy Folders 12 51
Folder Comparison 12 56
Importing Special Characters in Dynamics GP Through Integration Manager 3 97
fix34  challenge 9 181
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…
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…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Suggested Courses

738 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