MFC problem, nobody encounter this before!?

OK, i found this in while using mfc dialog box.

Create a combo box using dialog wizard and fill the combo box with some items, such that when the user drop down the combo box, a scroll bar appear.

Override the following method :

void CDialogTestDlg::OnSelchangeCombo1()
{
// TODO: Add your control notification handler code here

AfxMessageBox("Hello");
}

Run the program, drop down the combo, and right click on the scroll bar. The program hang...!!?? :confused: :confused:
[537 byte] By [mce] at [2007-11-20 9:54:46]
# 1 Re: MFC problem, nobody encounter this before!?
Anybody care to comment??

Any program developed in MFC with scroll combo box will have this issue if you try to pop up a message in CBN_SELCHANGE :mad: :mad:
mce at 2007-11-11 4:05:09 >
# 2 Re: MFC problem, nobody encounter this before!?
I need to pop up a messagebox in OnSelchangeCombo1 but the following code will cause MFC to hang! if your combo box is having a scrollbar and you right click on the scrollbar. :mad: :mad: :mad:

void CDialogTestDlg::OnSelchangeCombo1()
{
// TODO: Add your control notification handler code here

AfxMessageBox("Hello");
}



How to get around this stupid problem??
mce at 2007-11-11 4:06:05 >
# 3 Re: MFC problem, nobody encounter this before!?
AfxMessageBox shows a modal dialog box. How can you scroll through the combo in the dialog?
cilu at 2007-11-11 4:07:13 >
# 4 Re: MFC problem, nobody encounter this before!?
[ merged ]
cilu at 2007-11-11 4:08:08 >
# 5 Re: MFC problem, nobody encounter this before!?
AfxMessageBox shows a modal dialog box. How can you scroll through the combo in the dialog?

No, I don't intend to scroll through. I meant when you right click on the scroll bar. Of course under normal circumstances i won't right click on the scroll bar. But you see my problem here, i am poping up a modal dialog box to inform the user about a particular choice when the user select an item in the drop down combo. However, if the user just made a right click on the scroll bar , why would the MFC hang here? The use hasn't chosen anything yet in fact, or whatever it is, Isn;t it a problem of MFC or whatever windows messaging that doesn;t seem to take care of this and we need to do some twist?

So my question is still what can be done here if i need to pop up a modal dialog box in OnSelchangeCombo1? Or is it just forbidden under MFC rules to pop up modal dlg in OnSelchangeCombo1?
mce at 2007-11-11 4:09:09 >
# 6 Re: MFC problem, nobody encounter this before!?
:wave: :wave:

Hello!?

Any help ??

Or just tell me how to diable the right click on a scroll bar?
mce at 2007-11-11 4:10:11 >
# 7 Re: MFC problem, nobody encounter this before!?
:wave: :wave:

Hello!?

Any help ??A modal message box is just that -- modal. No messages are processed by the app until the modal dialog is closed.

Why would you popup a modal message box in the middle of combo box message processing? IMO, a combo box changing selection should not be interrupted this way. Usually, when a combo is selection is changed, the most that an app will do is update other controls, color something different, etc -- not popup a modal dialog.

And I doubt it has anything to do with MFC -- it more than likely is the way the Windows messaging works, regardless of using MFC or not.

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-11 4:11:15 >
# 8 Re: MFC problem, nobody encounter this before!?
Thanks a lot for the reply. :D

:wave:
But., It seems there are still some misundertsandings here.

As i mentioned earlier (may be i am not clear), you see., When user drop down my combo box with scroll bar there, and as he is making a selection by clicking on one specific of the item, i pop up a warning message if he really want the selection. If he is ok (meaning he click on the "OK" button on the modal dlg) the selection is made and everything went smoothly. All this is done in the OnSelchangeCombo1 function.

No issue here., everything is purrfect. I pop up message box/modal box where i want it to, give the user appropriate warning if he want to continue and return normally.

