| |
 |
Combobox Mode - Allow Entering New Values
In Combobox Mode, a LookUpEdit and GridLookUpEdit controls can act as a standard Windows Forms ComboBox control.
- The editor's dropdown displays records from a lookup data source.
- An end-user can enter any value in the edit box, including values that do not exist in the lookup data source.

To enable combobox mode, use the following properties.
-
the RepositoryItemLookUpEdit.AcceptEditorTextAsNewValue/RepositoryItemGridLookUpEdit.AcceptEditorTextAsNewValue properties should be set to a value that allows custom text to be entered in the edit box and accepted by the editor (saved to the editor's EditValue property).
The AcceptEditorTextAsNewValue properties are initially set to Default, which means True for the LookUpEdit control, and False for the GridLookUpEdit control. Thus, if you use the latter control, set its AcceptEditorTextAsNewValue property to True.
A custom value entered in the edit box is posted (saved to the editor's EditValue property) in certain cases (e.g., when the editor loses focus). If the AcceptEditorTextAsNewValue property is set to a value that disables this feature, entered custom values are discarded.
- Set the RepositoryItemLookUpEditBase.TextEditStyle property to Standard to enable text editing in the edit box.
- If the editor's lookup data source is an array of simple values (e.g., an array of strings), ensure that the RepositoryItemLookUpEditBase.ValueMember and RepositoryItemLookUpEditBase.DisplayMember properties are set to an empty string.
- If records in the editor's lookup data source have multiple fields, set the RepositoryItemLookUpEditBase.ValueMember and RepositoryItemLookUpEditBase.DisplayMember properties to the same field. Typically, you should set these properties to a field whose values need to be displayed in the edit box.

Example
This example shows the use of LookupEdit and GridLookupEdit controls in combobox mode, in which the editors accept any text in the edit box. An end-user can select an existing value from a lookup data source or type any string. The text entered is saved in the editor's edit value when the editor loses focus.
Lookup data sources for the LookupEdit and GridLookupEdit controls are an array of strings and a list of business objects, respectively.
Combobox mode is enabled when the following conditions are met:
-
the AcceptEditorTextAsNewValue property enables entering custom text in the edit box.
-
the ValueMember and DisplayMember properties are set to an empty string (see the LookupEdit control initialization), or to the same field in the lookup data source (see the GridLookupEdit control initialization).
-
the TextEditStyle property is set to Standard to enable text editing.

Program.cs |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Lookup_ComboboxMode {
static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
|
Form1.cs |
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Lookup_ComboboxMode {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
initLookupEdit();
initGridLookupEdit();
}
void initLookupEdit() {
lookUpEdit1.EditValueChanged += LookUpEdit1_EditValueChanged; ;
lookUpEdit1.Properties.NullText = "(select or type value)";
string[] colors = new string[] {
"Yellow", "Red", "Green", "Black", "White"
};
lookUpEdit1.Properties.DataSource = colors;
lookUpEdit1.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
lookUpEdit1.Properties.AcceptEditorTextAsNewValue = DevExpress.Utils.DefaultBoolean.Default;
}
void initGridLookupEdit() {
gridLookUpEdit1.EditValueChanged += LookUpEdit1_EditValueChanged;
gridLookUpEdit1.Properties.NullText = "(select or type value)";
List<Product> products = new List<Product> {
new Product(){ ProductName="Chang" },
new Product(){ ProductName="Ipoh Coffee" },
new Product(){ ProductName="Ravioli Angelo" },
new Product(){ ProductName="Filo Mix" },
new Product(){ ProductName="Tunnbröd" },
new Product(){ ProductName="Konbu" },
new Product(){ ProductName="Boston Crab Meat" }
};
gridLookUpEdit1.Properties.DataSource = products;
gridLookUpEdit1.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
gridLookUpEdit1.Properties.AcceptEditorTextAsNewValue = DevExpress.Utils.DefaultBoolean.True;
gridLookUpEdit1.Properties.ValueMember = "ProductName";
gridLookUpEdit1.Properties.DisplayMember = gridLookUpEdit1.Properties.ValueMember;
gridLookUpEdit1.ProcessNewValue += GridLookUpEdit1_ProcessNewValue;
}
Dictionary<LookUpEditBase, LabelControl> labelDictionaryCore;
Dictionary<LookUpEditBase, LabelControl> labelDictionary {
get {
if (labelDictionaryCore == null) {
labelDictionaryCore = new Dictionary<LookUpEditBase, LabelControl>();
labelDictionaryCore.Add(lookUpEdit1, labelControl1);
labelDictionaryCore.Add(gridLookUpEdit1, labelControl2);
}
return labelDictionaryCore;
}
}
private void LookUpEdit1_EditValueChanged(object sender, EventArgs e) {
LookUpEditBase lookupEditor = sender as LookUpEditBase;
if (lookupEditor == null) return;
LabelControl label = labelDictionary[lookupEditor];
if (label == null) return;
if (lookupEditor.EditValue == null)
label.Text = "Current EditValue: null";
else
label.Text = "Current EditValue: " + lookupEditor.EditValue.ToString();
}
private void GridLookUpEdit1_ProcessNewValue(object sender, DevExpress.XtraEditors.Controls.ProcessNewValueEventArgs e) {
GridLookUpEdit gridLookup = sender as GridLookUpEdit;
if (e.DisplayValue == null) return;
string newValue = e.DisplayValue.ToString();
if (newValue == String.Empty) return;
if (MessageBox.Show(this, "Add '" + newValue + "' to list?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes) {
List<Product> ds = gridLookup.Properties.DataSource as List<Product>;
ds.Add(new Product { ProductName = newValue });
e.Handled = true;
}
}
}
public class Product {
public string ProductName { get; set; }
}
}
|
Program.vb |
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Threading.Tasks
Imports System.Windows.Forms
Namespace Lookup_ComboboxMode
Friend NotInheritable Class Program
Private Sub New()
End Sub
''' <summary>
''' The main entry point for the application.
''' </summary>
<STAThread> _
Shared Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New Form1())
End Sub
End Class
End Namespace
|
Form1.vb |
Imports DevExpress.XtraEditors
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms
Namespace Lookup_ComboboxMode
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
initLookupEdit()
initGridLookupEdit()
End Sub
Private Sub initLookupEdit()
AddHandler lookUpEdit1.EditValueChanged, AddressOf LookUpEdit1_EditValueChanged
lookUpEdit1.Properties.NullText = "(select or type value)"
Dim colors() As String = { "Yellow", "Red", "Green", "Black", "White" }
lookUpEdit1.Properties.DataSource = colors
lookUpEdit1.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard
lookUpEdit1.Properties.AcceptEditorTextAsNewValue = DevExpress.Utils.DefaultBoolean.Default 'Default is equivalent to True for LookupEdit control
End Sub
Private Sub initGridLookupEdit()
AddHandler gridLookUpEdit1.EditValueChanged, AddressOf LookUpEdit1_EditValueChanged
gridLookUpEdit1.Properties.NullText = "(select or type value)"
Dim products As New List(Of Product) From { _
New Product() With {.ProductName="Chang"}, _
New Product() With {.ProductName="Ipoh Coffee"}, _
New Product() With {.ProductName="Ravioli Angelo"}, _
New Product() With {.ProductName="Filo Mix"}, _
New Product() With {.ProductName="Tunnbröd"}, _
New Product() With {.ProductName="Konbu"}, _
New Product() With {.ProductName="Boston Crab Meat"} _
}
gridLookUpEdit1.Properties.DataSource = products
gridLookUpEdit1.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard
gridLookUpEdit1.Properties.AcceptEditorTextAsNewValue = DevExpress.Utils.DefaultBoolean.True
gridLookUpEdit1.Properties.ValueMember = "ProductName"
gridLookUpEdit1.Properties.DisplayMember = gridLookUpEdit1.Properties.ValueMember
AddHandler gridLookUpEdit1.ProcessNewValue, AddressOf GridLookUpEdit1_ProcessNewValue
End Sub
Private labelDictionaryCore As Dictionary(Of LookUpEditBase, LabelControl)
Private ReadOnly Property labelDictionary() As Dictionary(Of LookUpEditBase, LabelControl)
Get
If labelDictionaryCore Is Nothing Then
labelDictionaryCore = New Dictionary(Of LookUpEditBase, LabelControl)()
labelDictionaryCore.Add(lookUpEdit1, labelControl1)
labelDictionaryCore.Add(gridLookUpEdit1, labelControl2)
End If
Return labelDictionaryCore
End Get
End Property
Private Sub LookUpEdit1_EditValueChanged(ByVal sender As Object, ByVal e As EventArgs)
'Display lookup editor's current value.
Dim lookupEditor As LookUpEditBase = TryCast(sender, LookUpEditBase)
If lookupEditor Is Nothing Then
Return
End If
Dim label As LabelControl = labelDictionary(lookupEditor)
If label Is Nothing Then
Return
End If
If lookupEditor.EditValue Is Nothing Then
label.Text = "Current EditValue: null"
Else
label.Text = "Current EditValue: " & lookupEditor.EditValue.ToString()
End If
End Sub
Private Sub GridLookUpEdit1_ProcessNewValue(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ProcessNewValueEventArgs)
'Add new values to GridLookUpEdit control's DataSource.
Dim gridLookup As GridLookUpEdit = TryCast(sender, GridLookUpEdit)
If e.DisplayValue Is Nothing Then
Return
End If
Dim newValue As String = e.DisplayValue.ToString()
If newValue = String.Empty Then
Return
End If
If MessageBox.Show(Me, "Add '" & newValue & "' to list?", "Confirm", MessageBoxButtons.YesNo) = System.Windows.Forms.DialogResult.Yes Then
Dim ds As List(Of Product) = TryCast(gridLookup.Properties.DataSource, List(Of Product))
ds.Add(New Product With {.ProductName = newValue})
e.Handled = True
End If
End Sub
End Class
Public Class Product
Public Property ProductName() As String
End Class
End Namespace
|
Is this topic helpful?
Additional Feedback
Close
|