cxcurrencyedit.pas

来自「胜天进销存源码,国产优秀的进销存」· PAS 代码 · 共 747 行 · 第 1/2 页

PAS
747
字号
  Result := cxFormatController.CurrencyFormat;
end;

function TcxCustomCurrencyEditProperties.GetDisplayFormatOptions: TcxEditDisplayFormatOptions;
begin
  Result := [dfoSupports, dfoNoCurrencyValue];
end;

function TcxCustomCurrencyEditProperties.HasDigitGrouping(
  AIsDisplayValueSynchronizing: Boolean): Boolean;
begin
  Result := not AIsDisplayValueSynchronizing and UseThousandSeparator;
end;

function TcxCustomCurrencyEditProperties.InternalGetEditFormat(out AIsCurrency,
  AIsOnGetTextAssigned: Boolean; AEdit: TcxCustomTextEdit = nil): string; //for VCL .Net
begin
  Result :=
    inherited InternalGetEditFormat(AIsCurrency, AIsOnGetTextAssigned, AEdit);
end;

function TcxCustomCurrencyEditProperties.IsEditValueNumeric: Boolean;
begin
  Result := True;
end;

function TcxCustomCurrencyEditProperties.StrToFloatEx(S: string;
  var Value: Double): Boolean;
const
  MinDouble = 5.0e-324;
  MaxDouble = 1.7e+308;
var
  E: Extended;
  I: Integer;
begin
  for I := Length(S) downto 1 do
    if S[I] = ThousandSeparator then
      Delete(S, I, 1);
  if not TextToFloat(PChar(S), E, fvExtended) or
    ((E <> 0) and ((Abs(E) < MinDouble) or (Abs(E) > MaxDouble))) then
  begin
    Value := 0;
    Result := S = '';
  end
  else
  begin
    Value := E;
    Result := True;
  end;
end;

procedure TcxCustomCurrencyEditProperties.Assign(Source: TPersistent);
begin
  if Source is TcxCustomCurrencyEditProperties then
  begin
    BeginUpdate;
    try
      inherited Assign(Source);
      with TcxCustomCurrencyEditProperties(Source) do
      begin
        Self.AssignedValues.DecimalPlaces := False;
        if AssignedValues.DecimalPlaces then
          Self.DecimalPlaces := DecimalPlaces;
        Self.Nullable := Nullable;
        Self.NullString := NullString;
        Self.UseThousandSeparator := UseThousandSeparator;
      end;
    finally
      EndUpdate
    end
  end
  else
    inherited Assign(Source);
end;

class function TcxCustomCurrencyEditProperties.GetContainerClass: TcxContainerClass;
begin
  Result := TcxCurrencyEdit;
end;

function TcxCustomCurrencyEditProperties.GetEditValueSource(AEditFocused: Boolean): TcxDataEditValueSource;
begin
  Result := evsValue;
  if not AEditFocused and (IDefaultValuesProvider <> nil) and
    IDefaultValuesProvider.IsOnGetTextAssigned then
      Result := evsText;
end;

function TcxCustomCurrencyEditProperties.IsDisplayValueValid(
  var DisplayValue: TcxEditValue; AEditFocused: Boolean): Boolean;
var
  C, AIsOnGetTextAssigned: Boolean;
  D: Double;
  S: string;
begin
  Result := not AEditFocused or
    (InternalGetEditFormat(C, AIsOnGetTextAssigned) <> '') or
    AIsOnGetTextAssigned;
  if not Result then
  begin
    S := Trim(VarToStr(DisplayValue));
    Result := StrToFloatEx(S, D);
    if Result then
      DisplayValue := S;
  end;
end;

procedure TcxCustomCurrencyEditProperties.PrepareDisplayValue(
  const AEditValue: TcxEditValue; var DisplayValue: TcxEditValue;
  AEditFocused: Boolean);
begin
  if VarIsSoftNull(AEditValue) then
    if AEditFocused then
      DisplayValue := ''
    else
      if Nullable then
        DisplayValue := NullString
      else
        inherited PrepareDisplayValue(0, DisplayValue, AEditFocused)//DisplayValue := '0'
  else
    try
      inherited PrepareDisplayValue(AEditValue, DisplayValue, AEditFocused);
    except
      on EConvertError do
        if AEditFocused then
          DisplayValue := ''
        else
          DisplayValue := AEditValue;
      on EVariantError do
        if AEditFocused then
          DisplayValue := ''
        else
          DisplayValue := AEditValue;
    end;
