Solved

programatically create list box

Posted on 1998-08-26
5
294 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
  • 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hibernate methods 2 71
centeredAverage challenge 8 165
child constructor and parent constructor, overriding and overloading 6 84
unix example issues 18 90
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…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 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