Главная страница | назад





Article #20716: Customizing the time/date display of the TDateTimePicker

Question:

The TDataTimePicker's time display defaults to showing hours, minutes, and seconds. I would prefer not to show seconds, but there doesn't seem to be a way of changing the format. Is there a way?

Answer:

The underlying API control can be customized using the DTM_SETFORMAT window message. Use the Perform message to send this message and its parameters to the control.

Example in C++:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
DateTimePicker1->Perform(DTM_SETFORMAT, NULL, "hh:mm tt");
// will yeild a display without seconds, like this: "8:30 PM"
}

Example in Object Pascal:

procedure TForm1.Button1Click(Sender: TObject);
var
s: String;
begin
s := 'hh:mm tt';
DateTimePicker1.Perform(DTM_SETFORMAT, DWord(nil) ,DWord(s));
end;

Consult the Windows API documentation here for further information on formatting strings.

Note: This technique bypasses the VCL wrapper around the date time picker by sending a message directly to the Windows control. This causes one problem. If you send a message that sets the formatting to show date elements while the Kind property is set to dtkTime (or vice versa), the date elements will display correctly, but the user may not be able to change those elements (or elements will change incorreclty). Therefore, it is suggested that you be sure the elements you display correspond to the control's Kind.

Last Modified: 21-MAR-00