Skip to main content

How to: Display TimeRulers with Different Time Formats Together

  • 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:

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;

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:

Services-TimeRulerFormatting

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

Create a Descendant Class

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
}

Create a Service

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);
}

Substitute the Service

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();
}

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