0

On a MFC application a CEdit control of a dialogue is subclassed. There is a numerical keypad on another dialogue which is supposed to send values to that text box. If the text is highlighted on the edit control the GetSel method returns the start and end index of the highlighted text and this will be replaced with the value comes from the keypad. This works fine.

Now if the the subclassed CEdit become a part of a custom CComboBox control the GetSel method on the CEdit control of combobox always returns 0.

I don't seem to realize what the reason and solution is. And would appreciate any help. Thanks.

Update:

Here is the piece of code that tries to get the highlighted text

BOOL CBaseDialog::PreTranslateMessage(MSG* pMsg) 
{
    if (pMsg->message == WM_KEYDOWN && pMsg->lParam == 2)
    {
        switch (pMsg->wParam)
        {
            case VK_TAB:
                //NextDialogCtrl();
                break;
            case 'ret':
                //keybd_event(VK_RETURN, 0, 0, 0);
                return FALSE;
            case '?':
                break;
            default:
                if (m_LastFocused >= 0)
                {        
                    CWnd* pwnd = GetDlgItem(m_LastFocused);

                    if (pwnd->IsKindOf(RUNTIME_CLASS(CComboBox)) )
                    {
                        CCustomComboBox* ctl = (CCustomComboBox*)pwnd;  

                        //this method always returns 0 index for the 
                        //start and end position index
                        ctl->m_edit->GetSel(m_LastStPos, m_LastEndPos);
                    }
                }
        } break;
    }
}

The combo is subclassed like this:

BOOL CSymbolDlg::OnInitDialog() 
{
    CDialog::OnInitDialog(); 

    //combo is CCustomComboBox type
    combo.SubclassDlgItem(IDC_COMBO,this);

   //rest of the code...
}

And the CEdit control:

HBRUSH CCustomComboBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    if (nCtlColor == CTLCOLOR_EDIT)
    {
        //[ASCII 160][ASCII 160][ASCII 160]Edit control
        if (m_edit.GetSafeHwnd() == NULL)
            m_edit.SubclassWindow(pWnd->GetSafeHwnd());
    }
    else if (nCtlColor == CTLCOLOR_LISTBOX)
    {
        //ListBox control
        if (m_listbox.GetSafeHwnd() == NULL)
            m_listbox.SubclassWindow(pWnd->GetSafeHwnd());
    }
    HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);
    return hbr;
}
Blacktempel
  • 3,935
  • 3
  • 29
  • 53
ali
  • 529
  • 4
  • 26
  • 1
    How do you get the handle to the edit control in the combo box? How do you subclass the edit control in the Combobox? Sure that you use this handle and not the combo box handle to send EM_GETSEL? – xMRi Nov 26 '15 at 12:22
  • @xMRi, I posted the piece of code that calls the GetSel method. It does use the edit control object to call the GetSel method. – ali Nov 26 '15 at 14:17
  • Please show how you subclass the edit control for the combo. And, have you tried checking the DWORD that is returned from GetSel? – rrirower Nov 26 '15 at 15:15
  • @rrirower: Thanks. It is always zero. – ali Nov 26 '15 at 16:15
  • 1
    Why are you subclassing the same child window twice? Once as m_edit and once as m_listbox!? Did you check with Spy++ if you really subclass the right control as the edit field? – Werner Henze Nov 26 '15 at 16:55
  • Use GetComboBoxInfo to retrieve the edit Control handle. – xMRi Nov 26 '15 at 20:13
  • @WernerHenze The combobox has a edit and a list control. That method is called by the framework twice. The ::GetWindow method returns the handle of each control on each call. – ali Nov 27 '15 at 08:28
  • @xMRi To send EM_GETSEL message to the edit control? But why would you think the ctl->m_edit->GetSel(..) which is a wrapper on that message is wrong. – ali Nov 27 '15 at 08:32
  • I would never rely on enumeration to get the correct handle. I suppose you have a wrong one. So use the Information from GetComboBoxInfo for subclassing. This is my full advice. – xMRi Nov 27 '15 at 08:39
  • @ali No, in SetCustomPopupCtrl you are not using GetComboBoxInfo but subclassing the same window twice. – Werner Henze Nov 27 '15 at 08:51
  • Thanks @WernerHenze. I had posted a wrong method. It's updated. – ali Nov 27 '15 at 10:18
  • @ali What about SetCustomPopupCtrl? Is it still being called? – Werner Henze Nov 30 '15 at 10:17
  • No not anymore. I realised though that if ::SetFocus(ctl->m_edit->m_handl) is called right before ctl->m_edit->GetSel(..) on that switch case statement then the GetSel returns the correct positions. Only as it reset the focus the positions are always returned as 0 and last character index. – ali Nov 30 '15 at 16:08

0 Answers0