end;

procedure TcxCustomCurrencyEditProperties.ValidateDisplayValue(
  var ADisplayValue: TcxEditValue; var AErrorText: TCaption;
  var AError: Boolean; AEdit: TcxCustomEdit);
var
  AValue: Double;
begin
  AError := not StrToFloatEx(VarToStr(ADisplayValue), AValue);
  inherited ValidateDisplayValue(ADisplayValue, AErrorText, AError, AEdit);
end;

{ TcxCustomCurrencyEdit }

function TcxCustomCurrencyEdit.GetProperties: TcxCustomCurrencyEditProperties;
begin
  Result := TcxCustomCurrencyEditProperties(FProperties);
end;

function TcxCustomCurrencyEdit.GetActiveProperties: TcxCustomCurrencyEditProperties;
begin
  Result := TcxCustomCurrencyEditProperties(InternalGetActiveProperties);
end;

function TcxCustomCurrencyEdit.GetValue: Double;
var
  V: Variant;
begin
  if Focused and not IsEditValidated and ModifiedAfterEnter then
  begin
    V := InternalGetEditingValue;
    if VarIsNumericEx(V) then
      Result := V
    else
      Result := 0.0;
  end
  else
    if VarIsNull(EditValue) or (VarIsStr(EditValue) and (StrToCurrDef(EditValue, 0) = 0)) then
      Result := 0.00
    else
      Result := EditValue;
end;

procedure TcxCustomCurrencyEdit.SetProperties(
  Value: TcxCustomCurrencyEditProperties);
begin
  FProperties.Assign(Value);
end;

procedure TcxCustomCurrencyEdit.SetValue(Value: Double);
begin
  with ActiveProperties do
    if IsValueBoundDefined(evbMin) and (Value < MinValue) then
      Value := MinValue
    else
      if IsValueBoundDefined(evbMax) and (Value > MaxValue) then
        Value := MaxValue;
  InternalEditValue := Value;
end;

procedure TcxCustomCurrencyEdit.CheckEditorValueBounds;
begin
  KeyboardAction := ModifiedAfterEnter;
  try
    with ActiveProperties do
      if IsValueBoundDefined(evbMin) and (Value < MinValue) then
        Value := MinValue
      else
        if IsValueBoundDefined(evbMax) and (Value > MaxValue) then
          Value := MaxValue;
  finally
    KeyboardAction := False;
  end;
end;

procedure TcxCustomCurrencyEdit.Initialize;
begin
  inherited Initialize;
  ControlStyle := ControlStyle - [csSetCaption];
end;

function TcxCustomCurrencyEdit.InternalGetEditingValue: TcxEditValue;
begin
  PrepareEditValue(Text, Result, True);
end;

function TcxCustomCurrencyEdit.IsValidChar(Key: Char): Boolean;

  function IsValidNumber(const S: string): Boolean;
  var
    ADecPos, AStartPos: Integer;
    V: Double;
  begin
    Result := False;
    ADecPos := Pos(DecimalSeparator, S);
    if ADecPos > 0 then
    begin
      AStartPos := Pos('E', UpperCase(S));
      if AStartPos > ADecPos then
        ADecPos := AStartPos - ADecPos - 1
      else
        ADecPos := Length(S) - ADecPos;
      if ADecPos > ActiveProperties.DecimalPlaces then
        Exit;
    end;
    Result := ActiveProperties.StrToFloatEx(S, V);
  end;

var
  AEndPos, AStartPos: Integer;
  AIsCurrency, AIsOnGetTextAssigned: Boolean;
  S: string;
begin
  Result := False;
  if not IsNumericChar(Key, ntExponent) and
    not (ActiveProperties.UseThousandSeparator and (Key = ThousandSeparator)) then
      Exit;
  if ((ActiveProperties.InternalGetEditFormat(AIsCurrency, AIsOnGetTextAssigned, Self) <> '') or
    AIsOnGetTextAssigned) and not IsValidNumber(Text) then
  begin
    Result := True;
    Exit;
  end;
  S := Text;
  AStartPos := SelStart;
  AEndPos := SelStart + SelLength;
  Delete(S, SelStart + 1, AEndPos - AStartPos);
  if (Key = '-') and (S = '') then
  begin
    Result := True;
    Exit;
  end;
  Insert(Key, S, AStartPos + 1);
  Result := IsValidNumber(S);
end;

procedure TcxCustomCurrencyEdit.KeyPress(var Key: Char);
begin
  if not (ActiveProperties.UseThousandSeparator and (Key = ThousandSeparator)) and
    (Key in ['.', ',']) then
    Key := DecimalSeparator;
  inherited KeyPress(Key);
end;                

procedure TcxCustomCurrencyEdit.PropertiesChanged(Sender: TObject);
begin
  if (Sender <> nil) and ActiveProperties.FFormatChanging then
    Exit;
  if not Focused then
    DataBinding.UpdateDisplayValue;
  inherited PropertiesChanged(Sender);
end;

class function TcxCustomCurrencyEdit.GetPropertiesClass: TcxCustomEditPropertiesClass;
begin
  Result := TcxCustomCurrencyEditProperties;
end;

procedure TcxCustomCurrencyEdit.PasteFromClipboard;
begin
  if CanModify then
    inherited;
end;

procedure TcxCustomCurrencyEdit.PrepareEditValue(
  const ADisplayValue: TcxEditValue; out EditValue: TcxEditValue;
  AEditFocused: Boolean);
var
  V: Double;
begin
  if (ADisplayValue = '') or (CompareStr(ADisplayValue, ActiveProperties.NullString) = 0) or
    not ActiveProperties.StrToFloatEx(ADisplayValue, V) then
      if ActiveProperties.Nullable then EditValue := Null else EditValue := 0.00
  else
    EditValue := V
end;

{ TcxCurrencyEdit }

class function TcxCurrencyEdit.GetPropertiesClass: TcxCustomEditPropertiesClass;
begin
  Result := TcxCurrencyEditProperties;
end;

function TcxCurrencyEdit.GetActiveProperties: TcxCurrencyEditProperties;
begin
  Result := TcxCurrencyEditProperties(InternalGetActiveProperties);
end;

function TcxCurrencyEdit.GetProperties: TcxCurrencyEditProperties;
begin
  Result := TcxCurrencyEditProperties(FProperties);
end;

procedure TcxCurrencyEdit.SetProperties(Value: TcxCurrencyEditProperties);
begin
  FProperties.Assign(Value);
end;

{ TcxFilterCurrencyEditHelper }

class function TcxFilterCurrencyEditHelper.GetFilterEditClass: TcxCustomEditClass;
begin
  Result := TcxCurrencyEdit;
end;

class function TcxFilterCurrencyEditHelper.GetSupportedFilterOperators(
  AProperties: TcxCustomEditProperties;
  AValueTypeClass: TcxValueTypeClass;
  AExtendedSet: Boolean = False): TcxFilterControlOperators;
begin
  Result := [fcoEqual, fcoNotEqual, fcoLess, fcoLessEqual, fcoGreater,
    fcoGreaterEqual, fcoBlanks, fcoNonBlanks];
  if AExtendedSet then
    Result := Result + [fcoBetween, fcoNotBetween, fcoInList, fcoNotInList];
end;

class procedure TcxFilterCurrencyEditHelper.InitializeProperties(AProperties,
  AEditProperties: TcxCustomEditProperties; AHasButtons: Boolean);
begin
  inherited InitializeProperties(AProperties, AEditProperties, AHasButtons);
  with TcxCustomCurrencyEditProperties(AProperties) do
  begin
    MinValue := 0;
    MaxValue := 0;
    Nullable := True;
    NullString := '';
  end;
end;

initialization
  GetRegisteredEditProperties.Register(TcxCurrencyEditProperties, scxSEditRepositoryCurrencyItem);
  FilterEditsController.Register(TcxCurrencyEditProperties, TcxFilterCurrencyEditHelper);

finalization
  FilterEditsController.Unregister(TcxCurrencyEditProperties, TcxFilterCurrencyEditHelper);

end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?