Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

message handling in dynamically created combobox

258 views
Skip to first unread message

Dinesh

unread,
Jun 10, 2009, 1:44:01 AM6/10/09
to
hello Alll
i m creating dynamic combobox but i want to handle
message(ON_CBN_DROPDOWN) when i click for drop down list.

Scot T Brennecke

unread,
Jun 10, 2009, 5:47:19 AM6/10/09
to

And what's stopping you from handling it? The notification will be sent
to the parent window (unless you use message reflection to handle it in
a class you derive from CCombobox).

Scott McPhillips [MVP]

unread,
Jun 10, 2009, 7:56:41 AM6/10/09
to
"Dinesh" <Din...@discussions.microsoft.com> wrote in message
news:1A921DED-ADE7-47E8...@microsoft.com...
> I can't write msg map dynamically in BEGIN_MESSAGE_MAP()
> END_MESSAGE_MAP()
> block.
> So tell me how I can capture msg ON_CBN_DROPDOWN.

You don't need to add it to the message map dynamically. #define the
control ID in resource.h and use the ID in the dynamic creation call and in
the message map ON_CBN_DROPDOWN. Add it to the message map by typing it in
before compiling.

--
Scott McPhillips [VC++ MVP]

Dinesh

unread,
Jun 10, 2009, 7:30:01 AM6/10/09
to

"Scot T Brennecke" wrote:

I can't write msg map dynamically in BEGIN_MESSAGE_MAP() END_MESSAGE_MAP()

Dinesh

unread,
Jun 10, 2009, 7:35:01 AM6/10/09
to

"Scot T Brennecke" wrote:

I can't write msg map dynamically in
BEGIN_MESSAGE_MAP()
.....
...
END_MESSAGE_MAP() block.

So tell me how I can capture msg ON_CBN_DROPDOWN. I am using CCombobox
object for creating combobox dynamically and this class had derived from
CDialog.

Ajay

unread,
Jun 10, 2009, 9:22:28 AM6/10/09
to

As already mentioned, you dont need to add it dynamically to
Begin_message_map. Define the ID at compile time and place it the
message map. At run time, simply create a combobox with this ID.

--
Ajay

Joseph M. Newcomer

unread,
Jun 10, 2009, 5:14:22 PM6/10/09
to
Why would you want to?

First, not your question. You indicate you are creating a singly dynamic combobox. To
create it, you have to have an ID. If you have an ID, you can add a handler to the
message map at compile time; the fact that there is currently no control of that ID is
irrelevant. When it is finally created, if it sends a notification, the notification will
be sent.

Of course, there is a question of why you need to create it at runtime. This is often
done because "it isn't needed all the time", in which case the solution is to create it at
design time, and merely make it invisible until it is needed.

On the other hand, if you need to create a multitude of these, you can use
ON_CONTROL_RANGE in your message map.

Do not assume for any reason that the message map can only be manipulated by the class
wizards. You can always hand-edit anything you want into them.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Dinesh

unread,
Jun 11, 2009, 5:49:02 AM6/11/09
to
When I use ON_CONTROL_RANGE macro.I have to add ID of combo box in
resource.h file before creating combo box.
I tried it and it is working for # define IDs in resource.h file.

but I don't know how many combo box will create in application. So what I
have to do now..

But one thing more, you have written that "If you have an ID, you can add a
handler to the message map at compile time." that is not clear.

How I can add a handler to the message map at compile time.
please tell me in detail.

thanks in advance

David Wilkinson

unread,
Jun 11, 2009, 6:55:07 AM6/11/09
to
Dinesh wrote:
> When I use ON_CONTROL_RANGE macro.I have to add ID of combo box in
> resource.h file before creating combo box.
> I tried it and it is working for # define IDs in resource.h file.
>
> but I don't know how many combo box will create in application. So what I
> have to do now..
>
> But one thing more, you have written that "If you have an ID, you can add a
> handler to the message map at compile time." that is not clear.
>
> How I can add a handler to the message map at compile time.
> please tell me in detail.

Just look at what the wizard does for a control on a dialog template, and do the
same things. There are three parts:

declaration in .h file
implementation in .cpp file
message map entry in .cpp file

--
David Wilkinson
Visual C++ MVP

Joseph M. Newcomer

unread,
Jun 11, 2009, 12:28:26 PM6/11/09
to
Where is it engraved in stone that the only IDs you are allowed to use must be contained
in resource.h? Hint: it isn't. They are JUST NUMBERS. You can define them in any file
you want to. The traditional means of doing this is to select an arbitrary range that is
unlikely to be hit by typical resource.h manipulation, so integers in the range of
5000-32767 are usually considered candidates. You choose a range that will encompass all
of your controls (note that you have now changed the question, to one of *multiple*
controls), for example, if I do not plan on having more than 100 combo boxes I might have
a header file, or even just in my .cpp file for that dialog,

#define DYNAMIC_COMBO_MIN 10000
#define DYNAMIC_COMBO_MAX 10100

and keep track of the IDs I assign. My range for ON_CONTROL_RANGE will be from
DYNAMIC_COMBO_MIN to DYNAMIC_COMBO_MAX.

Note: never assume you have a clue as to the dimensions of your combo box. If you have
hardwired integers into your code, your code is wrong. What I do is place an invisible
combo box on the dialog template, for example, tucked into one corner. I make it the size
I want. When the program runs, and I want to create a new combo box, I will do something
like

CRect proto;
c_PrototypeCombo.GetWindowRect(&proto);
ScreenToClient(&proto);
CRect combo;
combo.left = ... where you want to put it...;
combo.top = ... where you want to put it...;
combo.right = combo.left + proto.Width();
combo.bottom = combo.top + proto.Height();
...create the combo box

This means your combo box automatically matches the correct size for the screen,
resolution, default font, display driver version, and angular position of Mars that are
appropriate for the machine on which the program is currently executing (which can be the
same machine you developed it on, an hour later, with different settings). Sometimes, you
compute the width based on GetTextExtent of some suitably representative string. You may
also compute the height on the fly to ensure that you don't get some ugly situation in
which you had space for four items, it actually shows six, and the one the user needs is
*always* off-window and must be scrolled (scrolling is ugly in such situations and should
be avoided).

They way you add a handler to the message map at compile time is with a text editor!
There's nothing deep about that. Put the caret at the point you want to do the insertion.
Type.
joe

0 new messages