Now, the problem. if the user drop down the combo box and do a RIGHT CLICK, yes it is "RIGHT CLICK", on the scroll bar beside any item in the combo box, this is equivalent to left click of the mouse for the item. The OnSelchangeCombo1 will thus get called. But this time, the dialog box get screwed up. I noticed that the problem could be due to the program is trying to display the context menu and modal dialog box at the same time??
You probably have noticed that, when you right click on a scroll bar, a context menu is pop up by default whenever there is a scroll bar.

So if i can pop up message smoothly if i left click on the combo box, why i can't pop up message if i do a right click?? In other words, it is perfectly legal for me to do some sanity check by poping up a modal box inside the function OnSelchangeCombo1 ( where CBN_SELCHANGE message is processed) before i proceed to anything., and of course this works. Since this works, there is no reason why the right click on scroll bar should failed because of the modal dlg box.

I hope you understand my point .. :D :D
mce at 2007-11-11 4:12:14 >
# 9 Re: MFC problem, nobody encounter this before!?
Now, the problem. if the user drop down the combo box and do a RIGHT CLICK, yes it is "RIGHT CLICK", on the scroll bar beside any item in the combo box, this is equivalent to left click of the mouse for the item. The OnSelchangeCombo1 will thus get called. But this time, the dialog box get screwed up. I noticed that the problem could be due to the program is trying to display the context menu and modal dialog box at the same time??
Well, why are you trying to display the context menu and modal dialog box at the same time? :confused:
None of the comboboxes in my applications shows me the context menu when I do a "RIGHT CLICK" on the combobox scollbar!
VictorN at 2007-11-11 4:13:21 >
# 10 Re: MFC problem, nobody encounter this before!?
... When user drop down my combo box with scroll bar there, and as he is making a selection by clicking on one specific of the item, i pop up a warning message if he really want the selection. If he is ok (meaning he click on the "OK" button on the modal dlg) the selection is made and everything went smoothly. All this is done in the OnSelchangeCombo1 function.
I think that Paul McKenzie was trying to give you the generally-accepted advice that you should do very little inside the notifications from a control. These notifications are not meant to allow extensive processing, or anything that might take more than a moment or two, or otherwise interfere with the message pump of the application. So, putting a MessageBox inside the notification is not a great idea.

If you need to perform extensive processing after the user has selected a new item, then post a user-defined message to yourself (using PostMessage) so that the message shows up at the end of the message queue. Perform the extensive processing inside the handler for the user-defined message.

As a further thought, maybe your problems are caused by the WM_CONTEXTMENU handler. Post the code from that handler too.

Mike
MikeAThon at 2007-11-11 4:14:21 >
# 11 Re: MFC problem, nobody encounter this before!?
As Paul said, your message will pop up when users try to scroll through the combo box, which would cause them to want to do you bodily harm. There isn't much point in trying to solve a problem when your underlying design will have to change anyway.
GCDEF at 2007-11-11 4:15:22 >
# 12 Re: MFC problem, nobody encounter this before!?
Well, why are you trying to display the context menu and modal dialog box at the same time? :confused:
None of the comboboxes in my applications shows me the context menu when I do a "RIGHT CLICK" on the combobox scollbar!

Hi,

The context menu is not displayed by me. I don't even know how to remove this context menu which is put there by default by MFC framework whenever there is a scroll bar. If your combo scroll bar doesn't show context menu at all without you doing anything, then i am pretty surprised.. i am using VC++ 6.0.

I will create a project and attached here in a while. Thanks for all the info.. :D :D
mce at 2007-11-11 4:16:22 >
# 13 Re: MFC problem, nobody encounter this before!?
Hi All, :wave: :wave:

Here is a very simple MFC project.

Nothing complicated, no heavy processing anywhere, and a very normal way of coding you could expect by using MFC.

Just drop down the combobox and make a selection. Purrfect.

Then drop down again the combo box and try to right click on the scroll bar.. BOOM......... :D :D

