程序技术好文:自定义GridLookUpEdit编辑器

简介: 程序技术好文:自定义GridLookUpEdit编辑器

本文版权归博主 惊梦无痕 所有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作。原文地址


前两天分享了GridControl的自定义编辑器,今天再来分享一下整理出来的GridLookUpEdit的自定义编辑器。


本代码用的DevExpress版本号:17.2.6.0,旧的版本可能有些地方会有些微的变化。


该自定义编辑器需要用到上篇中定义的MyGridView(具体代码可在自定义GridControl编辑器一文中阅览),此控件包含了多列模糊查询功能,希望对使用或正在学习DevExpress的同学有所帮助。


后续有时间会陆续将一些比较实用的自定义编辑器分享出来。


1 using System;


2 using System.Collections.Generic;


3 using System.ComponentModel;


4 using System.Windows.Forms;


5 using Comteck.Dto;


6 using DevExpress.XtraEditors;


7 using DevExpress.XtraEditors.Controls;


8 using DevExpress.XtraEditors.Drawing;


9 using DevExpress.XtraEditors.Popup;


10 using DevExpress.XtraEditors.Registrator;


11 using DevExpress.XtraEditors.Repository;


12 using DevExpress.XtraEditors.ViewInfo;


13 using DevExpress.XtraGrid;


14 using DevExpress.XtraGrid.Views.Base;


15


