Back to Devexpress

RepositoryItemLookUpEdit.AcceptEditorTextAsNewValue Property

windowsforms-devexpress-dot-xtraeditors-dot-repository-dot-repositoryitemlookupedit-9039527d.md

latest14.4 KB
Original Source

RepositoryItemLookUpEdit.AcceptEditorTextAsNewValue Property

Gets or sets whether a custom value (a value that is not present in the lookup data source) that is entered in the edit box is accepted by the editor (when the value is validated and editor loses focus). This property supports ComboBox mode for the lookup editor.

Namespace : DevExpress.XtraEditors.Repository

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
[DefaultValue(DefaultBoolean.Default)]
[DXCategory("Behavior")]
public DefaultBoolean AcceptEditorTextAsNewValue { get; set; }
vb
<DXCategory("Behavior")>
<DefaultValue(DefaultBoolean.Default)>
Public Property AcceptEditorTextAsNewValue As DefaultBoolean

Property Value

TypeDefaultDescription
DefaultBooleanDefault

A value that specifies if custom values are accepted by the editor. The DefaultBoolean.Default property value is equivalent to True.

|

Available values:

NameDescriptionReturn Value
True

The value is true.

|

0

| | False |

The value is false.

|

1

| | Default |

The value is specified by a global option or a higher-level object.

|

2

|

Remarks

The AcceptEditorTextAsNewValue property supports ComboBox mode for the lookup editor. See the ComboBox Mode - Enter New Values topic to learn more.

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.

View Example

csharp
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; //Default is equivalent to True for LookupEdit control
        }

        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) {
            //Display lookup editor's current value.
            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) {
            //Add new values to GridLookUpEdit control's DataSource.
            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; }
    }

}
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

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the AcceptEditorTextAsNewValue property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-data-lookups-combobox-mode/CS/Lookup-ComboboxMode/Form1.cs#L34

csharp
lookUpEdit1.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
    lookUpEdit1.Properties.AcceptEditorTextAsNewValue = DevExpress.Utils.DefaultBoolean.Default; //Default is equivalent to True for LookupEdit control
}

winforms-data-lookups-combobox-mode/VB/Lookup-ComboboxMode/Form1.vb#L28

vb
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

See Also

ProcessNewValue

ComboBox Mode - Enter New Values

RepositoryItemLookUpEdit Class

RepositoryItemLookUpEdit Members

DevExpress.XtraEditors.Repository Namespace