Combobox Mode - Allow Entering New Values
- 6 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.
Set up the properties below as follows to enable combobox mode:
Set the RepositoryItemLookUpEdit.AcceptEditorTextAsNewValue/RepositoryItemGridLookUpEdit.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).
Note
Although a custom value is saved to the EditValue property, the value (a new row) is not automatically added to the editor’s lookup data source. Handle the RepositoryItemLookUpEditBase.ProcessNewValue event to add a custom value to the data source.
When an inplace lookup editor loses focus, the editor’s display text is cleared if a custom value is not added to the data source.
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.
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());
}
}
}