?
Solved

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

Posted on 2010-04-07
11
Medium Priority
?
443 Views
Last Modified: 2013-11-23
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?
0
Comment
Question by:Norm-al
  • 7
  • 3
11 Comments
 
LVL 26

Expert Comment

by:Eddie Shipman
ID: 30113795
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
0
 
LVL 21

Accepted Solution

by:
developmentguru earned 2000 total points
ID: 30114221
In the grid you can easily add a custom button that would be beside the drop down that shows the date combo.  This should be the easiest way to add the button you are looking for.  As for displaying the date as either date or possibly 'Not Applicable'... that I would have to experiment with.  

You can look in the Columns property of your grid and find the column for your date field.  There is a property in the column called ButtonStyle.  You can set this to cbsEllipsis to give the field an ellipsis field that you can respond to using OnButton Click, just be sure to check if the current column is the one you are targeting.  The date picker can be placed on a dialog that you would have full control of (just another Delphi form).  This form could be used as a drop down but this is a much more difficult process.  I would recommend at least starting with it as a dialog.  Then, if you like the results and want to make it a drop down, open a new question on how to do that.

In the field (on the dataset) there is an event called 'OnGetText'.  You can use this method to change what text will be displayed, just before it is displayed.  I would use this to spoof your 'Not Applicable' date.  Trying to use this in combination with the normal date picker may cause issues though... depends on whether or not the date is passed as a date.  Even then it would want to show it as 1/1/1900.

Let me know if you need more.
0
 
LVL 26

Expert Comment

by:Eddie Shipman
ID: 30114315
In addition, the dropdown is actually a TMonthCalendar. mullet_attack wrote some code way back in 2000 showing a fully operational TMonthCalendar replacement:
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_10321846.html

I'm sure you could modify it and then write your own TDateTimePicker to use this new calendar as the dropdown.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 26

Expert Comment

by:Eddie Shipman
ID: 30114432
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.
0
 
LVL 21

Expert Comment

by:developmentguru
ID: 30114513
The original poster did LOL
0
 
LVL 26

Expert Comment

by:Eddie Shipman
ID: 30114618
He is not editing in the grid from what I read.
0
 

Author Comment

by:Norm-al
ID: 30116583
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?
0
 
LVL 26

Expert Comment

by:Eddie Shipman
ID: 30120226
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

0
 
LVL 26

Expert Comment

by:Eddie Shipman
ID: 30120297
BTW, the code above was from a DTP descendant, kind of like the TJvDateTimepicker from JVCL.
0
 
LVL 21

Expert Comment

by:developmentguru
ID: 30236630
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.
0
 
LVL 26

Expert Comment

by:Eddie Shipman
ID: 30272353
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.
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question