16 namespace Comteck.Winforms.Controls {


17 ///


18 /// 自定义的GridLookupEdit,允许进行多列的匹配


19 ///


参照:


20 ///


21 【ToolboxItem(true)】


22 public class MyGridLookUpEdit : GridLookUpEdit {


23 ///


24 /// 自动注册下拉框编辑器


25 ///


26 static MyGridLookUpEdit() {


27 RepositoryItemMyGridLookUpEdit.RegisterCustomGridLookUpEdit();


28 }


29


30 ///


31 /// 创建自定义GridLookupEdit


32 ///


33 public MyGridLookUpEdit() : base() {


34 Initialize();


35 }


36


37 ///


38 /// 初始化


39 ///


40 private void Initialize() {


41 this.Tag = false; // 设置全选标记


42 this.Properties.AllowMouseWheel = false;


43 //this.EnterMoveNextControl = true;


44 this.Properties.ImmediatePopup = true;


45 this.Properties.TextEditStyle = TextEditStyles.Standard;


46


47 #region 编辑框默认自动全选


48


49 // 鼠标移入文本编辑框触发事件


50 this.Enter += (sender, e) => {


51 // 设置全选标记


52 this.Tag = true;


53 this.SelectAll();


54 };


55 // 获取输入焦点时自动全选


56 this.MouseUp += (sender, e) => {


57 // 如果鼠标左键操作并且标记存在,则执行全选


58 if (e.Button == MouseButtons.Left && (bool)this.Tag) {


59 this.SelectAll();


60 }


61


62 // 取消全选标记


63 this.Tag = false;


64 };


65 // 双击输入框时自动全选


66 this.DoubleClick += (sender, e) => {


67 this.SelectAll();


68 };


69


70 #endregion


71


72 this.KeyDown += this.MyGridLookUpEdit_KeyDown;


73 }


74


75 ///


76 ///


77 ///


78 ///


79 ///


80 private void MyGridLookUpEdit_KeyDown(object sender, KeyEventArgs e) {


81 if (e.KeyCode == Keys.Delete) {


82 var control = sender as BaseEdit;


83 if (control.ReadOnly) { return; }


84 control.EditValue = null;


85 e.Handled = true;


86 } else if (e.KeyCode =//代码效果参考:http://hnjlyzjd.com/xl/wz_24141.html

= Keys.Down) {

87 this.ShowPopup();


88


89 e.Handled = true;


90 } else if (e.KeyCode == Keys.Back) {


91 if (this.IsPopupOpen == false) {


92 this.ShowPopup();


93


94 e.Handled = true;


95 }


96 }


97 }


98


99 ///


100 /// 自定义编辑器名称


101 ///


102 public override string EditorTypeName => RepositoryItemMyGridLookUpEdit.MyGridLookUpEditName;


103


104 ///


105 /// 重写下拉框编辑器


106 ///


107 【DesignerSerializationVisibility(DesignerSerializationVisibility.Content)】


108 public new RepositoryItemMyGridLookUpEdit Properties => base.Properties as RepositoryItemMyGridLookUpEdit;


109


110 //


111 // 摘要:


112 // Gets or sets whether focus is moved to the next control (according to the tab


113 // order) when an end-user presses ENTER.


114 【DefaultValue(true)】


115 【DXCategory("Behavior")】


116 public override bool EnterMoveNextControl { get; set; } = true;


117


118 ///


119 ///


120 ///


121 ///


122 protected override PopupBaseForm CreatePopupForm() {


123 return new MyGridLookUpPopupForm(this);


124 }


125


126 ///


127 ///


128 ///


129 ///


130 ///


131 public override bool IsNeededKey(KeyEventArgs e) {


132 return this.Properties.IsNeededKey(e.KeyData);


133 }


134


135 ///


136 /// 正常情况下,在输入第一个字符(主要是数字及字母)后,虽然自动弹出了下拉框并过滤了数据,


137 /// 但是此时光标并未定位到下拉框中,导致回车后并未返回匹配到的首行记录


138 /// 此处就是为了解决以上问题,展开下拉框时自动定位到首行


139 ///


140 protected override void OnPopupShown() {


141 base.OnPopupShown();


142


143 BeginInvoke(new Action(() => {


144 if (this.GetSelectedDataRow() == null) {


145 this.Properties.View.FocusedRowHandle = 0;


146 }


147 }));


148 }


149 }


150


151 ///


152 /// 匹配自定义编辑器的下拉GridLookUpEdit


153 ///


154 【DXCategory("Properties")】


155 【UserRepositoryItem("RegisterMyGridLookUpEdit")】


156 public class RepositoryItemMyGridLookUpEdit : RepositoryItemGridLookUpEdit {


157 ///


158 /// 编辑器的名称


159 ///


160 public const string MyGridLookUpEditName = "MyGridLookUpEdit";


161


162 ///


163 /// 注册编辑器


164 ///


165 static RepositoryItemMyGridLookUpEdit() {


166 RegisterCustomGridLookUpEdit();


167 }


168


169 ///


170 /// 创建自定义的编辑器


171 ///


172 public RepositoryItemMyGridLookUpEdit() {


173 // 不允许自动完成


174 base.AutoComplete = false;


175 }


176


177 ///


178 /// 编辑器名称


179 ///


180 public override string EditorTypeName => MyGridLookUpEditName;


181


182 ///


183 /// 注册编辑器


184 ///


185 public static void RegisterCustomGridLookUpEdit() {


186 EditorRegistrationInfo.Default.Editors.Add(


相关文章
|
2月前
|
前端开发 数据可视化 API
前端react 18.2整合ckeditor富文本编辑器——配置插件、自定义toolbar工具栏(二)
前端react 18.2整合ckeditor富文本编辑器——配置插件、自定义toolbar工具栏
126 0
前端react 18.2整合ckeditor富文本编辑器——配置插件、自定义toolbar工具栏(二)
|
2月前
|
前端开发 JavaScript CDN
前端react 18.2整合ckeditor富文本编辑器——配置插件、自定义toolbar工具栏(一)
前端react 18.2整合ckeditor富文本编辑器——配置插件、自定义toolbar工具栏
82 0
|
11月前
|
Unix Shell Linux
第九章、vim程序编辑器
第九章、vim程序编辑器
52 0
|
11月前
|
JavaScript
Vue CKEditor5 自定义编辑器详细流程(插件安装使用流程)
Vue CKEditor5 自定义编辑器详细流程(插件安装使用流程)
976 0
|
12月前
|
数据库
wangEditor富文本编辑器的调用开发实录2(V5版本自定义粘贴,去除复制word或网页html冗余样式代码的解决方案)
wangEditor富文本编辑器的调用开发实录2(V5版本自定义粘贴,去除复制word或网页html冗余样式代码的解决方案)
473 0
|
Unix Linux 开发工具
【Linux】Vim程序编辑器
Vim是一种流行的命令行程序编辑器,常用于编辑程序代码或其他文本文件。它常被Linux和Unix用户所喜欢,也有逐渐应用到其他计算机系统。
82 0
西门子S7-200 SMART如何切换编程编辑器、如何输入LAD程序、编辑程序
上篇文章中我们学习了西门子S7-200 SMART PLC基本操作、查看PLC信息以及如何设置PLC日期和时间,本篇我们来介绍如何在编程软件STEP7-Micro/WIN SMART中切换程序编辑器、输入LAD程序以及如何编辑程序。STEP7-Micro/WIN SMART支持三种编程方式:LAD(梯形图)、FBD(功能块图)、STL(语句表)。其中LAD(梯形图)是最常用的编程方式,本篇我们就以梯形图为例来讲解。
西门子S7-200 SMART如何切换编程编辑器、如何输入LAD程序、编辑程序
|
JavaScript 前端开发 程序员
前端开发:VS Code编辑器新建Vue文件自定义模板的方法
在前端开发过程中,尤其是对于经验不足的初级开发者来说,需要掌握一些必备的提升开发速度的技巧,比如使用各种插件来提升开发速度,这样才能事半功倍。
310 0
前端开发:VS Code编辑器新建Vue文件自定义模板的方法
|
Java Spring
Spring自定义属性编辑器及原理解释.md
Spring自定义属性编辑器及原理解释.md
|
Java
编写Java程序,使用菜单组件制作一个记事本编辑器
编写Java程序,使用菜单组件制作一个记事本编辑器
220 0
编写Java程序,使用菜单组件制作一个记事本编辑器