Combobox Mode - Allow Entering New Values
- 7 minutes to read
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.
Note
Although a custom value is saved to the EditValue property, it is not added to the editor’s lookup data source. You can perform this task manually by handling the RepositoryItemLookUpEditBase.ProcessNewValue event.
- 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.
Note
The SearchLookUpEdit and TreeListLookUpEdit controls do not support combobox mode.
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.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/lookup-editors-combobox-mode-t378371
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Lookup_ComboboxMode {
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}