4

for example

MyDateEdit.MinDate := DateOf(Now);

I'm using Delphi XE7 Update 1 and the help file seems to not mention any property or method to do this

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
user2720638
  • 41
  • 1
  • 3

1 Answers1

5

There is no property that I'm aware of, but you can enforce this using the OnChange event of your MyDateEdit control.

procedure TForm1.MyDateEditChange(Sender: TObject);
begin
  with Sender as TDateEdit do
  begin
    if Date < Now then
      Date := Now;
    end;
  end;

To clarify based off @SilverWarior 's comment below: This will not allow any date prior to your minimum date to exist in your control at any time. If you want a user to enter a date by keyboard entry (and during their entry the date may fall below your minimum), it may be best to use the OnExit event instead of the OnChange event.

Dale M
  • 833
  • 16
  • 21
  • 3
    I haven't tried this but I belive your code could be potentionally dangerous. Why? I asume that the OnChange event would fire after every character entered into the component if you decide to go and enter the date through the keyboard. Right? If it is so it means that in next scenario your code will actually prevent user from inputing the correctvalid date. Let us say that todays date is 25.03.2015 and you want to enter the date of 24.04.2015 which is valid. But in order to do so you will start typing in the day part first.... – SilverWarior Mar 25 '15 at 17:48
  • 2
    ... So ass son as you would press 2 Key the day part would be set to 2 and the date would be recognized as 02.03.2015 which would be considered as invalid and quickly changed in todays date. This change could happen so quick that to the user it would seem that the control doesen't even accepty any keyboard input. So perhaps it would be best to use OnExit event which would validate the date when the focus of the TDateEdit controll is lost. Perhaps you would even want to make use of KeyPress event to detect the pressing of Enter key to validate the date without leaving the control. – SilverWarior Mar 25 '15 at 17:53
  • 1
    And one other thing. When you are working with events it is best to make use of Sender which references to the component or object that fired this event. Main advantage of this is that you can then assign one event method to multiple components and the event method will still proces the data for that specific component. – SilverWarior Mar 25 '15 at 17:55
  • 1
    @SilverWarior, TDateEdit uses a date picker and doesn't allow direct editing of the date as text. I tested the answer given and it works well. – Mike Sutton Mar 25 '15 at 20:16
  • 1
    @SilverWarior Thanks for the comments. I agree with using Sender in events and have updated my answer to reflect that. I've also clarified that the OnChange will not allow a date below minimum to exist at any time, and using OnExit is an alternative. – Dale M Mar 25 '15 at 23:25
  • @MikeSutton Not according to the documentation. QUOTE from documentation: `Represents a single-line editable text box containing a date. When you click or tap the TDateEdit control, it displays a date picker that allows you to select a date.` – SilverWarior Mar 26 '15 at 06:53
  • @Mike TDateEdit is editable if `CanFocus` is `True`, which it is as default. SilverWarriors comment is correct. – Tom Brunberg Mar 26 '15 at 22:51
  • @TomBrunberg, the functionality SilverWarrior is describing is not the behaviour I'm seeing in the actual control. It does not (in my tests) allow typing in a date. Date editing is either via the date picker or by left/right/up/down keys to edit day/month/year values. The values are always validated. Also pasting in text the control only accepts valid dates. – Mike Sutton Mar 28 '15 at 18:23
  • I would argue, however, that there are better ways to validate a form then by 'secretly' changing the value of a control and hoping the user notices. – Mike Sutton Mar 28 '15 at 18:25
  • @Mike Do you have the discussed OnChange handler active? In a XE7 FMX Windows project I dropped a TDateEdit, with no changes to any properties (`CanFocus` is true by default). I can click on, say, day and type valid day numbers, use up/down arrow keys to increase/decrease or use the dropdown calendar. Adding the discussed OnChange event, I can not edit (by typing) the day because event handler prevents it, unless I first increase the month. Then, of course, the OnChange event doesn't prevent typing whatever valid day number. – Tom Brunberg Mar 28 '15 at 19:13
  • @TomBrunberg Ahh, I see now. It only accepts text input for valid dates, not the random stuff I was typing. – Mike Sutton Mar 29 '15 at 12:32