Link to home
Start Free TrialLog in
Avatar of Norm-al
Norm-alFlag for United States of America

asked on

Can I edit the TDateTime Picker to add a button below 'Today' and 'Clear'?

I have date fields that do not apply to some records but I do not want the users NOT entering anything in because it just looks like it was just skipped. Is it possible to add a button to the datetime picker called 'Not Applicable' that when clicked (below the Today and Clear buttons) would post a fake date '1/1/1900' to the field and the field then shows all values where date = 1/1/1900 show it as 'Not Applicable' in both the field and in the grid?
Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

The TDateTimePicker is really just a wrapper for the Windows common control.
JVCL has one but written from the ground up, to my dismay as well as hundreds of others, you can't just pick out one or two controls from JVCL to install. You have to install the entire package.

You may try one here to see if you can find a free one with source to modify yourself:
http://www.torry.net/pages.php?id=297
ASKER CERTIFIED SOLUTION
Avatar of developmentguru
developmentguru
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In addition, the dropdown is actually a TMonthCalendar. mullet_attack wrote some code way back in 2000 showing a fully operational TMonthCalendar replacement:
https://www.experts-exchange.com/questions/10321846/Create-a-new-component.html

I'm sure you could modify it and then write your own TDateTimePicker to use this new calendar as the dropdown.
developmentguru, who said anything about a grid?

BTW, I have modified DateTimePicker code to show something different for a null date. I'll try to locate it.
The original poster did LOL
He is not editing in the grid from what I read.
Avatar of Norm-al

ASKER

SHE is displaying the 'Not Applicable' in the grid AND in the data entry form datetime field. OnGetDisplayText worked fine to show 'Not Applicable' if the date = 1/1/1900 but what I cannot do now is to show "Not Applicable" in the text of the datetime field... because incompatible type of string and date I am thinking... is there a way to show NA in a datetime field?
You are using a TDateTimePicker in the data entry form, correct?

I used this code to change the display to something other than the dates. However, this particular code I have a few problems with, I don't remember, it was 6 yrs ago. I don't have the full working code because I no longer work there and not sure if I have it on my home system, although I'll do a grep search to see if I can find what I'm looking for on my home system and post it if I find it.

My suggestion is to begin looking at the messages being sent to the DTP.


procedure TCSDateTimeEdit.CheckValue;
var
  LResult: Boolean;
begin
  // Only change the format at run-time!
  if ( not (csDesigning in ComponentState) ) then
  begin
    LResult := ((Kind = dtkDate) and (Trunc(DateTime) = Trunc(HighDate))
or
              ((Kind = dtkTime) and WithinDelta(DateTime, HighDate)));
    if LResult then
    begin
      SendMessage(Handle, DTM_SETFORMAT, 0, Integer(PChar('''' +
FHighText + '''')));
    end
    else
    begin
      LResult := ((Kind = dtkDate) and (Trunc(DateTime) = Trunc
(LowDate)) or
                ((Kind = dtkTime) and WithinDelta(DateTime, LowDate)));
      if LResult then
      begin
        SendMessage(Handle, DTM_SETFORMAT, 0, Integer(PChar('''' +
FLowText + '''')));
      end
      else
      begin
        LResult := ((Kind = dtkDate) and (Trunc(DateTime) = Trunc
(NullDate)) or
                   ((Kind = dtkDate) and (Date = Unassigned)) or
                   ((Kind = dtkTime) and WithinDelta(DateTime,
NullDate)));
        if LResult then
        begin
          SendMessage(Handle, DTM_SETFORMAT, 0, Integer(PChar('''' +
FNullText + '''')));
        end
        else
        begin
          SendMessage(Handle, DTM_SETFORMAT, 0, Integer(PChar(Format)));
        end;
      end;
    end;
  end;
end;

Open in new window

BTW, the code above was from a DTP descendant, kind of like the TJvDateTimepicker from JVCL.
The TDateTimePicker, as I recall, is really focused on maintaining a valid date.  This will make it nearly impossible to force text into the control.  There are other controls that can emulate the date time picker... make it look like a date time picker without being so picky on the text in the control.  You could use part of my suggestions to pop up a form that has the calendar and the extra button you wanted (or if you want to put the extra work into it, make it drop down).  This could be as easy as placing a non-focusable button next to a read only edit field.  When the user clicks the button then the form is displayed allowing the user to select a date or click the button for 'Not Applicable'.  You may want the fields to default to make it easier on the user as well (if there is a logical default anyway).

  The TDateTimePicker is both behaving in ways you do not want (disallowing non-date text) and not behaving in ways that you do want it to (no extra button for the 'Not Applicable).  This is a good indicator that the control is not the best place to start.  Normally when I look into this kind of thing myself I try to look up the inheritance tree and find the best place to start.  I then draw from the descendants to put the behavior I want into a new child control.
Not really, it does accept different "formats" and I was able to force it to use a value that wasn't a date. I had to make sure, on the data storage end, that I stored a valid date, though.

I think I know where the old source is for this so I'll look tomorrow.