Integer-only Edit control

Implementing an integer-only edit control requires little coding, and indeed does not require you to handle the OnKey* events to check the contents to allow only digits.  Windows introduces the style ES_NUMBER for such occasions.  To specify this style, you can either derive a component from TEdit and override the CreateParams method, or alternatively, use the GetWindowLong() / SetWindowLong() comination as demonstrated below.

KEYWORDS: ES_NUMBER, CreateParams, GetWindowLong, SetWindowLong
 

 

//-----------------------------------------------------------------------------
//in source...

__fastcall TForm1::TForm1(TComponent* Owner)
     : TForm(Owner)
{
    //get the current style flags
    LONG dwStyle = GetWindowLong(Edit1->Handle, GWL_STYLE);

    //add ES_NUMBER to these
    SetWindowLong(Edit1->Handle, GWL_STYLE, dwStyle | ES_NUMBER);
}