Creation of MFC controls at run time: How to allocate resource ID's?

Posted on 2003-03-18
Medium Priority
Last Modified: 2013-11-20
I may be missing something, I don't pretend to be an expert,
but I have to create a dialog box with controls that can only
be created at run time.
I have to give these controls a resource ID but the number I
require varies. It seems that I could just create a block
of them via 'view' 'ID's' in the PWB but that seems to me to
be a 'hack'. If I just start at resource 1 and increment
will they get confused with other ID's already created in

Question by:mellor
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
LVL 48

Expert Comment

ID: 8159860
Visual Studio resource editor starts resource IDs from 100. You can be quiet starting from 1.
You can also add your own resource ID using function View - Resource Symbols. To be 100% correct, add some number of consequent IDs:


and use first of them as start value:

for ( int i = 0; i < ..., i++ )
   ... Create(ID_MYCONTROL1 + i, ...)

LVL 11

Expert Comment

ID: 8159964
You can also allocate a block of control IDs by adding one new control ID, then closing the resource file and opening the resource.h file then changing the value after _APS_NEXT_CONTROL_VALUE.  Close resource.h, reopen the resource editor, and add the "last" control for marking the end of the block you just reserved.
LVL 12

Expert Comment

ID: 8160382

 I use 0xE000 to 0xF000
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!


Expert Comment

ID: 8164710
As long as all the IDs are unique within the dialog there is no problem.

Author Comment

ID: 8165181
Dear aphillips,
Could you expand on your comment.
How does one check that one is only
working with items 'within the dialog'

Accepted Solution

aphillips earned 280 total points
ID: 8165820
> Could you expand on your comment.

When you do anything (ie call an API function or an MFC class method) that uses a control ID you also specify a window, in this case a dialog.  At the Windows API level the HWND of the dialog and the ID of the control uniquely identify the control.

I guess your confusion arises from the fact that Developer Studio puts a lot of effort into keeping track of IDs in resource.h.  However, this is due to the use of #defined values which for C/C++ are in the global "namespace" and hence its is trying to avoid conflicts in the names.

I hope this makes sense.

One reason you may want unique IDs across all dialogs is if you use Developer Studio to generate help IDs from the control IDs.  In this case the help IDs must be unique within the help file.  But I doubt whether you are using the context help facility for controls since it sounds like you generate the controls dynamically and are not sure of the number or type.

Author Comment

ID: 8165979
Thanks very much.
You learn somthing new every day.

Featured Post

Independent Software Vendors: 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

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…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

801 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