DevExpress LookupEdit按回车键自动确认选择数据|C/S开发框架
作者:csframework|C/S框架网  发布日期:2026/01/01 23:04:09

DevExpress LookupEdit按回车键自动确认选择数据|C/S开发框架

非常好用的代码:

C# 全选
/// <summary>
/// LookupEdit按回车键自动确认选择数据
/// </summary>
/// <param name="editor"></param>
public static void HandleEnterKeyAcceptValue(this LookUpEdit editor)
{
    editor.KeyDown += (s, e) =>
    {
        if (e.KeyCode == Keys.Enter)
        {
            // 优先级1:根据显示文本获取键值
            var value1 = editor.Properties.GetKeyValueByDisplayText(editor.Text);
            if (value1 != null)
            {
                editor.ClosePopup(); //必须先关闭下拉窗体
                editor.SetBindingValue(value1, true);
                e.Handled = true;
                return;
            }

            // 优先级2:录入的文本作为键值获取数据源obj
            var obj = editor.Properties.GetDataSourceRowByKeyValue(editor.Text);
            if (obj == null) obj = editor.Properties.GetDataSourceRowByKeyValue(editor.Text.ToLower()); //查找全小写
            if (obj == null) obj = editor.Properties.GetDataSourceRowByKeyValue(editor.Text.ToUpper()); //查找全大写
            if (obj != null)
            {
                editor.ClosePopup(); //必须先关闭下拉窗体
                var v = ObjectHelper.GetPropertyValue(obj, editor.Properties.ValueMember);
                editor.SetBindingValue(v, true);
                e.Handled = true;
                return;
            }
        }
    };
}

DevExpress LookupEdit按回车键自动确认选择数据|C/S开发框架

C/S框架网|原创精神.创造价值.打造精品


扫一扫加作者微信
C/S框架网作者微信 C/S框架网|原创作品.质量保障.竭诚为您服务
上一篇 下一篇