Back to Devexpress

RepositoryItem.CustomDisplayText Event

windowsforms-devexpress-dot-xtraeditors-dot-repository-dot-repositoryitem-b25360e8.md

latest11.2 KB
Original Source

RepositoryItem.CustomDisplayText Event

Enables custom display text to be provided for an editor.

Namespace : DevExpress.XtraEditors.Repository

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

Important

If an editor’s TextEditStyle property is not DisableTextEditor , the CustomDisplayText event fires only when this editor is not focused. Editors with disabled text editors fire this event with no limitations.

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. Initially, this property contains the current display text, which is a formatted representation of the editor’s edit value. To access the editor’s edit value, use the Value parameter.

For ImageComboBoxEdit editors, the CustomDisplayText event only fires when no value is selected in the editor (the ComboBoxEdit.SelectedIndex is set to -1 ). When a value is selected, the CustomDisplayText event doesn’t fire. In this instance, the display text is specified by the selected item’s ImageComboBoxItem.Description property.

The CustomDisplayText event also fires for cells in a container control (e.g GridControl, etc) if a repository item object is assigned to the control’s column/cell for in-place editing. In display mode (when an in-place editor is not active), the CustomDisplayText event fires with the sender parameter, specifying a corresponding repository item object (the RepositoryItem class’ descendant). When an in-place editor is activated, the CustomDisplayText event doesn’t fire. However, it’s possible to disable the text editing feature via the RepositoryItemButtonEdit.TextEditStyle property. In this instance, when the editor is activated, the CustomDisplayText event fires with the sender parameter specifying a corresponding editor object (the BaseEdit class’ descendant).

After your CustomDisplayText event handler is complete, the display text may be modified according to the RepositoryItemTextEdit.CharacterCasing option.

The BaseEdit.CustomDisplayText event is equivalent to the current event.

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

The following code snippets (auto-collected from DevExpress Examples) contain references to the CustomDisplayText event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

xaf-how-to-display-an-enumeration-property-as-a-drop-down-box-with-check-boxes/CS/EFCore/EnumCheckBoxEF/EnumCheckBoxEF.Win/Editors/EnumPropertyEditorEx.cs#L51

csharp
checkedItem.ParseEditValue += checkedEdit_ParseEditValue;
checkedItem.CustomDisplayText += checkedItem_CustomDisplayText;
checkedItem.Disposed += checkedItem_Disposed;

winforms-lookup-display-custom-text-for-not-found-edit-value/CS/Form1.cs#L35

csharp
editor.DisplayMember = "Name";
editor.CustomDisplayText += new CustomDisplayTextEventHandler(RepositoryItemLookUpEdit_CustomDisplayText);

winforms-lookup-display-custom-text-for-not-found-edit-value/VB/Form1.vb#L36

vb
editor.DisplayMember = "Name"
AddHandler editor.CustomDisplayText, New CustomDisplayTextEventHandler(AddressOf RepositoryItemLookUpEdit_CustomDisplayText)
gridView1.Columns(0).ColumnEdit = editor

See Also

SelectedIndex

EditValue

Text

DisplayFormat

RepositoryItem Class

RepositoryItem Members

DevExpress.XtraEditors.Repository Namespace