Good day! Please advise the Edit-component for numbers so that the numbers in it always end with "," (or a dot) and two digits.

  • I wrote how to do it? Not sure how to write the code? I just do not think that someone specifically made a library with components that would duplicate Edit and add such elementary things to its capabilities. - teanYCH

1 answer 1

Okay, I used to do this before, so I'll give you a sample code.

You create a class somewhere:

type TMyEdit = class(TEdit) // этот класс у нас будет отвечать за правостороннее расположение чисел в поле ввода protected procedure CreateParams(var Params: TCreateParams);override; end; 

and describe the procedure:

 procedure TMyEdit.CreateParams(var Params: TCreateParams); begin inherited CreateParams(Params); Params.Style:=Params.Style or ES_RIGHT; end; 

When creating a form, prescribe this for all financial Edit's, so that the numbers are displayed on the right:

  pPointer(Edit3)^:=TMyEdit; TMyEdit(Edit3).RecreateWnd; 

Write handlers for financial Edit's:

 procedure TForm1.Edit3Enter(Sender: TObject); begin if Edit3.Text = '0.00' then // ну тут все ясно Edit3.Text:= ''; end; procedure TForm1.Edit3Exit(Sender: TObject); var Text: string; begin Text:= Edit3.Text; if Pos('.', Edit3.Text) = 0 then // если пользователь не указал копейки, то добавляем нули вместе с точкой Edit3.Text:= Edit3.Text + '.00' else begin Delete(Text, 1, Pos('.', Text)); if length(Text) <= 2 then begin if Text = '' then Edit3.Text:= Edit3.Text + '00' else begin Delete(Text, 1, pos(text[1], text)); if Text = '' then Edit3.Text:= Edit3.Text + '0' end; end else begin // если много цифр после точки, то обрезаем их Text:= Copy(Text, 3, Length(Text)); Edit3.Text:= Copy(Edit3.Text, 1, Length(Edit3.Text) - Length(Text)); end; end; if Edit3.Text[1] = '.' then Edit3.Text:= '0.00' end; procedure TForm1.Edit3KeyPress(Sender: TObject; var Key: Char); var Text: string; begin if (Edit3.Text = '') and (key in [#46]) then Key:= #0; // Настройка маски ввода для поля if not (Key in ['0'..'9', #8, #46]) or ((Pos('.', Edit3.Text) <> 0) and (Key in [#46])) then Key := #0; end; 
  • Wrote this a long time ago, it seems to be working = / - teanYCH
  • Thanks, teanYCH! - leklerk