CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 1999
    Posts
    2

    numeric edit control

    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.


  2. #2
    Join Date
    Apr 1999
    Location
    CA, USA
    Posts
    78

    Re: numeric edit control

    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


  3. #3
    Join Date
    May 1999
    Posts
    11

    Re: numeric edit control

    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:[email protected]


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured