Recent

Author Topic: Formatting textbox result  (Read 6901 times)

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Formatting textbox result
« Reply #15 on: August 17, 2017, 03:18:27 am »
Of course it can be done with a normal Edit... even better with TNumEdit...  :)
Hey Dmitri, try this if you like and test it... (press CTRL+F11 and load the lpi file...)

EDIT: some improvements...  :D
Code: Pascal  [Select][+][-]
  1.   Function MyRound(dNum: Double): String;
  2.    Begin
  3.     If Frac(dNum) = 0
  4.     Then Result:= FloatToStr(dNum) // don't round if frac = 0
  5.     Else Result:= IntToStr(Trunc(dNum)+1); // round if frac > 0
  6.    End;

Code: Pascal  [Select][+][-]
  1. UNIT Unit1;
  2.  {$MODE OBJFPC}{$H+}{$J-}
  3.  
  4. Interface
  5.  USES
  6.   Classes, SysUtils, Forms, Controls, Graphics, uNumEdit,
  7.   StdCtrls; // Only for TLabel;
  8.  
  9.  TYPE
  10.   TForm1 = Class(TForm)
  11.  
  12.    Procedure FormCreate        (Sender: TObject);
  13.    Procedure AvgBagsPerPaxExit (Sender: TObject);
  14.    Procedure NoContextPopup    (Sender: TObject; MousePos: TPoint;
  15.                                 Var Handled: Boolean);
  16.     PRIVATE
  17.      edtBookedPax,
  18.      edtAvgBagsPerPax,
  19.      edtTotalBagPieces: TNumEdit; // NumbersOnlyEdit
  20.   End;                            // with/without decimal separator
  21.  
  22.  VAR
  23.   Form1: TForm1;
  24.  
  25. Implementation
  26.  {$R *.LFM}
  27.  
  28.  
  29. Procedure TForm1.FormCreate(Sender: TObject);
  30.   Var
  31.    lab: TLabel;
  32.  Begin
  33.   SetBounds((Screen.WorkAreaWidth -500) Div 2,
  34.             (Screen.WorkAreaHeight-300) Div 2,
  35.              500, 300);
  36.  
  37.   Constraints.MinHeight:= 200;
  38.   Constraints.MinWidth := 400;
  39.  
  40.   edtBookedPax                 := TNumEdit.Create(Self);
  41.   edtBookedPax.Align           := alTop;
  42.   edtBookedPax.Caption         := '202';
  43.   edtBookedPax.Font.Size       := 20;
  44.   edtBookedPax.Font.Quality    := fqAntialiased;
  45.   edtBookedPax.DecimalSeparator:= False; // Only Numbers
  46.   edtBookedPax.OnContextPopup  := @NoContextPopup;
  47.   edtBookedPax.Parent          := Self;
  48.  
  49.   edtAvgBagsPerPax                 := TNumEdit.Create(Self);
  50.   edtAvgBagsPerPax.Align           := alTop;
  51.   edtAvgBagsPerPax.Caption         := '1,3';
  52.   edtAvgBagsPerPax.Font.Size       := 20;
  53.   edtAvgBagsPerPax.Font.Quality    := fqAntialiased;
  54.   edtAvgBagsPerPax.OnExit          := @AvgBagsPerPaxExit;
  55.   edtAvgBagsPerPax.OnContextPopup  := @NoContextPopup;
  56.   edtAvgBagsPerPax.DecimalSeparator:= True; // Numbers and DecimalSeparator
  57.   edtAvgBagsPerPax.Parent          := Self;
  58.  
  59.   edtTotalBagPieces                 := TNumEdit.Create(Self);
  60.   edtTotalBagPieces.Caption         := '';
  61.   edtTotalBagPieces.Align           := alBottom;
  62.   edtTotalBagPieces.Font.Size       := 20;
  63.   edtTotalBagPieces.Font.Quality    := fqAntialiased;
  64.   edtTotalBagPieces.DecimalSeparator:= False; // Only Numbers
  65.   edtTotalBagPieces.OnContextPopup  := @NoContextPopup;
  66.   edtTotalBagPieces.Parent          := Self;
  67.  
  68.   lab             := TLabel.Create(Application);
  69.   lab.Font.Size   := 15;
  70.   lab.Font.Quality:= fqAntialiased;
  71.   lab.Align       := alBottom;
  72.   lab.Caption     := 'Edit1 = AvgBagsPerPax'+sLineBreak+
  73.                      'Edit2 = BookedPax'    +sLineBreak+
  74.                      'Edit3 = TotalBagPieces';
  75.   lab.Parent      := Self;
  76.  End;
  77.  
  78.  
  79. Procedure TForm1.NoContextPopup(Sender: TObject; MousePos: TPoint;
  80.                                 Var Handled: Boolean);
  81.  Begin
  82.   Handled:= True; // don't show context menu (no paste with mouse)
  83.  End;
  84.  
  85.  
  86. Procedure TForm1.AvgBagsPerPaxExit(Sender: TObject);
  87.  
  88.   Function MyRound(dNum: Double): String;
  89.    Begin
  90.     If Frac(dNum) = 0
  91.     Then Result:= FloatToStr(dNum) // don't round if frac = 0
  92.     Else Result:= IntToStr(Trunc(dNum)+1); // round if frac > 0
  93.    End;
  94.  
  95.  Begin
  96.   // if empty...
  97.   If (edtAvgBagsPerPax.Text = '') Or (edtBookedPax.Text = '')
  98.   Then
  99.    Begin
  100.     edtTotalBagPieces.Clear;
  101.     Exit;
  102.    End;
  103.  
  104.   If Not edtAvgBagsPerPax.IsValidInput // valid decimal or cardinal
  105.   Then
  106.    Begin
  107.     edtAvgBagsPerPax.Clear;
  108.     edtTotalBagPieces.Clear;
  109.    End
  110.   Else edtTotalBagPieces.Text:= MyRound((StrToInt  (edtBookedPax.Text) *
  111.                                          StrToFloat(edtAvgBagsPerPax.Text)));
  112.  End;
  113.  
  114. END.

