title: MFC - Changing dialog item focus programmatically
body: <p>I have a Modeless dialog which shows a bunch of buttons; some of these are customized to draw stuff with GDI.<br>
  Now, when the user clicks on a customized one under certain conditions, a message box appears to alert user of the error and this is fine.<br>
  The problem is that after accepting the Message Box (showed as MB_ICON_ERROR), everywhere I click in the dialog, I always get the error message as if the whole dialog send the message to the customized button and the only way to get rid this is to press tab and give the focus to another control.<br>
  This is a strange behaviour and knowing why happens wouldn't be bad, but a simple workaround for now should do the job.<br>
  Since the moment that is probably a matter of focus, I've tried to set it on another control (in the owner dialog) by doing:<code><pre>GetDlgItem( IDC_BTN_ANOTHER_BUTTON )->SetFocus();</pre></code>
  and then, inside the customized control by adding:<code><pre>KillFocus( NULL );</pre></code>but had no results.<br>
  How should I use these functions?
  Thanks in advance.</p>
  
  <p>PS: if I comment the AfxMessageBox, the control does not show this bizarre behaviour.<br><br>
  <b>EDIT</b><br>I'll show some code as requested.</p>
  
  <pre><code>// This is where Message Box is popping out. It is effectively inside the dialog code.
  void CProfiloSuolaDlg::ProcessLBtnDownGraphProfilo(PNT_2D &amp;p2dPunto)
  {
      // m_lboxProfiles is a customized CListBox
      if(m_lboxProfiles.GetCurSel() == 0)
      {
          // This profile cannot be modified.
          /*
          CString strMessage;
  
          strMessage.Format( _T("Default Profile cannot be edited.") );
          AfxMessageBox( strMessaggio, MB_ICONERROR );
          */
  
          return;
      }
      // Selecting a node from sole perimeter.
      SelectNodo(p2dPoint);
  }
  </code></pre>
  
  <p>Actually, the message is commented to keep the dialog working.</p>
  
  <pre><code>// This is inside the customization of CButton
  void CMyGraphicButton::OnLButtonDown(UINT nFlags, CPoint point)
  {
      PNT_2D p2dPunto;
      CProfiloSuolaDlg* pDlg = (CProfiloSuolaDlg*)GetParent();     
  
      m_pVD-&gt;MapToViewport(point,p2dPunto);
      switch(m_uType)
      {
      case GRF_SEZIONE:
          pDlg-&gt;ProcessLBtnDownGraphProfilo(p2dPunto);
          break;
      case GRF_PERIMETRO:
          pDlg-&gt;ProcessLBtnDownGraphPerimetro(p2dPunto);
          break;
      }
      CButton::OnLButtonDown(nFlags, point);
  }
  </code></pre>