As you may notice in the code, i can;'t see why i shouldn't pop up a dlg box inside the OnSelchangeCombo1? Or really there is a better way here to do what i want to achieve? Appreciate all feedback..................
mce at 2007-11-11 4:17:23 >
# 14 Re: MFC problem, nobody encounter this before!?
When posting a project, eliminate the unnecessary files such as *.aps, *.opt, *ncb, etc. The only files needed are the *.dsp, *.dsw, resource files, and source code.

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-11 4:18:25 >
# 15 Re: MFC problem, nobody encounter this before!?
Hi All, :wave: :wave:

Here is a very simple MFC project.

Nothing complicated, no heavy processing anywhere, and a very normal way of coding you could expect by using MFC.

Just drop down the combobox and make a selection. Purrfect.

Then drop down again the combo box and try to right click on the scroll bar.. BOOM......... :D :D

As you may notice in the code, i can;'t see why i shouldn't pop up a dlg box inside the OnSelchangeCombo1? Or really there is a better way here to do what i want to achieve? Appreciate all feedback..................I tried your project and it works fine for me. Of course, since I don't have VC6 installed anywhere, I converted it to VC7.1 (VC 2003). It works fine.

A couple of things:

As several others have suggested, it's not a good idea to do this kind of thing in the middle of handling a selchange message.
Looks like the behavior has changed in a later version of VC. Consider upgrading your 9 year old version.
Lastly, with regard your question of why you shouldn't popup a dialog in the middle of the OnSelChanged handler. Should you be allowed to? Maybe, VC7.1 allows you to, but is it really a good user experience in this case? I sure wouldn't want to have to confirm a combo box selection. It would be very annoying. Perhaps you could allow the user to change several settings and then prompt the user for confirmation during a save or apply operation?
Arjay at 2007-11-11 4:19:28 >
# 16 Re: MFC problem, nobody encounter this before!?
hi mce ,
i run your project but when i right click on the scroll bar nothing happens.
i mean it does not hang. but simply do not respond. as right click on scrolll bar has not been handled. i am not sure.if this could be done by some Hooking of the commmon Controls events.

put straight.
this certainly does not hang. it just does not respond.

regards
Dingo
dingo_kasper at 2007-11-11 4:20:24 >
# 17 Re: MFC problem, nobody encounter this before!?
I tried your application in VC6 and it definitely hangs, just the way you described.

If you comment out the MessageBox() function, then the application does not hang. Instead, the context menu for a scroll bar appears.

More testing shows the it's not the right click down message that causes the problem. Rather, it's the WM_RBUTTONUP (or WM_NCRBUTTONUP ??) message that causes the problem. For example, drop down the combo, and right click and hold on the scroll bar. No problem. Move the mouse and release the right button anywhere except the scroll bar and there's no problem. Conversely, drop down the combo box. Right click and hold anywhere except the scroll bar. No problem. Move the mouse and release the right button on the scroll bar and the application hangs.

This would seem to implicate the combo box's handling of the WM_RBUTTONUP or WM_NCRBUTTONUP messages. According to the docs ( http://msdn2.microsoft.com/en-us/library/ms673114.aspx ), the combo box doesn't handle this message explicitly; rather, it passes it to DefWindowProc. DefWindowProc generates the WM_CONTEXTMENU message in response to WM_RBUTTONUP, and sends the message to the window's parent.

Because the WM_CONTEXTMENU message is sent, that might be the reason why the application hangs. Maybe since the application is stuck inside the modal message pump (instead of the MFC message pump), the parent can't handle the message, and the call to SendMessage(hWndParent, WM_CONTEXTMENU, ...) never returns.

Anyway, whatever the cause, the basic advice still applies: don't display a message box from inside the handler for a notification from a control. As mentioned above, the problem disappears completely if you delete the call to MessageBox.

Mike
MikeAThon at 2007-11-11 4:21:21 >
# 18 Re: MFC problem, nobody encounter this before!?
Ok, thanks for all the replies.

