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

Actions

Information

One response

30 04 2009
roman

Good, thanks. But what If I also want negative numbers? I.e. -55 ? “-” doesn’t seem to be valid for ES_NUMBER.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s




%d bloggers like this: