How to restrict the tree control labels to be integer?

18 01 2009


Tree Controls supports label editing and by default it accepts all kind of chars for label. But is there any trick to restrict the input label to be an integer?

limittreeinputasint


Yes. You can. When user starts editing tree control will send a notification message – TVN_BEGINLABELEDIT. At first you have to handle the message and in the message handler, you can get the label edit control by calling GetEditControl(). Then set the edit control style to ES_NUMBER. Then the label edit control of tree, will allow only integers as input. Have a look at the code snippet.

// Message Map.
BEGIN_MESSAGE_MAP(CRabbitDlg, CDialog)
	...
	ON_NOTIFY(TVN_BEGINLABELEDIT, IDC_TREECTRL, OnBeginlabeleditTreectrl)
END_MESSAGE_MAP()

// Begin Label edit - Message Handler.
void CRabbitDlg::OnBeginlabeleditTreectrl(NMHDR* pNMHDR,
					  LRESULT* pResult)
{
    // Get the edit control which of tree control and set
    // its style to number only.
    CEdit* pEdit = m_TreeCtrl.GetEditControl();
    if( pEdit )
    {
        // Allow only numbers
        pEdit->ModifyStyle( 0, ES_NUMBER );
    }

    *pResult = 0;
}


Feel the power!


Targeted Audience – Intermediate.

Advertisements