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

前两天分享了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. /// <summary>
  18. /// 自定义的GridLookupEdit,允许进行多列的匹配
  19. /// <para>参照:https://www.devexpress.com/Support/Center/Example/Details/E1985 </para>
  20. /// </summary>
  21. [ToolboxItem(true)]
  22. public class MyGridLookUpEdit : GridLookUpEdit {
  23. /// <summary>
  24. /// 自动注册下拉框编辑器
  25. /// </summary>
  26. static MyGridLookUpEdit() {
  27. RepositoryItemMyGridLookUpEdit.RegisterCustomGridLookUpEdit();
  28. }
  29.  
  30. /// <summary>
  31. /// 创建自定义GridLookupEdit
  32. /// </summary>
  33. public MyGridLookUpEdit() : base() {
  34. Initialize();
  35. }
  36.  
  37. /// <summary>
  38. /// 初始化
  39. /// </summary>
  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. /// <summary>
  76. ///
  77. /// </summary>
  78. /// <param name="sender"></param>
  79. /// <param name="e"></param>
  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 == 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. /// <summary>
  100. /// 自定义编辑器名称
  101. /// </summary>
  102. public override string EditorTypeName => RepositoryItemMyGridLookUpEdit.MyGridLookUpEditName;
  103.  
  104. /// <summary>
  105. /// 重写下拉框编辑器
  106. /// </summary>
  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. /// <summary>
  119. ///
  120. /// </summary>
  121. /// <returns></returns>
  122. protected override PopupBaseForm CreatePopupForm() {
  123. return new MyGridLookUpPopupForm(this);
  124. }
  125.  
  126. /// <summary>
  127. ///
  128. /// </summary>
  129. /// <param name="e"></param>
  130. /// <returns></returns>
  131. public override bool IsNeededKey(KeyEventArgs e) {
  132. return this.Properties.IsNeededKey(e.KeyData);
  133. }
  134.  
  135. /// <summary>
  136. /// 正常情况下,在输入第一个字符(主要是数字及字母)后,虽然自动弹出了下拉框并过滤了数据,
  137. /// 但是此时光标并未定位到下拉框中,导致回车后并未返回匹配到的首行记录
  138. /// 此处就是为了解决以上问题,展开下拉框时自动定位到首行
  139. /// </summary>
  140. protected override void OnPopupShown() {
  141. base.OnPopupShown();
  142.  
  143. BeginInvoke(new Action(() => {
  144. if (this.GetSelectedDataRow() == null) {
  145. this.Properties.View.FocusedRowHandle = ;
  146. }
  147. }));
  148. }
  149. }
  150.  
  151. /// <summary>
  152. /// 匹配自定义编辑器的下拉GridLookUpEdit
  153. /// </summary>
  154. [DXCategory("Properties")]
  155. [UserRepositoryItem("RegisterMyGridLookUpEdit")]
  156. public class RepositoryItemMyGridLookUpEdit : RepositoryItemGridLookUpEdit {
  157. /// <summary>
  158. /// 编辑器的名称
  159. /// </summary>
  160. public const string MyGridLookUpEditName = "MyGridLookUpEdit";
  161.  
  162. /// <summary>
  163. /// 注册编辑器
  164. /// </summary>
  165. static RepositoryItemMyGridLookUpEdit() {
  166. RegisterCustomGridLookUpEdit();
  167. }
  168.  
  169. /// <summary>
  170. /// 创建自定义的编辑器
  171. /// </summary>
  172. public RepositoryItemMyGridLookUpEdit() {
  173. // 不允许自动完成
  174. base.AutoComplete = false;
  175. }
  176.  
  177. /// <summary>
  178. /// 编辑器名称
  179. /// </summary>
  180. public override string EditorTypeName => MyGridLookUpEditName;
  181.  
  182. /// <summary>
  183. /// 注册编辑器
  184. /// </summary>
  185. public static void RegisterCustomGridLookUpEdit() {
  186. EditorRegistrationInfo.Default.Editors.Add(
  187. new EditorClassInfo(
  188. MyGridLookUpEditName,
  189. typeof(MyGridLookUpEdit),
  190. typeof(RepositoryItemMyGridLookUpEdit),
  191. typeof(GridLookUpEditBaseViewInfo),
  192. new ButtonEditPainter(),
  193. true));
  194. }
  195.  
  196. /// <summary>
  197. /// 创建自定义GridView
  198. /// </summary>
  199. /// <returns></returns>
  200. protected override ColumnView CreateViewInstance() {
  201. return new MyGridView();
  202. }
  203.  
  204. /// <summary>
  205. /// 创建自定义GridControl
  206. /// </summary>
  207. /// <returns></returns>
  208. protected override GridControl CreateGrid() {
  209. return new MyGridControl();
  210. }
  211. }
  212.  
  213. public class MyGridLookUpPopupForm : PopupGridLookUpEditForm
  214. {
  215. public MyGridLookUpPopupForm(GridLookUpEdit ownerEdit)
  216. : base(ownerEdit) {
  217. }
  218.  
  219. protected override void OnKeyDown(KeyEventArgs e) {
  220. if (e.KeyCode == Keys.Tab) {
  221. this.OwnerEdit.EditValue = QueryResultValue();
  222. this.OwnerEdit.SendKey(e);
  223. }
  224.  
  225. base.OnKeyDown(e);
  226. }
  227. }
  228. }

