Andrew Truckle
Andrew Truckle

Reputation: 19107

Exposing the current combo selection index for the CGridCellCombo class

For several years I have been using the CGridCellCombo class. It is designed to be used with the CGridCtrl.

Several years ago I did make a request in the comments section for an enhancement but I got no replies.

The basic concept of the CGridCellCombo is that it works with the text value of the cell. Thus, when you present the drop list it will have that value selected. Under normal circumstances this is fine.

But I have places where I am using the combo as a droplist. In some situations it is perfectly fine to continue to use the text value as the go-between.

But is some situations it would have been ideal to know the actual selected index of the combo. When I have a droplist and it is translated into 30 languages, and I need to know the index, I have no choice but to load the possible options for that translation and then examine the cell value and based on the value found in the array I know the index.

It works, but is not very elegant. I did spend a bit of time trying to keep track of the selected index by adding a variable to CInPlaceList and setting it but. I then added a wrapper method to the CGridCellCombo to return that value. But it didn't work.

I wondered if anyone here has a good understanding of the CGridCellCombo class and might be able to advise me in exposing the CComboCell::GetCurSel value.

I know that the CGridCtrl is very old but I am not away of another flexible grid control that is designed for MFC.

Upvotes: 0

Views: 464

Answers (2)

Andrew Truckle
Andrew Truckle

Reputation: 19107

I have found this to be the simplest solution:

int CGridCellCombo::GetSelectedIndex()
{
    int iSelectedIndex = CB_ERR;

    CString strText = GetText();
    for (int iOption = 0; iOption < m_Strings.GetSize(); iOption++)
    {
        if (strText.CollateNoCase(m_Strings[iOption]) == 0) // Match
        {
            iSelectedIndex = iOption;
            break;
        }
    }

    return iSelectedIndex;
}

Upvotes: 0

xMRi
xMRi

Reputation: 15365

The value that is transfered back to the CGridCtrl is choosen in CInPlaceList::EndEdit. The internal message GVN_ENDLABELEDIT is used, and this message always use a text to set it into the grid.

The value is taken here via GetWindowText from the control. Feel free to overwrite this behaviour.

The handler CGridCtrl::OnEndInPlaceEdit again calls OnEndEditCell. All take a string send from GVN_ENDLABELEDIT.

When you want to make a difference between the internal value and the selected value you have to manage this via rewriting the Drawing and selecting. The value in the grid is the GetCurSel value and you have to show something different... There isn't much handling about this in the current code to change.

More information

The key is CInPlaceList::EndEdit(). There is a call to GetWindowText (CInPlaceList is derived from CComboBox), just get the index here. Also in CGridCellCombo::EndEdit you have access to the m_pEditWnd, that is the CInPlaceList object and derived from CComboBox, so you have access here too.

Upvotes: 1

Related Questions