Suppose a lookup editor displays a list of persons in the dropdown window. Dropdown rows are retrieved from a collection of a custom ContactList class. For this purpose, the RepositoryItemLookUpEditBase.DataSource property is set to an instance of the ContactList collection.
Each element in the collection represents an object of the Contact class.
The following LookUpEditBase.ProcessNewValue event handler adds new records to the data source. This occurs when the end-user enters a new name in the edit box that does not exist in the dropdown list and presses the Enter key. The entry is added by the Add method implemented in the ContactList class. The ProcessNewValueEventArgs.Handled parameter is set to true in order to force the editor to locate the newly inserted record after the event handler is performed.
Before inserting a new record, a message box is displayed and this allows the user to submit or discard the value. The following message box appears when a 'Jack Cooper' entry is entered:
After pressing the 'Yes' button, the new string is added to the list and the editor picks it up:
using DevExpress.XtraEditors.Controls; //ProcessNewValue event handler private void LookUpEdit1_ProcessNewValue(object sender, ProcessNewValueEventArgs e) { if ((string)e.DisplayValue != String.Empty && MessageBox.Show(this, "Add the '" + e.DisplayValue.ToString() + "' entry to the list?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes) { ((sender as LookUpEdit).Properties.DataSource as ContactList).Add( new Contact(e.DisplayValue.ToString())); e.Handled = true; } } private void Form1_Load(object sender, System.EventArgs e) { //create a contact list ContactList cList = new ContactList(); cList.Add(new Contact("John Doe")); cList.Add(new Contact("Sam Hill")); cList.Add(new Contact("Karen Holmes")); cList.Add(new Contact("Bobbie Valentine")); cList.Add(new Contact("Frank Frankson")); //bind the lookup editor to the list lookUpEdit1.Properties.DataSource = cList; lookUpEdit1.Properties.DisplayMember = "Name"; lookUpEdit1.Properties.ValueMember = "ID"; // Add columns. // The ID column is populated // via the GetNotInListValue event (not listed in the example). lookUpEdit1.Properties.Columns.Add(new LookUpColumnInfo("ID", "ID", 20)); lookUpEdit1.Properties.Columns.Add(new LookUpColumnInfo("Name", "Name", 80)); //enable text editing lookUpEdit1.Properties.TextEditStyle = TextEditStyles.Standard } public class ContactList : System.Collections.CollectionBase { public Contact this[int index] { get {return (Contact)(List[index]);} set {List[index] = value;} } public int Add(Contact value) { return List.Add(value); } //... } public class Contact { private string name; public Contact(string _name) { name = _name; } public string Name { get { return name; } set { name = value; } } public string Id { get { return Id; } set { name = value; } } }
Imports DevExpress.XtraEditors.Controls 'ProcessNewValue event handler Private Sub lookUpEdit1_ProcessNewValue(ByVal sender As Object, ByVal e As ProcessNewValueEventArgs) If CStr(e.DisplayValue) <> String.Empty AndAlso MessageBox.Show(Me, "Add the '" & e.DisplayValue.ToString() & "' entry to the list?", "Confirm", MessageBoxButtons.YesNo) = DialogResult.Yes Then TryCast((TryCast(sender, LookUpEdit)).Properties.DataSource, ContactList).Add(New Contact(e.DisplayValue.ToString())) e.Handled = True End If End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load 'create a contact list Dim cList As ContactList = New ContactList cList.Add(New Contact("John Doe")) cList.Add(New Contact("Sam Hill")) cList.Add(New Contact("Karen Holmes")) cList.Add(New Contact("Bobbie Valentine")) cList.Add(New Contact("Frank Frankson")) 'bind the lookup editor to the list LookUpEdit1.Properties.DataSource = cList LookUpEdit1.Properties.DisplayMember = "Name" LookUpEdit1.Properties.ValueMember = "ID" ' Add columns. ' The ID column is populated ' via the GetNotInListValue event (not listed in the example). LookUpEdit1.Properties.Columns.Add(New LookUpColumnInfo("ID", "ID", 20)) LookUpEdit1.Properties.Columns.Add(New LookUpColumnInfo("Name", "Name", 80)) 'enable text editing LookUpEdit1.Properties.TextEditStyle = TextEditStyles.Standard End Sub Public Class ContactList Inherits System.Collections.CollectionBase Default Public Property Item(ByVal index As Integer) As Contact Get Return CType(List(index), Contact) End Get Set(ByVal Value As Contact) List(index) = Value End Set End Property Public Function Add(ByVal value As Contact) As Integer Return List.Add(value) End Function '... End Class Public Class Contact Private name_Renamed As String Public Sub New(ByVal _name As String) name_Renamed = _name End Sub Public Property Name() As String Get Return name_Renamed End Get Set(ByVal value As String) name_Renamed = value End Set End Property Public Property Id() As String Get Return Id End Get Set(ByVal value As String) name_Renamed = value End Set End Property End Class