Microsoft KB Archive/187644

= The ATL controls does not recognize the default button on a VB form in Visual C++ =

Article ID: 187644

Article Last Modified on 6/4/2005

-

APPLIES TO

 Microsoft ActiveX Template Library 2.0, when used with:  Microsoft Visual C++ 5.0 Enterprise Edition

 Microsoft Visual C++ 6.0 Enterprise Edition

 Microsoft Visual C++ 5.0 Professional Edition

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2002 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2003 Standard Edition</li></ul> </li> Microsoft ActiveX Template Library 2.1, when used with:  Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 <li>Microsoft Visual C++ 6.0 Standard Edition</li></ul>

<ul> <li>Microsoft Visual C++ .NET 2002 Standard Edition</li></ul>

<ul> <li>Microsoft Visual C++ .NET 2003 Standard Edition</li></ul> </li> <li>Microsoft ActiveX Template Library 3.0, when used with: <ul> <li>Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

<ul> <li>Microsoft Visual C++ 5.0 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Standard Edition</li></ul>

<ul> <li>Microsoft Visual C++ .NET 2002 Standard Edition</li></ul>

<ul> <li>Microsoft Visual C++ .NET 2003 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q187644

<div class="symptoms_section">

SYMPTOMS
When an ATL-based ActiveX control has the focus on a Visual Basic form, it does not give the focus to the default button on the form (the button with a Default property of True) when you press ENTER.

Steps to Reproduce Behavior
<ol> <li>Add an ActiveX control that was written in ATL to a Visual Basic form.</li> <li>Add a Command button to the form. Set the Default property to True.</li> <li>Add an ActiveX control that was not written in ATL to the form.</li> <li> Implement the Click event for the Command button as follows: Private Sub Command1_Click MsgBox "Hello from Command1" End Sub </li> <li>Run the form. Select the ActiveX control that was not written in ATL and press ENTER. This causes the default Command button to activate and the message box appears.</li> <li>Select the ATL-based control and press ENTER. The Command button does not get activated and the message box does not appear.</li></ol>

<div class="cause_section">

CAUSE
The reason for this behavior is that the default implementation in ATL of the IOleControlImpl::GetControlInfo and IOleInPlaceActiveObjectImpl::TranslateAccelerator methods just returns E_NOTIMPL. The container calls GetControlInfo to get the control's keyboard mnemonics and keyboard behavior, and it calls TranslateAccelerator to process the keys.

<div class="resolution_section">

RESOLUTION
Because the implementation in ATL of these IOleControlImpl-based methods simply returns E_NOTIMPL, the control writer must override these methods in the control class.

<div class="status_section">

STATUS
Microsoft is researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

<div class="moreinformation_section">

MORE INFORMATION
To override this behavior implement IOleControlImpl::GetControlInfo and IOleInPlaceActiveObjectImpl::TranslateAccelerator in your control class as shown below:

Sample Code
STDMETHOD(GetControlInfo)(CONTROLINFO *pCI) {     if(!pCI) {        return E_POINTER; }     pCI->hAccel   = NULL; pCI->cAccel  = 0; pCI->dwFlags = 0;

return S_OK; }

STDMETHOD(TranslateAccelerator)(MSG *pMsg) {     if (         ((pMsg->message >= WM_KEYFIRST) && (pMsg->message <= WM_KEYLAST))         &&         ((VK_TAB == pMsg->wParam) || (VK_RETURN == pMsg->wParam))      ) {        CComQIPtr<IOleControlSite,&IID_IOleControlSite> spCtrlSite(m_spClientSite); if(spCtrlSite) {           return spCtrlSite->TranslateAccelerator (pMsg,0); }     }      return S_FALSE; } In the above sample, the TranslateAccelerator code processes only the TAB and the ENTER keys.

<div class="references_section">