Click to See Complete Forum and Search --> : numeric edit control


Kristin Wheeler
April 30th, 1999, 10:48 AM
How do you set an edit control to display an integer? I have Number selected in the control properties, so there is no problem with the control accepting only numbers. However, I want to set a 'default' when the edit control is displayed. SetWindowText won't work because the variable I want to use is an integer. If I type cast it to a char*, nothing happens. Also, nothing happens if I use SetDlgItemInt. Any suggestions? Thank you.

Lynx
April 30th, 1999, 11:27 AM
You can use ClassWizard to add a member variable for the editbox. Category should be "Value" and Variable type can be "long", "int", "short"...
After you add a new variable, e.q. m_editValue, you can update or retrieve the number to/from the editbox by calling UpdateData().

For example, in OnInitDialog()

m_editValue = 10; // default value
// other initialization
CDialog::OnInitDialog();
return TRUE;

When you want to get the value, call UpdateData(TRUE) to set the m_editValue.
Call UpdateData(FALSE) when you want to update the editbox. Using MFC DDX is much easier than calling GetWindowText and convert string to the desired format.

Hope this will help. Good luck.

Lynx

AndrewZhang
May 6th, 1999, 01:38 AM
You can build a new class dirived form CEdit.
Deal with the WM_CHAR Message in this class such as:
void ::Char(nChar .....)
{
static CString separators(_T("."));
TCHAR tChar=(TCHAR)nChar;
if((IsCharAlphaNumeric(tChar) && !IsCharAlpha(tChar)) ||
separators.Find (nChar)!=-1 || nChar==0x8)
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
else
MessageBox("Please input the number!",
"Input error",
MB_ICONSTOP|MB_OK);
}

You can try it .
AndrewZhang e-mail:xjtuandrew@hotmail.com