At lease i can confirm that this is part of the problem in MFC or at least something related to it.

And i am very convinced that some guys who provided the framework realized this issue and fix it in VC++ 7.0.

And to all the recommendations that i should not pop up message box in the handling of the OnSelchangeCombo1, i think really, this is something that need to be fixed by microsoft rather than asking the programmer not to do so in the message handling. After all, it is already allowed through left clicking, so why right clicking is not handled properly? This sounds more like a bug in the Microsoft. :D :D

And back to my original problem, if i were to remove all the message box in the OnSelchangeCombo1, i don't think my software tester and my boss will agree with me. Why? This is what they are telling me :-

The user make a selection in the combo box, and it pass through. Why the user is only informed/warned about the particular selection much later on?? rather than the moment the selection is made? If you were me, what would you say ? i need a valid reason to fight them off :D :D unless i tell them this is a bug in the Microsoft and i can't do anything better.
mce at 2007-11-11 4:22:24 >
# 19 Re: MFC problem, nobody encounter this before!?
And to all the recommendations that i should not pop up message box in the handling of the OnSelchangeCombo1, i think really, this is something that need to be fixed by microsoft rather than asking the programmer not to do so in the message handling. After all, it is already allowed through left clicking, so why right clicking is not handled properly? This sounds more like a bug in the Microsoft. :D :D You were told to post a message, did you do that? That way, you are out of the combo box processing, and the message is posted. You then process this posted message, and display as many modal dialogs as your heart (and boss) desires. Then if the user wants to change the selection, then and only then do you take the combo box and do whatever you want to do with it to set it back to the old selection.

The problem is that you've come up with a false "solution", and you want to keep using the false "solution" until it works. What you should be doing is what others have suggested (post a message) and process things that way. The boss or tester doesn't know about "OnSelChangeCombo1", so I don't see what the issue is. Posting a message and handling the posted message would look and behaves the same, without the issue of the modal dialog interfering with the message pump.

As to the way other apps "warn users of changes", there is usually another control, text colored differently, whatever, that shows that things have changed. It doesn't have to be obtrusive and annoying as a message box popping up in the weirdest of places. Guaranteed, your "power users" will get very annoyed at this message box.

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-11 4:23:29 >
# 20 Re: MFC problem, nobody encounter this before!?
Here is a very simple MFC project.
...
Just drop down the combobox and make a selection. Purrfect.

Then drop down again the combo box and try to right click on the scroll bar.. BOOM......... :D :D Ok, I've just tested your sanple on my Win XP SP2 PC with VC++6.0 SP6.
The result is the same as with comboboxes in all my applications.
There is no context menu at all! :cool:

As you may notice in the code, i can;'t see why i shouldn't pop up a dlg box inside the OnSelchangeCombo1? Or really there is a better way here to do what i want to achieve? Appreciate all feedback..................Note, that you are asking the user about "Do you really want to change selection" after this selection has been already changed! You cannot prevent this any way except by re-selecting the previously selected item.
In this case you have to save this information after the user chooses "Yes".
VictorN at 2007-11-11 4:24:31 >
# 21 Re: MFC problem, nobody encounter this before!?
Ok, I've just tested your sanple on my Win XP SP2 PC with VC++6.0 SP6.
The result is the same as with comboboxes in all my applications.
There is no context menu at all! :cool:

That is funny. I am using XP sp2, vc++ i think is sp6 too (though I am not sure how to verify the sp version of my vc++ 6).

MikeAThon... i think he can reproduce, see his post earlier.

The fact that you don see it in your application/on your pc.., i think it is dangerous .. it is highly possible to see it on your customer's pc.. :D :D :D
mce at 2007-11-11 4:25:25 >
# 22 Re: MFC problem, nobody encounter this before!?
That is funny. I am using XP sp2, vc++ i think is sp6 too (though I am not sure how to verify the sp version of my vc++ 6).
You can see this information in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\6.0\ServicePacks key
VictorN at 2007-11-11 4:26:26 >
# 23 Re: MFC problem, nobody encounter this before!?
SP6 :D