Code: Pascal  [Select][+][-]
  1. UNIT uNumEdit;
  2. {$MODE OBJFPC}{$H+}{$J-}
  3.  
  4. Interface
  5.  USES
  6.   Classes, SysUtils, StdCtrls, LCLType;
  7.  
  8.  TYPE
  9.   TNumEdit = Class(TEdit)
  10.  
  11.    PRIVATE
  12.     booDecimal: Boolean;
  13.  
  14.     Procedure ClipbrdNumbers;
  15.  
  16.    PROTECTED
  17.     Procedure KeyDown  (Var Key: Word; Shift : TShiftState); Override;
  18.     Procedure KeyPress (Var Key: Char); Override;
  19.  
  20.    PUBLIC
  21.     Function NumbersOnly     : Boolean; // always set to false, not needed...
  22.     Function IsValidDecimal  : Boolean;
  23.     Function IsValidInput    : Boolean;
  24.     Property DecimalSeparator: Boolean
  25.               READ booDecimal WRITE booDecimal DEFAULT False;
  26.   End;
  27.  
  28. Implementation
  29.   USES Clipbrd;
  30.  
  31.  
  32. Procedure TNumEdit.ClipbrdNumbers;
  33.   Var
  34.    strPaste,
  35.    strNum,
  36.    strEdit,
  37.    strOut: String;
  38.  
  39.    arrCharNum: Array[0..10] Of Char =
  40.                ('1','2','3','4','5','6','7','8','9','0',',');
  41.    i, iNum,
  42.    iCursor,
  43.    iRange: Integer;
  44.  Begin
  45.   strPaste:= ClipBoard.AsText;
  46.    If strPaste = '' Then Exit;
  47.  
  48.   If SelLength > 0 Then SelText:= '';
  49.    strEdit:= Text;
  50.    iCursor:= SelStart;
  51.  
  52.   If booDecimal
  53.   Then
  54.    Begin
  55.     arrCharNum[10]:= FormatSettings.DecimalSeparator;
  56.     iRange:= 0;
  57.    End
  58.   Else iRange:= -1;
  59.  
  60.   For i:= 1 To Length(strPaste)
  61.   Do
  62.    Begin
  63.     For iNum:= 0 To High(arrCharNum)+iRange
  64.     Do
  65.      Begin
  66.       If strPaste[i] = arrCharNum[iNum]
  67.       Then strNum:= strNum + strPaste[i];
  68.      End;
  69.    End;
  70.  
  71.   For i:= 1 To iCursor
  72.   Do strOut:= strOut + strEdit[i];
  73.  
  74.   strOut:= strOut + strNum;
  75.  
  76.   For i:= iCursor+1 To Length(strEdit)
  77.   Do strOut:= strOut + strEdit[i];
  78.  
  79.   Text    := strOut;
  80.   SelStart:= iCursor + Length(strNum);
  81.  End;
  82.  
  83.  
  84. Procedure TNumEdit.KeyDown(Var Key: Word; Shift: TShiftState);
  85.  Begin
  86.   If (ssCTRL In Shift)
  87.   Then
  88.    Begin
  89.     If Key = VK_V
  90.     Then
  91.      Begin
  92.       Key:= 0;
  93.       ClipbrdNumbers;
  94.      End;
  95.    End;
  96.  
  97.  
  98.   If (ssSHIFT In Shift)
  99.   Then
  100.    Begin
  101.     If Key = VK_INSERT
  102.     Then
  103.      Begin
  104.       Key:= 0;
  105.       ClipbrdNumbers;
  106.      End;
  107.    End;
  108.  End;
  109.  
  110.  
  111. Procedure TNumEdit.KeyPress(Var Key: Char);
  112.   Var
  113.    c: Char;
  114.  Begin
  115.   If booDecimal
  116.   Then c:= FormatSettings.DecimalSeparator
  117.   Else c:= #0;
  118.  
  119.   If Not (Key In ['0'..'9', c, #8, #13])
  120.   Then Key:= #0;
  121.  End;
  122.  
  123.  
  124. Function TNumEdit.IsValidDecimal: Boolean;
  125.   Var
  126.    c             : Char;
  127.    cCount        : Cardinal;
  128.    strEdit       : String;
  129.    i, iLengthEdit: Integer;
  130.  Begin
  131.   Result:= False;
  132.  
  133.    strEdit    := Text;
  134.    iLengthEdit:= Length(Text);
  135.    c          := FormatSettings.DecimalSeparator;
  136.    cCount     := 0;
  137.  
  138.    For i:= 1 To iLengthEdit
  139.    Do
  140.     Begin
  141.      If strEdit[i] = c
  142.      Then Inc(cCount);
  143.     End;
  144.  
  145.    If (cCount = 1)
  146.     And Not (strEdit[1] = c)
  147.     And Not (strEdit[iLengthEdit] = c)
  148.    Then Result:= True;
  149.  End;
  150.  
  151.  
  152. Function TNumEdit.IsValidInput: Boolean;
  153.   Var
  154.    c             : Char;
  155.    cCount        : Cardinal;
  156.    strEdit       : String;
  157.    i, iLengthEdit: Integer;
  158.  Begin
  159.   Result:= False;
  160.  
  161.    strEdit    := Text;
  162.    iLengthEdit:= Length(Text);
  163.    c          := FormatSettings.DecimalSeparator;
  164.    cCount     := 0;
  165.  
  166.    For i:= 1 To iLengthEdit
  167.    Do
  168.     Begin
  169.      If strEdit[i] = c
  170.      Then Inc(cCount);
  171.     End;
  172.  
  173.    If (cCount < 2)
  174.     And Not (strEdit[1] = c)
  175.     And Not (strEdit[iLengthEdit] = c)
  176.    Then Result:= True;
  177.  End;
  178.  
  179.  
  180. Function TNumEdit.NumbersOnly: Boolean;
  181.  Begin
  182.   // we don't need the original one...
  183.   Result:= False;
  184.  End;
  185.  
  186. END.
« Last Edit: August 26, 2017, 03:23:48 am by RAW »
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

Dmitri Dumas

  • Newbie
  • Posts: 6
Re: Formatting textbox result
« Reply #16 on: August 17, 2017, 07:48:40 am »
 :D

To all you wonderful folks that responded, I say thank you. I have taken what you folks have said and implemented them. At my age (51), I had to learn to program from scratch after spending many many years in another field completely. It is refreshing to know that there are still folks online that are willing to share their knowledge and willing to help where they can.

Once again MANY MANY thanks and fondest regards
Dmitri

 

TinyPortal © 2005-2018