Solved

programatically create list box

Posted on 1998-08-26
5
291 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
Comment Utility
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
Comment Utility
The destructor of the object will call DestroyWindow which destroys the list box just created.
0
 

Author Comment

by:tim_men
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
iSeries FTP Exit Program 8 113
no14 challenge 14 56
scoreUp challenge 14 47
changeXy challenge 13 56
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

15 Experts available now in Live!

Get 1:1 Help Now