Click here to Skip to main content
15,889,865 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi instead of handling BN_CLICKED, i used ON_COMMAND for handling a button in a dialog box for the handler to be called not only by clicking on the button but also by hitting enter key or space key when the button is focused.

1) am i right? in another words, isn't handler called if i handle BN_CLICKED instead of WM_COMMAND when i hit enter or space?

then i added ON_UPDATE_COMMAND_UI for the button to get disabled in a specific condition. but even when the condition is false the handler is called.

2) why?
Posted
Comments
Maximilien 14-Mar-12 12:29pm    
If the button has the focus, it should automatically respond to the enter and space keys.
ilostmyid2 14-Mar-12 12:33pm    
yes, i've changed them back to ON_BN_CLICKED and they also work for enter and space keys. now consider i'm going to enable and disable the button in case the user selects an item in the list or deselects it. when must this be handled?

1 solution

I would use BN_CLICKED to have the standard behaviour. Using other methods may confuse the users.

The Update UI mechanism does only work with frame window derived classes because the code that calls the UI handlers is not found in other classes like CDialog.

To disable a button in a dialag, use EnableWindow() of the button window. But you must not disable a button that has the focus. If you want to disable such a button, you must move the focus to another control using GotoDlgCtrl().
 
Share this answer
 
Comments
ilostmyid2 14-Mar-12 12:18pm    
thank u. that was a very good guidance. but if i could use the UI mechanism, the button would get enabled and disabled automatically. where do u think to be the best place for putting the code? the button is disabled if no item in a list ctrl is selected and enabled otherwise.
Jochen Arndt 14-Mar-12 13:15pm    
Since the UI commands are never send, they can't be used. Add a LVN_ITEMCHANGED notification handler for the list control to your dialog:
void CMyDlg::OnItemchangedList(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
if ((pNMListView->uChanged & LVIF_STATE) &&
(pNMListView->uOldState & LVNI_SELECTED) !=
(pNMListView->uNewState & LVNI_SELECTED))
{
BOOL bEnable = m_list.GetSelectedCount() ? 1 : 0;
if (!bEnable && GetFocus() == m_button)
GotoDlgCtrl(GetDlgItem(ID_OTHER_BUTTON));
m_button.EnableWindow(bEnable);
}
}
ilostmyid2 14-Mar-12 22:55pm    
thx :)
it's apparently not possible that an item in the list ctrl gets selected or deselected while it's not focused. so is checking the button not to be focused while this notification is received necessary?
Jochen Arndt 15-Mar-12 3:59am    
Yes, you are right. As far as I remember, the handler is not called when selecting items by code. If you do so, you must enable/disable the button accordingly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900