Back to Devexpress

HumanReadableTimeSpanHelper.ParseString Event

corelibraries-devexpress-dot-xtrascheduler-dot-native-dot-humanreadabletimespanhelper-d7cef37c.md

latest5.3 KB
Original Source

HumanReadableTimeSpanHelper.ParseString Event

Occurs before the specified string is converted to a TimeSpan value.

Namespace : DevExpress.XtraScheduler.Native

Assembly : DevExpress.XtraScheduler.v25.2.Core.Desktop.dll

NuGet Package : DevExpress.Scheduler.CoreDesktop

Declaration

csharp
public static event TimeSpanStringConvertEventHandler ParseString
vb
Public Shared Event ParseString As TimeSpanStringConvertEventHandler

Event Data

The ParseString event's data class is TimeSpanStringConvertEventArgs. The following properties provide information specific to this event:

PropertyDescription
HandledGets or sets whether an event was handled, if it was handled the default actions are not required.
StringValueGets or sets the string used when converting a TimeSpan value to a string or vice versa.
TimeSpanValueGets or sets the TimeSpan value used when converting a TimeSpan value to a string or vice versa.

Remarks

Use the HumanReadableTimeSpanHelper.Parse method to convert a string to the TimeSpan value. Handle the ParseString event to implement a custom conversion procedure.

Example

This example demonstrates how to use the HumanReadableTimeSpanHelper instance to implement the custom conversion of a TimeSpan value to a string and vice versa. To do this handle the HumanReadableTimeSpanHelper.ParseString and HumanReadableTimeSpanHelper.ConvertToString events and use the members of the corresponding TimeSpanStringConvertEventArgs object.

csharp
using DevExpress.XtraScheduler.Native;
// ...

// Handle the ParseString and ConvertToString events of the HumanReadableTimeSpanHelper.
HumanReadableTimeSpanHelper.ParseString += new TimeSpanStringConvertEventHandler(OnParse);
HumanReadableTimeSpanHelper.ConvertToString += new TimeSpanStringConvertEventHandler(OnToString);

// Custom parsing of a string to a TimeSpan value.
void OnParse(object sender, TimeSpanStringConvertEventArgs e) {
   double val;
   try {
         string number = e.StringValue.Substring(0, e.StringValue.IndexOf(" milliseconds"));
         val = Convert.ToDouble(number);
   }
   catch {
         val = 0;
   }

   e.TimeSpanValue = TimeSpan.FromMilliseconds(val);
   e.Handled = true;
}

// Custom conversion of a TimeSpan value to a string.
void OnToString(object sender, TimeSpanStringConvertEventArgs e) {
   e.StringValue = e.TimeSpanValue.TotalMilliseconds.ToString() + " milliseconds";
   e.Handled = true;
}
vb
Imports DevExpress.XtraScheduler.Native
' ...

' Handle the ParseString and ConvertToString events of the HumanReadableTimeSpanHelper.
AddHandler HumanReadableTimeSpanHelper.ParseString, AddressOf OnParse
AddHandler HumanReadableTimeSpanHelper.ConvertToString, AddressOf OnToString

' Custom parsing of a string to a TimeSpan value.
Sub OnParse(sender As Object, e As TimeSpanStringConvertEventArgs)
   Dim val As Double
   Try
      Dim number As String = e.StringValue.Substring(0, e.StringValue.IndexOf(" milliseconds"))
      val = Convert.ToDouble(number)
   Catch
      val = 0
   End Try

   e.TimeSpanValue = TimeSpan.FromMilliseconds(val)
   e.Handled = True
End Sub

' Custom conversion of a TimeSpan value to a string.
Sub OnToString(sender As Object, e As TimeSpanStringConvertEventArgs)
   e.StringValue = e.TimeSpanValue.TotalMilliseconds.ToString() + " milliseconds"
   e.Handled = True
End Sub

See Also

ConvertToString

HumanReadableTimeSpanHelper Class

HumanReadableTimeSpanHelper Members

DevExpress.XtraScheduler.Native Namespace