Back to Devexpress

How to: Display Appointments in Military Time

windowsforms-4820-controls-and-libraries-scheduler-examples-date-and-time-how-to-display-appointments-in-military-time.md

latest4.6 KB
Original Source

How to: Display Appointments in Military Time

  • Jan 23, 2019
  • 2 minutes to read

This document illustrates the use of AppointmentFormatStringService to change the display format of appointment’s Appointment.Start and Appointment.End values. The time will be expressed in military style, that is, using a unique two-digit number to identify each of the 24 hours in a day, leading zero and no spaces between hours and minutes.

To accomplish this, substitute the service with a custom service, containing method overrides. The list of methods which can be overridden is found in the Formatting Services topic.

The following code implements a descendant class which displays military time and replaces the required service with a custom descendant:

csharp
IAppointmentFormatStringService oldService = scheduler.GetService<IAppointmentFormatStringService>();
if (oldService != null)
{
    MyAppointmentFormatStringService newService = new MyAppointmentFormatStringService(oldService);
    scheduler.RemoveService(typeof(IAppointmentFormatStringService));
    scheduler.AddService(typeof(IAppointmentFormatStringService), newService);
}
scheduler.ActiveView.LayoutChanged();
vb
Dim oldService As IAppointmentFormatStringService = scheduler.GetService(Of IAppointmentFormatStringService)()
If oldService IsNot Nothing Then
    Dim newService As New MyAppointmentFormatStringService(oldService)
    scheduler.RemoveService(GetType(IAppointmentFormatStringService))
    scheduler.AddService(GetType(IAppointmentFormatStringService), newService)
End If
scheduler.ActiveView.LayoutChanged()
csharp
public class MyAppointmentFormatStringService : AppointmentFormatStringServiceWrapper
{
    public MyAppointmentFormatStringService(IAppointmentFormatStringService service)
        : base(service) { }
    public override string GetVerticalAppointmentStartFormat(
        IAppointmentViewInfo aptViewInfo)
    {
        return "{0: HHmm:ss - 'VerticalAppointmentStart'}";
    }
    public override string GetVerticalAppointmentEndFormat(
        IAppointmentViewInfo aptViewInfo)
    {
        return "{0: HHmm:ss - 'VerticalAppointmentEnd'}";
    }
    public override string GetHorizontalAppointmentEndFormat(
        IAppointmentViewInfo aptViewInfo)
    {
        return "{0: HHmm - 'HorizontalAppointmentEnd'}";
    }
    public override string GetHorizontalAppointmentStartFormat(
        IAppointmentViewInfo aptViewInfo)
    {
        return "{0: HHmm - 'HorizontalAppointmentStart'}";
    }
    public override string GetContinueItemStartFormat(IAppointmentViewInfo aptViewInfo)
    {
        return "{0: HHmm MMM dd - 'ContinueItemStart'}";
    }
    public override string GetContinueItemEndFormat(IAppointmentViewInfo aptViewInfo)
    {
        return "{0: HHmm MMM dd - 'ContinueItemEnd'}";
    }
}
vb
Public Class MyAppointmentFormatStringService
    Inherits AppointmentFormatStringServiceWrapper

    Public Sub New(ByVal service As IAppointmentFormatStringService)
        MyBase.New(service)
    End Sub
    Public Overrides Function GetVerticalAppointmentStartFormat(ByVal aptViewInfo As IAppointmentViewInfo) As String
        Return "{0: HHmm:ss - 'VerticalAppointmentStart'}"
    End Function
    Public Overrides Function GetVerticalAppointmentEndFormat(ByVal aptViewInfo As IAppointmentViewInfo) As String
        Return "{0: HHmm:ss - 'VerticalAppointmentEnd'}"
    End Function
    Public Overrides Function GetHorizontalAppointmentEndFormat(ByVal aptViewInfo As IAppointmentViewInfo) As String
        Return "{0: HHmm - 'HorizontalAppointmentEnd'}"
    End Function
    Public Overrides Function GetHorizontalAppointmentStartFormat(ByVal aptViewInfo As IAppointmentViewInfo) As String
        Return "{0: HHmm - 'HorizontalAppointmentStart'}"
    End Function
    Public Overrides Function GetContinueItemStartFormat(ByVal aptViewInfo As IAppointmentViewInfo) As String
        Return "{0: HHmm MMM dd - 'ContinueItemStart'}"
    End Function
    Public Overrides Function GetContinueItemEndFormat(ByVal aptViewInfo As IAppointmentViewInfo) As String
        Return "{0: HHmm MMM dd - 'ContinueItemEnd'}"
    End Function
End Class