Link to home
Start Free TrialLog in
Avatar of krispin
krispin

asked on

CComboBox and OnChar

I have defined a class which inherits from CComboBox and have declared the message map for the OnChar method so I can do some processing each time a character is pressed.

The problem is that message doesn't seem to be picked up as the method is never called.
Has anyone any ideas why?

class CMyComboBox : public CComboBox
{
public:
	DECLARE_DYNAMIC(CMyComboBox)
	CMyComboBox(TItem* item = NULL);
	virtual ~CMyComboBox();
	
	afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);	
protected:
        TItem *m_Item;
	DECLARE_MESSAGE_MAP()
 
};
 
 
IMPLEMENT_DYNAMIC(CSRAutoCompletionComboBox,CComboBox)
 
CSRAutoCompletionComboBox::CSRAutoCompletionComboBox(TItem* item)
: CComboBox()
{
	m_Item = item;
}
 
/*virtual*/ CSRAutoCompletionComboBox::~CSRAutoCompletionComboBox()
{
}
 
BEGIN_MESSAGE_MAP(CSRAutoCompletionComboBox, CSRComboBox)
	ON_WM_CHAR()
END_MESSAGE_MAP()
 
void CSRAutoCompletionComboBox::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	if(!fAutoComplete)
		CSRComboBox::OnChar(nChar, nRepCnt, nFlags);
}

Open in new window

Avatar of Zoppo
Zoppo
Flag of Germany image

Hi krispin,

probably there's no WM_CHAR message produced from default message processing for a ComboBox for WM_KEYDOWN/WM_KEYUP - you could add a message handler for WM_KEYDOWN instead ...

Hope that helps,

ZOPPO
The dialog will catch all key messages. Your overloaded class needs a handler for the WM_GETDLGCODE message, where the dialog asks whether the control wants to handle messages itself:

BEGIN_MESSAGE_MAP(CMyComboBox, CComboBox)

    ON_WM_GETDLGCODE( )

END_MESSAGE_MAP()

UINT CMyComboBox::OnGetDlgCode( )
{  
    return DLGC_WANTALLKEYS;
}

In the class header you need to specify the following member function:

    afx_msg UINT OnGetDlgCode( );

Regards, Alex


Avatar of krispin
krispin

ASKER

Thanks for you responses.
Unfortunately neither of these approaches worked.
Like with OnChar(), when a key is pressed, the OnGetDlgCode() method does not get called.

I'm wondering if the message is getting lost somewhere along the way. Let me explain more about what I am trying to do.
I have my combo box as explained, but this is embedded within a List Control (using Report View) which has rows and columns. I have created a class which inherits from List Control so that I can create editable columns (as MFC only provides the ability to edit the first column with the "Edit Lables" property).

So, when a line in the List Control is double clicked, I have a method which checks which column this is, and checks what type of control it should be (at the moment, only Combo Boxes). So, a Combo Box is inserted in the appropriate position.

It is my combo box class that is instantiated and put into position. The idea then that each time I press a key, this combo box will do some sort of processing (for example, if I press tab, the List Control should be notified to remove this combo box from the list). But, as I have stated, the message does not seem to reach the combo box. Is it possible that the message is being sent to the list control instead of the active combo box, or somewhere else for that matter (The List Control is sitting in an MDI child window)?

On a somewhat interesting sidenote;
I tried implementing the PreTranslateMessage method in my combobox class as in my code snippet. When I did this, when I pressed a key, I could do some processing on the key press. So obviously, somewhere after translate and dispatch the message must get sent somewhere else.

This approach will actually work fine for what I need, but I consider this to be a bit messy and would much rather use the message map approach than this. If anyone has any ideas, I'm all ears. Thanks.
BOOL CMyComboBox::PreTranslateMessage(MSG* pMsg)
{
	if(pMsg->message == WM_KEYDOWN)
	{
		//Do Something
	}
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial