View Issue Details

IDProjectCategoryView StatusLast Update
0001798JEDI VCL00 JVCL Componentspublic2004-05-28 01:04
ReporterhamiltonAssigned Touser72 
PrioritynormalSeveritymajorReproducibilityalways
Status resolvedResolutionfixed 
Product Version 
Target VersionFixed in Version 
Summary0001798: JvDBDateTimePicker resets date portion of TDateTime when used as time control and vice verse.
DescriptionMy application uses TDateTime fields. I am attempting to use 2 JvDBDateTimePickers to represent the date and time portions of these fields separately.

The problem is that changing the date resets the time to midnight and changing the time resets the date to 1899.

A further complication occurs if the NullText property is used. Using ' ' (the space character) as the NullText results in the controls becoming disabled. Changing the date resets the time to midnight, displays a blank time and disables the time control. Changing the time does almost the same thing, except that the dropdown calendar is still enabled and dates may be set this way.
TagsNo tags attached.

Activities

user72

2004-05-27 00:30

  ~0004369

Here's a proposal: when the field type is ftDateTime, ftTimeStamp we don't clear the "opposite" value but if it is ftDate or ftTime we trunc/frac as appropriate. We could additionally add a boolean property to control this behavior (TrimValue). Anyway, try making the following changes:


  published
    property TrimValue:boolean read FTrimValue write FTrimValue default True;
  end;

constructor TJvDBDateTimePicker.Create(AOwner: TComponent);
begin
  ...
  FTrimValue := True;
end;

procedure TJvDBDateTimePicker.DataChange(Sender: TObject);
begin
  if FDataLink.Field <> nil then
  begin
    if Kind = dtkDate then
    begin
      if IsDateAndTimeField then
        DateTime := FDataLink.Field.AsDateTime
      else
        DateTime := Trunc(FDataLink.Field.AsDateTime);
    end
    else
    begin
      if IsDateAndTimeField then
        DateTime := FDataLink.Field.AsDateTime
      else
        DateTime := Frac(FDataLink.Field.AsDateTime);
    end;
  end
  else if csDesigning in ComponentState then
  begin
    DateTime := Now;
  end;
  CheckNullValue;
end;

procedure TJvDBDateTimePicker.KeyDown(var Key: Word; Shift: TShiftState);
begin
  // we still parent code
  inherited KeyDown(Key, Shift);
  // Is it Delete key, insert key or shiftstate ...
  case Key of
    VK_DELETE:
      begin
        FDataLink.Edit;
        if Kind = dtkDate then
        begin
          if IsDateAndTimeField then
            DateTime := NullDate
          else
            DateTime := Trunc(NullDate);
        end
        else
        begin
          if IsDateAndTimeField then
            DateTime := NullDate
          else
            DateTime := Frac(NullDate);
        end;
        CheckNullValue;
        UpdateData(Self);
      end;
    VK_INSERT:
      if ssShift in Shift then
        FDataLink.Edit;
  end;
end;

procedure TJvDBDateTimePicker.UpdateData(Sender: TObject);
begin
  // update value in datalink with date value in control, not from system
  if not FDataLink.Editing then
    Exit;
  if Kind = dtkDate then
  begin
    if Trunc(NullDate) = Trunc(DateTime) then
      FDataLink.Field.Value := NULL
    else if IsDateAndTimeField then
      FDataLink.Field.AsDateTime := DateTime
    else
      FDataLink.Field.AsDateTime := Trunc(DateTime);
  end
  else
  begin
    if Frac(NullDate) = Frac(DateTime) then
      FDataLink.Field.Value := NULL
    else if IsDateAndTimeField then
      FDataLink.Field.AsDateTime := DateTime;
    else
      FDataLink.Field.AsDateTime := Frac(DateTime);
  end;
end;

function TJvDBDateTimePicker.IsDateAndTimeField: Boolean;
begin
  with FDataLink do
    Result := (Field <> nil) and (Field.DataType in [ftDateTime, ftTimeStamp]) and not TrimValue;
end;

hamilton

2004-05-27 08:46

reporter   ~0004377

Thanks for the code Peter. I will try it out shortly then get back to you.

hamilton

2004-05-27 10:10

reporter   ~0004378

The code works perfectly. Cheers!

Being pedantic, there are 2 typos that you will pick up when you compile the code:
* FTrimValue needs to be declared
* There is an extra ';' in UpdateData

I think this bugnote can be closed.

I did discover two other problems while testing, I will post these as new bugnotes. [Sorry :( ]

user72

2004-05-27 10:41

  ~0004381

So, can you help me come up with a better name than "TrimValue"?

hamilton

2004-05-27 10:51

reporter   ~0004385

TrimValue is probably as good as it is going to get :) The alternative may be a short sentence!

I opened the NullText issue as a separate report. Have a look at what I've said there and let me know if you are still unable to replicate it.

user72

2004-05-27 11:12

  ~0004389

>The alternative may be a short sentence!
I was thinking about "RemoveDateOrTimePortionNotUsedBasedOnKindPropertyUnlessDataTypeIsDateTimeOrSQLTimeStamp" but wasn't sure it would be appreciated ;)

hamilton

2004-05-27 12:18

reporter   ~0004396

> "RemoveDateOrTimePortionNotUsedBasedOnKindPropertyUnlessDataTypeIsDateTimeOrSQLTimeStamp"

If variable names were all like that then you wouldn't need to write online help :D

user72

2004-05-27 12:34

  ~0004398

No, but you'd need a 52 inch screen at max resolution to read the code :)

user72

2004-05-28 01:04

  ~0004400

I've updated this part in CVS. Still need to find a solution for the NullText problem, though (0001805)

Issue History

Date Modified Username Field Change
2004-05-26 12:06 hamilton New Issue
2004-05-27 00:30 user72 Note Added: 0004369
2004-05-27 00:31 user72 Status new => feedback
2004-05-27 08:46 hamilton Note Added: 0004377
2004-05-27 10:10 hamilton Note Added: 0004378
2004-05-27 10:41 user72 Note Added: 0004381
2004-05-27 10:41 user72 Status feedback => assigned
2004-05-27 10:41 user72 Assigned To => user72
2004-05-27 10:51 hamilton Note Added: 0004385
2004-05-27 11:12 user72 Note Added: 0004389
2004-05-27 12:18 hamilton Note Added: 0004396
2004-05-27 12:34 user72 Note Added: 0004398
2004-05-28 01:04 user72 Status assigned => resolved
2004-05-28 01:04 user72 Resolution open => fixed
2004-05-28 01:04 user72 Note Added: 0004400