Edit - Somente com entrada para numeros

Top  Previous  Next

Every developer needs any editbox where end-user can enter only numbers. 

This is very popular task and you may find a lot of such topics in the 

public forums/newsgroups. 

 

But most answers in forums are incorrect as I see, because they suggest 

to write some code in OnKeyPress handler and to check there what is a 

character pressed. 

I say that this code is an incorrect because user could enter any 

alfa-characters there anyway (via clipboard, for example). 

 

Today I want to post a simple code that allow to solve this task without 

any problems. 

Microsoft have the ES_NUMBER style for any editbox. So all what you need 

is to send this flag for your editbox: 

 

var 

  defstyle: dWord; 

begin 

  defstyle := GetWindowLong(Edit1.Handle, GWL_STYLE); 

  SetWindowLong(Edit1.Handle, GWL_STYLE, defstyle or ES_NUMBER) 

end

 

If you also want to have a right aligned number, send additionally the 

ES_RIGHT flag there 

 

It's all - only numeric characters are possible and this is controled on 

OS level. Hope that you'll like this tip...