Back to Devexpress

BaseEdit.CustomDisplayText Event

windowsforms-devexpress-dot-xtraeditors-dot-baseedit-5a6cf7fc.md

latest7.2 KB
Original Source

BaseEdit.CustomDisplayText Event

Enables custom display text to be provided for an editor.

Namespace : DevExpress.XtraEditors

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Events")]
public event CustomDisplayTextEventHandler CustomDisplayText
vb
<DXCategory("Events")>
Public Event CustomDisplayText As CustomDisplayTextEventHandler

Event Data

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

PropertyDescription
DisplayTextGets or sets an editor’s display text.
ValueGets an editor’s current value.

Remarks

The CustomDisplayText event can be used to provide custom display text for an editor. To provide custom text, assign it to the event’s DisplayText parameter.

The editor’s CustomDisplayText event is equivalent to the RepositoryItem.CustomDisplayText event available via the BaseEdit.Properties object. See RepositoryItem.CustomDisplayText for more details.

For ImageComboBoxEdit editors, the CustomDisplayText event only fires when the editor has no selected value (the ComboBoxEdit.SelectedIndex equals -1). When a value is selected, the display text is specified by the selected item’s ImageComboBoxItem.Description property.

Example

The code below shows how to provide custom display text for a ProgressBarControl by handling the BaseEdit.CustomDisplayText event.

In the example, the ProgressBarControl displays a countdown timer.

csharp
using DevExpress.XtraEditors;

System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

private void Form1_Load(object sender, EventArgs e) {
    progressBarControl1.Properties.ShowTitle = true;
    progressBarControl1.Properties.PercentView = false;
    progressBarControl1.CustomDisplayText += ProgressBarControl1_CustomDisplayText;
    timer.Tick += timer1_Tick;
}

private void btnStartTimer_Click(object sender, EventArgs e) {
    timer.Enabled = false;
    int totalSeconds = 5;
    DateTime startTime = DateTime.Now;
    progressBarControl1.Tag = startTime;
    progressBarControl1.Properties.Minimum = 0;
    progressBarControl1.Properties.Maximum = totalSeconds;
    progressBarControl1.Position = progressBarControl1.Properties.Minimum;
    timer.Enabled = true;
}

private void ProgressBarControl1_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e) {
    ProgressBarControl pb = sender as ProgressBarControl;
    if (pb.Tag == null) { e.DisplayText = "Stopped"; return; }
    int currentPosition = Convert.ToInt32(e.Value);
    int maximum = pb.Properties.Maximum;
    int secondsLeft = maximum - currentPosition;
    TimeSpan timeLeft = new TimeSpan(0, 0, secondsLeft);
    string s = string.Format("{0:dd} days {0:hh} hours {0:mm} minutes {0:ss} seconds", timeLeft);
    e.DisplayText = s;
}

private void timer1_Tick(object sender, EventArgs e) {
    ProgressBarControl pb = progressBarControl1;
    if (pb.Tag == null || !(pb.Tag is DateTime)) return;
    DateTime startTime = (DateTime)pb.Tag;
    DateTime currentTime = DateTime.Now;
    int elapsedSeconds = Convert.ToInt32((currentTime - startTime).TotalSeconds);
    int totalSeconds = pb.Properties.Maximum;
    if (elapsedSeconds >= totalSeconds) {
        (sender as System.Windows.Forms.Timer).Enabled = false;
        pb.Tag = null;
        BeginInvoke(new MethodInvoker(delegate
        {
            MessageBox.Show("Stop");
        }));
    }
    pb.Position = elapsedSeconds;
}
vb
Imports DevExpress.XtraEditors

Public Sub New()
    InitializeComponent()

    ProgressBarControl1.Properties.ShowTitle = True
    ProgressBarControl1.Properties.PercentView = False
    AddHandler ProgressBarControl1.CustomDisplayText, AddressOf ProgressBarControl1_CustomDisplayText
    AddHandler timer.Tick, AddressOf timer1_Tick
End Sub

Dim timer As System.Windows.Forms.Timer = New Timer()

Private Sub buttonStartTimer_Click(sender As Object, e As EventArgs) Handles buttonStartTimer.Click
    timer.Enabled = False
    Dim totalSeconds As Integer = 5
    Dim startTime As DateTime = DateTime.Now
    ProgressBarControl1.Tag = startTime
    ProgressBarControl1.Properties.Minimum = 0
    ProgressBarControl1.Properties.Maximum = totalSeconds
    ProgressBarControl1.Position = ProgressBarControl1.Properties.Minimum
    timer.Enabled = True
End Sub

Private Sub ProgressBarControl1_CustomDisplayText(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs)
    Dim pb As ProgressBarControl = TryCast(sender, ProgressBarControl)
    If pb.Tag Is Nothing Then
        e.DisplayText = "Stopped"
        Return
    End If
    Dim currentPosition As Integer = Convert.ToInt32(e.Value)
    Dim maximum As Integer = pb.Properties.Maximum
    Dim secondsLeft As Integer = maximum - currentPosition
    Dim timeLeft As TimeSpan = New TimeSpan(0, 0, secondsLeft)
    Dim s As String = String.Format("{0:dd} days {0:hh} hours {0:mm} minutes {0:ss} seconds", timeLeft)
    e.DisplayText = s
End Sub

Private Sub timer1_Tick(ByVal sender As Object, ByVal e As EventArgs)
    Dim pb As ProgressBarControl = ProgressBarControl1
    If pb.Tag Is Nothing OrElse Not (TypeOf pb.Tag Is DateTime) Then Return
    Dim startTime As DateTime = CType(pb.Tag, DateTime)
    Dim currentTime As DateTime = DateTime.Now
    Dim elapsedSeconds As Integer = Convert.ToInt32((currentTime - startTime).TotalSeconds)
    Dim totalSeconds As Integer = pb.Properties.Maximum
    If elapsedSeconds >= totalSeconds Then
        CType(sender, System.Windows.Forms.Timer).Enabled = False
        pb.Tag = Nothing
        BeginInvoke(New MethodInvoker(Sub()
                                            MessageBox.Show("Stop")
                                        End Sub))
    End If
    pb.Position = elapsedSeconds
End Sub

See Also

CustomDisplayText

BaseEdit Class

BaseEdit Members

DevExpress.XtraEditors Namespace