So i am using Xp SP2 and VC6 sp6 too.
mce at 2007-11-11 4:27:27 >
# 24 Re: MFC problem, nobody encounter this before!?
My environment was VC6, SP5, WinXP Pro, and definitely exhibited the problem reported by mce.

But again, the problem is your own making:
And to all the recommendations that i should not pop up message box in the handling of the OnSelchangeCombo1, ... , it is already allowed through left clicking, so why right clicking is not handled properly? This sounds more like a bug in the Microsoft
It's not allowed through left clicking either. Don't pop up a message box in any of the notifications from a child control, or otherwise perform extensive processing, or anything that might take more than a moment or two, or otherwise interfere with the message pump of the application. So, putting a MessageBox inside the notification is not a great idea, whether a left click or a right click.

As Paul McKenzie stated, you have focused on a false solution, which doesn't work in your environment (at least), so why stick with it when better options exist. Post a message to yourself, as previously advised. The following works fine for me:
// in dialogTestDlg.h : header file

#define CM_SPECIAL_MESSAGE (WM_APP+1)

// ...

public:
LRESULT OnSpecialMessage(LPARAM lParam, WPARAM wParam);

// in dialogTestDlg.cpp : implementation file

// ...
BEGIN_MESSAGE_MAP(CDialogTestDlg, CDialog)
//{{AFX_MSG_MAP(CDialogTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_CBN_SELCHANGE(IDC_COMBO1, OnSelchangeCombo1)
ON_CBN_DROPDOWN(IDC_COMBO1, OnDropdownCombo1)
ON_MESSAGE(CM_SPECIAL_MESSAGE, OnSpecialMessage)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// ...

void CDialogTestDlg::OnSelchangeCombo1()
{
// TODO: Add your control notification handler code here
int sel = m_CtrlCombo.GetCurSel();

if(sel != -1){

// save data about old and new selection into member variables, so that
// it can be used in the OnSpecialMessage handler

// naturally, wParam and lParam do not need to be zero. You can use special values for
//wparam and lparam to encode special meanings to the message

PostMessage(CM_SPECIAL_MESSAGE, 0L, 0L );

}

}

LRESULT CDialogTestDlg::OnSpecialMessage(LPARAM lParam, WPARAM wParam)
{
// posted to myself just after the user changes the selection in the combo box

int nRet = MessageBox(_T("Do you really want to change selection?"),"Warning",MB_YESNO);

if(nRet == IDYES){

//Set some value here

}

else{


//just restore the previous selection
}
return 0L;

}
Mike
MikeAThon at 2007-11-11 4:28:27 >
# 25 Re: MFC problem, nobody encounter this before!?
Now it might be a good time to get your boss to upgrade to a newer compiler.
Arjay at 2007-11-11 4:29:28 >
# 26 Re: MFC problem, nobody encounter this before!?
SP6 for VS6 is free for download, BTW! No upgrade is needed. ;)
VictorN at 2007-11-11 4:30:37 >
# 27 Re: MFC problem, nobody encounter this before!?
And to all the recommendations that i should not pop up message box in the handling of the OnSelchangeCombo1, i think really, this is something that need to be fixed by microsoft rather than asking the programmer not to do so in the message handling. After all, it is already allowed through left clicking, so why right clicking is not handled properly? This sounds more like a bug in the Microsoft. :D :D

And back to my original problem, if i were to remove all the message box in the OnSelchangeCombo1, i don't think my software tester and my boss will agree with me. Why? This is what they are telling me :-

The user make a selection in the combo box, and it pass through. Why the user is only informed/warned about the particular selection much later on?? rather than the moment the selection is made? If you were me, what would you say ? i need a valid reason to fight them off :D :D unless i tell them this is a bug in the Microsoft and i can't do anything better.

