Back to Devexpress

How to: Display TimeRulers with Different Time Formats Together

windowsforms-4777-controls-and-libraries-scheduler-examples-date-and-time-how-to-display-timerulers-with-different-time-formats-together.md

latest7.8 KB
Original Source

How to: Display TimeRulers with Different Time Formats Together

  • Sep 07, 2023
  • 4 minutes to read

This document illustrates the use of TimeRulerFormatStringService to change the display format of a time ruler. What are the advantages of this approach? Well, to modify the display time format you could change the culture settings for the whole application, as shown below:

csharp
System.Globalization.CultureInfo culture = 
    new System.Globalization.CultureInfo(System.Globalization.CultureInfo.CurrentCulture.LCID);
culture.DateTimeFormat.LongTimePattern = "HH:mm:ss";
System.Threading.Thread.CurrentThread.CurrentCulture = culture;
vb
Private culture As System.Globalization.CultureInfo = _
    New System.Globalization.CultureInfo(System.Globalization.CultureInfo.CurrentCulture.LCID)
Private culture.DateTimeFormat.LongTimePattern = "HH:mm:ss"
Private System.Threading.Thread.CurrentThread.CurrentCulture = culture

But, if you need different time formats in different parts of a control, then you should definitely look at the Formatting Services.They provide the capability to implement a scenario when the time ruler with local time will display AM/PM, while another uses 24 hr time.

To accomplish this, substitute the service with a custom service, containing methods overridden as required.

The result is shown below:

Our task consists of three parts: Create a descendant class, Create a service, Substitute a service.

Create a Descendant Class

csharp
public class CustomTimeRulerFormatStringService : 
    TimeRulerFormatStringServiceWrapper {
    public CustomTimeRulerFormatStringService(ITimeRulerFormatStringService service)
        : base(service) {
    }

#region ITimeRulerFormatStringService Members
// Override methods so they return 12 hr time format for the time ruler with local time.

    public override string GetHalfDayHourFormat(TimeRuler ruler) {
        return ruler.UseClientTimeZone ? "htt:mm" :"HH:mm" ;
    }
    public override string GetHourFormat(TimeRuler ruler) {
        return ruler.UseClientTimeZone ? "htt:mm" : "HH:mm";
    }
    public override string GetHourOnlyFormat(TimeRuler ruler) {
        return ruler.UseClientTimeZone ? "htt" : "HH";
    }
    public override string GetMinutesOnlyFormat(TimeRuler ruler) {
        return "mm";
    }
    public override string GetTimeDesignatorOnlyFormat(TimeRuler ruler) {
        return ruler.UseClientTimeZone ? "tt" : "mm";
    }
#endregion
}
vb
Public Class CustomTimeRulerFormatStringService
    Inherits TimeRulerFormatStringServiceWrapper
    Public Sub New(ByVal service As ITimeRulerFormatStringService)
        MyBase.New(service)
    End Sub

#Region "ITimeRulerFormatStringService Members"
' Override methods so they return 12 hr time format for the time ruler with local time.

    Public Overrides Function GetHalfDayHourFormat(ByVal ruler As TimeRuler) As String
        If ruler.UseClientTimeZone Then
            Return "htt:mm"
        Else
            Return "HH:mm"
        End If
    End Function
    Public Overrides Function GetHourFormat(ByVal ruler As TimeRuler) As String
        If ruler.UseClientTimeZone Then
            Return "htt:mm"
        Else
            Return "HH:mm"
        End If
    End Function
    Public Overrides Function GetHourOnlyFormat(ByVal ruler As TimeRuler) As String
        If ruler.UseClientTimeZone Then
            Return "htt"
        Else
            Return "HH"
        End If
    End Function
    Public Overrides Function GetMinutesOnlyFormat(ByVal ruler As TimeRuler) As String
        Return "mm"
    End Function
    Public Overrides Function GetTimeDesignatorOnlyFormat(ByVal ruler As TimeRuler) _
As String
        If ruler.UseClientTimeZone Then
            Return "tt"
        Else
            Return "mm"
        End If
    End Function
#End Region
End Class

Create a Service

csharp
using DevExpress.XtraScheduler;
using DevExpress.XtraScheduler.Services;
// ...
private void Form1_Load(object sender, EventArgs e) {
    CreateTimeRulerFormatStringService();
}

// Define the variables to hold the default service and our custom service.
ITimeRulerFormatStringService prevTimeRulerFormatStringService;
CustomTimeRulerFormatStringService customTimeRulerFormatStringService;

public void CreateTimeRulerFormatStringService() {
    this.prevTimeRulerFormatStringService = 
    (ITimeRulerFormatStringService)schedulerControl1.GetService(
    typeof(ITimeRulerFormatStringService));
    this.customTimeRulerFormatStringService = 
        new CustomTimeRulerFormatStringService(prevTimeRulerFormatStringService);
}
vb
Imports DevExpress.XtraScheduler
Imports DevExpress.XtraScheduler.Services
' ...
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
    CreateTimeRulerFormatStringService()
End Sub

' Define the variables to hold the default service and our custom service.
Private prevTimeRulerFormatStringService As ITimeRulerFormatStringService
Private customTimeRulerFormatStringService As CustomTimeRulerFormatStringService

Public Sub CreateTimeRulerFormatStringService()
    Me.prevTimeRulerFormatStringService = CType(schedulerControl1.GetService( _
        GetType(ITimeRulerFormatStringService)), ITimeRulerFormatStringService)
    Me.customTimeRulerFormatStringService = _
        New CustomTimeRulerFormatStringService( prevTimeRulerFormatStringService)
End Sub

Substitute the Service

csharp
private void button1_Click(object sender, EventArgs e) {
    // Substitute the service.
    schedulerControl1.RemoveService(typeof(ITimeRulerFormatStringService));
    schedulerControl1.AddService(typeof(ITimeRulerFormatStringService), 
        customTimeRulerFormatStringService);

    // Initiate a control refresh.
    schedulerControl1.ActiveView.LayoutChanged();
}

private void button2_Click(object sender, EventArgs e) {
    // Substitute the service.
    schedulerControl1.RemoveService(typeof(ITimeRulerFormatStringService));
    schedulerControl1.AddService(typeof(ITimeRulerFormatStringService), 
        prevTimeRulerFormatStringService);

    // Initiate a control refresh.
    schedulerControl1.ActiveView.LayoutChanged();
}
vb
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    ' Substitute the service.
    schedulerControl1.RemoveService(GetType(ITimeRulerFormatStringService))
    schedulerControl1.AddService(GetType(ITimeRulerFormatStringService), _
        customTimeRulerFormatStringService)

    ' Initiate a control refresh.
    schedulerControl1.ActiveView.LayoutChanged()
End Sub

Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
    ' Substitute the service.
    schedulerControl1.RemoveService(GetType(ITimeRulerFormatStringService))
    schedulerControl1.AddService(GetType(ITimeRulerFormatStringService), _
        prevTimeRulerFormatStringService)

    ' Initiate a control refresh.
    schedulerControl1.ActiveView.LayoutChanged()
End Sub

Tip

A complete sample project is available in the DevExpress Code Examples database at https://supportcenter.devexpress.com/ticket/details/e507/winforms-scheduler-formatting-services.

See Also

Services

Formatting Services