Solved

programatically create list box

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Separate into files by filename 12 75
ffmpeg - "rtsp://...... Operation not permitted" 4 73
lucky13 challenge 11 120
Thin secure Windows 10 5 73
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

929 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

9 Experts available now in Live!

Get 1:1 Help Now