Using Datetimepicker with Time

Asked

Viewed 488 times

1

I’m using the Tdatetimepicker component.

I put in the FORMAT property the following value: dd/MM/yyyy HH:mm:ss

Running in form if not "click" on component and change values, example

21/09/2018 14:55:56 para 21/10/2018 20:55:56

when capturing (Datetimepicker2.Datetime) the value I have 21/10/2018 14:55:56 for some reason the Time "HH:mm:ss" component does not update.

I found on the Internet the code, to put in the Onchange of the Datetimepicker:

var
 lEdit: TCustomEdit;
begin
 TDateTimePicker(Sender).DateTime := StrToDateTime(TCustomEdit(Sender));

But I have the following error when compiling:

E2250 There is no overloaded version of 'StrToDateTime' that can be called with these arguments

3 answers

1


Drop a TDateTimePicker and a TEdit in the form and then write the event handlers as follows:

type
  TForm1 = class(TForm)
    DateTimePicker1: TDateTimePicker;
    Edit1: TEdit;
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    DateTimePicker1.DateTime:= StrToDateTime(Edit1.Text);
    // Você também pode usar TryStrToDateTime()
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  DateTimePicker1.Format:= 'dd / MM / yyyy HH: mm: ss';
end;

end.

To get the error, the function StrToDateTime () expecting a String while you pass a TCustomEdit. You can write if the Sender for TEdit

StrToDateTime(TEdit(Sender).Text);

0

It worked, just needed to put the .Text:

TDateTimePicker(Sender).DateTime := StrToDateTime(TCustomEdit(Sender).Text);

-1

Using the Onchange method it is very quiet to update the time

 procedure TForm1.DateTimePicker1Change(Sender: TObject);
 begin
   DateTimePicker1.Time := Now;
 end;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.