Back to Devexpress

RepositoryItemLookUpEditBase.ProcessNewValue Event

windowsforms-devexpress-dot-xtraeditors-dot-repository-dot-repositoryitemlookupeditbase.md

latest10.1 KB
Original Source

RepositoryItemLookUpEditBase.ProcessNewValue Event

Occurs when a new value entered into the edit box is validated.

Namespace : DevExpress.XtraEditors.Repository

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

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

Event Data

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

PropertyDescription
DisplayValueGets or sets the value entered by the end-user.
HandledGets or sets a value specifying whether the lookup editor must locate the newly inserted record.

Remarks

End-users can type new values into a lookup editor provided that the RepositoryItemLookUpEditBase.TextEditStyle property is set to TextEditStyles.Standard. When an end-user presses the Enter key or moves focus to another control, the editor searches for the row whose RepositoryItemLookUpEditBase.DisplayMember field value matches the typed text. If such a row is not found, the ProcessNewValue event is fired.

If a record matching the typed text is found, the event does not occur.

You can handle the ProcessNewValue event to add a new record representing the new text. If you add a record, you need to set the ProcessNewValueEventArgs.Handled property of the event parameter to true. This will force the editor to locate and select the newly inserted record. Set the EditValue property manually if the DisplayMember property is not specified. In this instance, the e.Handled parameter is ignored.

Note

If the type of the data field specified by the RepositoryItemLookUpEditBase.DisplayMember property is not convertable from the String type, the LookUpEdit will not be able to locate the new record using the text inserted by the user. To overcome this issue, manually set the event’s e.DisplayValue parameter to the new value stored in the RepositoryItemLookUpEditBase.DisplayMember field of the newly added record.

If you do not want to add a new record or you do not want the LookUpEdit to locate your new record, leave the ProcessNewValueEventArgs.Handled parameter set to false.

Note

The ProcessNewValue event may not function properly if the underlying data source does not support change notifications (specifically, if it does not provide the ListChanged event).

The editor’s LookUpEditBase.ProcessNewValue event is equivalent to the current event.

Example

This example demonstrates how to allow users to type in the text box and add new values to the lookup’s data source.

  • Drop the WinForms LookUpEdit control onto a form.
  • Set the lookup’s TextEditStyle property to Standard to allow users to type in the text box.
  • Handle the ProcessNewValue event to parse entered values and add new records to the lookup’s data source.

A message box is displayed before a new record is added to the data source. A new value is added to the data source after the user confirms the operation.

csharp
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

public Form1() {
    InitializeComponent();
    // Binds the lookup to data.
    lookUpEdit1.Properties.DataSource = Task.GetSampleData();
    // Sets the lookup's data fields.
    lookUpEdit1.Properties.DisplayMember = "Caption";
    lookUpEdit1.Properties.ValueMember = "ID";
    // Sets the lookup's value.
    lookUpEdit1.EditValue = 0;
    // Enables adding new values.
    lookUpEdit1.Properties.TextEditStyle = TextEditStyles.Standard;
    lookUpEdit1.ProcessNewValue += new ProcessNewValueEventHandler(this.lookUpEdit1_ProcessNewValue);
}
private void lookUpEdit1_ProcessNewValue(object sender, ProcessNewValueEventArgs e) {
    if((string)e.DisplayValue == String.Empty) return;
    if(XtraMessageBox.Show("Do you want to add a new value?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {
        List<Task> dataSource = (sender as LookUpEdit).Properties.DataSource as List<Task>;
        dataSource.Add(new Task(dataSource.Count) { Caption = (string)e.DisplayValue });
        e.Handled = true;
    }
}

public class Task {
    int fID;
    public Task(int id) {
        fID = id;
        CreateDate = DateTime.Today;
    }
    public int ID {
        get {
            return fID;
        }
    }
    public string Caption { get; set; }
    public DateTime CreateDate { get; set; }
    public static List<Task> GetSampleData() {
        return new List<Task>() {
            new Task(0){Caption = "Research", CreateDate = new DateTime(2022, 10, 15)},
            new Task(1){Caption = "UI Design", CreateDate = new DateTime(2022, 11, 5)},
            new Task(2){Caption = "Environment Setup", CreateDate = new DateTime(2022, 11, 10)},
            new Task(3){Caption = "Sprint 1", CreateDate = new DateTime(2022, 11, 11)},
            new Task(4){Caption = "Sprint 2", CreateDate = new DateTime(2022, 12, 12)},
            new Task(5){Caption = "Sprint 3", CreateDate = new DateTime(2023, 1, 10)},
            new Task(6){Caption = "Testing", CreateDate = new DateTime(2022, 2, 10)}
        };
    }
}
vb
Imports System
Imports System.Windows.Forms
Imports System.Collections.Generic
Imports DevExpress.XtraEditors
Imports DevExpress.XtraEditors.Controls

Public Sub New()
    InitializeComponent()
    ' Binds the lookup to data.
    lookUpEdit1.Properties.DataSource = Task.GetSampleData()
    ' Sets the lookup's data fields.
    lookUpEdit1.Properties.DisplayMember = "Caption"
    lookUpEdit1.Properties.ValueMember = "ID"
    ' Sets the lookup's value.
    lookUpEdit1.EditValue = 0
    ' Enables adding new values.
    lookUpEdit1.Properties.TextEditStyle = TextEditStyles.Standard
    AddHandler lookUpEdit1.ProcessNewValue, AddressOf lookUpEdit1_ProcessNewValue
End Sub
Private Sub lookUpEdit1_ProcessNewValue(ByVal sender As Object, ByVal e As ProcessNewValueEventArgs)
    If CStr(e.DisplayValue) = String.Empty Then
        Return
    End If
    If XtraMessageBox.Show("Do you want to add a new value?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
        Dim dataSource As List(Of Task) = TryCast((TryCast(sender, LookUpEdit)).Properties.DataSource, List(Of Task))
        dataSource.Add(New Task(dataSource.Count) With {.Caption = CStr(e.DisplayValue)})
        e.Handled = True
    End If
End Sub

Public Class Task
    Private fID As Integer
    Public Sub New(ByVal id As Integer)
        fID = id
        CreateDate = Date.Today
    End Sub
    Public ReadOnly Property ID() As Integer
        Get
            Return fID
        End Get
    End Property
    Public Property Caption() As String
    Public Property CreateDate() As Date
    Public Shared Function GetSampleData() As List(Of Task)
        Return New List(Of Task)() From {
            New Task(0) With {.Caption = "Research", .CreateDate = New Date(2022, 10, 15)},
            New Task(1) With {.Caption = "UI Design", .CreateDate = New Date(2022, 11, 5)},
            New Task(2) With {.Caption = "Environment Setup", .CreateDate = New Date(2022, 11, 10)},
            New Task(3) With {.Caption = "Sprint 1", .CreateDate = New Date(2022, 11, 11)},
            New Task(4) With {.Caption = "Sprint 2", .CreateDate = New Date(2022, 12, 12)},
            New Task(5) With {.Caption = "Sprint 3", .CreateDate = New Date(2023, 1, 10)},
            New Task(6) With {.Caption = "Testing", .CreateDate = New Date(2022, 2, 10)}
        }
    End Function
End Class

See Also

RepositoryItemGridLookUpEdit.AcceptEditorTextAsNewValue

RepositoryItemLookUpEdit.AcceptEditorTextAsNewValue

AddNewValue

RepositoryItemLookUpEditBase Class

RepositoryItemLookUpEditBase Members

DevExpress.XtraEditors.Repository Namespace