Run your app. Open the combobox and drop down the list. Use the down arrow on your keyboard to scroll down the list to the selection you want. Your messagebox will pop up on every item in the list. That's why it's a bad idea.
GCDEF at 2007-11-11 4:31:32 >
# 28 Re: MFC problem, nobody encounter this before!?
Run your app. Open the combobox and drop down the list. Use the down arrow on your keyboard to scroll down the list to the selection you want. Your messagebox will pop up on every item in the list. That's why it's a bad idea.Good point. If the user uses the keyboard to control the combo box, they will see that the program is being totally user-unfriendly and a hindrance to getting any work done.

To mce:

Does your boss and tester ever run their programs using the keyboard instead of the mouse? If not, let them try their idea using only a keyboard -- maybe that will finally convince them to drop this silly idea of popping up message boxes.

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-11 4:32:34 >
# 29 Re: MFC problem, nobody encounter this before!?
Does your boss and tester ever run their programs using the keyboard instead of the mouse? If not, let them try their idea using only a keyboard -- maybe that will finally convince them to drop this silly idea of popping up message boxes.

Agree with your opinion.

mce: I think it's better to use tooltip instead of message box
henky@nok.co.id at 2007-11-11 4:33:40 >
# 30 Re: MFC problem, nobody encounter this before!?
Agree with your opinion.

mce: I think it's better to use tooltip instead of message box

I don't see how that helps in this instance either.
GCDEF at 2007-11-11 4:34:34 >
# 31 Re: MFC problem, nobody encounter this before!?
My environment was VC6, SP5, WinXP Pro, and definitely exhibited the problem reported by mce.

Mike

Solutions aside, could you download/upgrade to sp6 and give it a try?

I am using VC6, SP6 and WinXP Pro SP2. It exhibits the same behavior.

I am just wondering why VictorN doesn't get the same behavior :D
mce at 2007-11-11 4:35:44 >
# 32 Re: MFC problem, nobody encounter this before!?
Interesting!
I get context menu by right mouse click on the ListBox scrollbar, but not on the listbox part of the combobox!
Other version of common controls? :confused:
I am using IE 7.
What IE version are you using?
VictorN at 2007-11-11 4:36:36 >
# 33 Re: MFC problem, nobody encounter this before!?
I don't see how that helps in this instance either.
What i mean with this option is, there is a requirement to show message but
can't use message box. So, showing tooltip for that message is one of choice.
Don't forget that software developers need to hear what users want even
not all. Otherwise developed software will be garbage. :cool:
henky@nok.co.id at 2007-11-11 4:37:45 >
# 34 Re: MFC problem, nobody encounter this before!?
Interesting!
I get context menu by right mouse click on the ListBox scrollbar, but not on the listbox part of the combobox!
Other version of common controls? :confused:
I am using IE 7.
What IE version are you using?

I am using IE 6. But, i don think it is related to IE version. I have tried to run the application on Windows Vista with IE 7, and it display the same behavior.
mce at 2007-11-11 4:38:38 >
# 35 Re: MFC problem, nobody encounter this before!?
...When user drop down my combo box with scroll bar there, and as he is making a selection by clicking on one specific of the item, i pop up a warning message if he really want the selection. If he is ok (meaning he click on the "OK" button on the modal dlg) the selection is made and everything went smoothly. All this is done in the OnSelchangeCombo1 function.Any kind of validation in the combobox should be done, I think, in CBN_SELENDOK handler.
CBN_SELCHANGE doesn't mean any commitmrnt from the user to change the selection. It means - "just browsing"...
I also agree with other posters here - you should not pop up modal UI in ANY message handler other than WM_COMMAND. You can confuse your main UI if it will get, for example, WM_LBUTTONDOWN that will never be followed by WM_LBUTTONUP (because your modal box ate it.
VladimirF at 2007-11-11 4:39:42 >