Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

programatically create list box

Posted on 1998-08-26
5
Medium Priority
?
304 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 400 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

722 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