自定义GridLookUpEdit编辑器的更多相关文章

  1. springmvc自定义日期编辑器

    1.控制器 @Controller public class MyController { // 处理器方法 @RequestMapping(value = "/first.do" ...

  2. 使用 TypeScript,React,ANTLR 和 Monaco Editor 创建一个自定义 Web 编辑器(二)

    译文来源 欢迎阅读如何使用 TypeScript, React, ANTLR4, Monaco Editor 创建一个自定义 Web 编辑器系列的第二章节, 在这之前建议您阅读使用 TypeScrip ...

  3. 我的第一个npm包:wechat-menu-editor 基于Vue的微信自定义菜单编辑器

    wechat-menu-editor 微信自定义菜单编辑器 前言 在做微信公众号相关开发时,基本上会去开发的功能就是微信自定义菜单设置的功能,本着不重复造轮子的原则,于是基于Vue封装的一个微信自定义 ...

  4. 自定义GridControl编辑器

    本文版权归博主 惊梦无痕 所有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作.SourceLink 鉴于网上的针对GridControl的一些代码比较凌乱,且功能分散,故将整理过的代码分享出来 ...

  5. C#如何在DataGridViewCell中自定义脚本编辑器

    上一篇博文探讨了如何自定义DataGridViewColumn实现一个TreeViewColumn来在DataGridView控件中显示TreeView控件,其实我们还可以继续发挥想象,自定义其他的列 ...

  6. 如何自定义kindeditor编辑器的工具栏items即去除不必要的工具栏或者保留部分工具栏

    kindeditor编辑器的工具栏主要是指编辑器输入框上方的那些可以操作的菜单,默认情况下编辑器是给予了所有的工具栏.针对不同的用户,不同的项目,不同的环境,可能就需要保留部分工具栏.那么我们应该如何 ...

  7. Unity编辑器:自定义编辑器样式——GUIStyle

    通过GUIStyle,可以自定义Unity编辑器的样式. GUIStyle可以new一个全新的实例,这样,需要自己处理所有自己需要的效果. GUIStyle还可以基于已经存在的实例new一个新的实例, ...

  8. 【Unity】自定义编辑器窗口——拓展编辑器功能

    最近学习了Unity自定义编辑器窗口,下面简单总结,方便用到时回顾. 新建一个脚本: using UnityEngine; using System.Collections; using UnityE ...

  9. 百度编辑器(UEditor)自定义工具栏

    百度编辑器(UEditor)自定义工具栏的自定义 百度编辑器默认功能比较齐全,但是不一定是我们所需要的,有的功能可以去掉,用自己想要的就可以了,可以参考百度官方文档! 百度编辑器默认配置展示界面 如何 ...

随机推荐

  1. centos下网络的配置

    1.网络模式要进行使用NAT,要连网的话,要配置好设置:网络要进行一下共享到虚拟机 进入vi /etc/sysconfig/network-scripts/ifcfg-eth0   把里面的onboo ...

  2. linux 关于Apache默认编码错误 导致网站乱码的解决方案

    Apache默认编码UTF-8在解析A网站的时候没有任何问题,当运行B网站时出现的"蝌蚪文"乱码问题   最近经常有同学在使用LAMP/WAMP时,遇到这样的编码错误问题: A网站 ...

  3. python_重写数组

    class MyArray: '''All the elements in this array must be numbers''' def __IsNumber(self,n): if not i ...

  4. CF#483(div2 C)

    http://codeforces.com/contest/984/problem/C C. Finite or not time limit per test 1 second memory lim ...

  5. @ConditionalOnMissingBean注解理解

    @ConditionalOnMissingBean注解作用在@bean定义上,它的作用就是在容器加载它作用的bean时,检查容器中是否存在目标类型(ConditionalOnMissingBean注解 ...

  6. capwap学习笔记——初识capwap(四)(转)

    2.5.7 CAPWAP传输机制 WTP和AC之间使用标准的UDP客户端/服务器模式来建立通讯. CAPWAP协议支持UDP和UDP-Lite [RFC3828]. ¢ 在IPv4上,CAPWAP控制 ...

  7. js术语扫盲贴:XHR、RegExp、call-apply、prototype

    (1) XHR:xml httprequestXHR注入:XHR 注入技术是通过XMLHttpRest来获取javascript的.但与eval不同的是,该机制是通过创建一个script的DOM元素, ...

  8. tkinter中spinbox递增和递减控件(十)

    spinbox递增和递减控件 import tkinter wuya = tkinter.Tk() wuya.title("wuya") wuya.geometry("3 ...

  9. 理解Flexbox弹性盒子

    http://www.w3cplus.com/css3/understanding-flexbox-everything-you-need-to-know.html参考文档 1:要开始使用Flexbo ...

  10. 如何打开JSP文件/JS和JSP的区别/Servlet的本质是什么,是如何工作的?

    一:如何打开JSP文件 1.安装JAVA 2.安装TOMCAT——免费开源的JAVAWEB服务器 3.安装ECLIPSE 二:JS和JSP区别 名字: JS:JavaScript JSP:Java S ...