Error message here!

Hide Error message here!

忘记密码?

Error message here!

请输入正确邮箱

Hide Error message here!

密码丢失?请输入您的电子邮件地址。您将收到一个重设密码链接。

Error message here!

返回登录

Close

自定义GridLookUpEdit编辑器

惊梦无痕 2019-02-20 16:57:00 阅读数:171 评论数:0 点赞数:0 收藏数:0

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

前两天分享了GridControl的自定义编辑器,今天再来分享一下整理出来的GridLookUpEdit的自定义编辑器。本代码用的DevExpress版本号:17.2.6.0,旧的版本可能有些地方会有些微的变化。该自定义编辑器需要用到上篇中定义的MyGridView(具体代码可在自定义GridControl编辑器一文中阅览),此控件包含了多列模糊查询功能,希望对使用或正在学习DevExpress的同学有所帮助。后续有时间会陆续将一些比较实用的自定义编辑器分享出来。1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Windows.Forms;5 usingComteck.Dto;6 usingDevExpress.XtraEditors;7 usingDevExpress.XtraEditors.Controls;8 usingDevExpress.XtraEditors.Drawing;9 usingDevExpress.XtraEditors.Popup;10 usingDevExpress.XtraEditors.Registrator;11 usingDevExpress.XtraEditors.Repository;12 usingDevExpress.XtraEditors.ViewInfo;13 usingDevExpress.XtraGrid;14 usingDevExpress.XtraGrid.Views.Base;15 16 namespaceComteck.Winforms.Controls {17 ///

18 ///自定义的GridLookupEdit,允许进行多列的匹配19 /// 参照:https://www.devexpress.com/Support/Center/Example/Details/E1985 20 /// 21 [ToolboxItem(true)]22 public classMyGridLookUpEdit : GridLookUpEdit {23 /// 24 ///自动注册下拉框编辑器25 /// 26 staticMyGridLookUpEdit() {27 RepositoryItemMyGridLookUpEdit.RegisterCustomGridLookUpEdit();28 }29 30 /// 31 ///创建自定义GridLookupEdit32 /// 33 public MyGridLookUpEdit() : base() {34 Initialize();35 }36 37 /// 38 ///初始化39 /// 40 private voidInitialize() {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(objectsender, KeyEventArgs e) {81 if (e.KeyCode ==Keys.Delete) {82 var control = sender asBaseEdit;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 /// 100 ///自定义编辑器名称101 /// 102 public override string EditorTypeName =>RepositoryItemMyGridLookUpEdit.MyGridLookUpEditName;103 104 /// 105 ///重写下拉框编辑器106 /// 107 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]108 public new RepositoryItemMyGridLookUpEdit Properties => base.Properties asRepositoryItemMyGridLookUpEdit;109 110 // 111 //摘要:112 //Gets or sets whether focus is moved to the next control (according to the tab113 //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 overridePopupBaseForm CreatePopupForm() {123 return new MyGridLookUpPopupForm(this);124 }125 126 /// 127 /// 128 /// 129 /// 130 /// 131 public override boolIsNeededKey(KeyEventArgs e) {132 return this.Properties.IsNeededKey(e.KeyData);133 }134 135 /// 136 ///正常情况下,在输入第一个字符(主要是数字及字母)后,虽然自动弹出了下拉框并过滤了数据,137 ///但是此时光标并未定位到下拉框中,导致回车后并未返回匹配到的首行记录138 ///此处就是为了解决以上问题,展开下拉框时自动定位到首行139 /// 140 protected override voidOnPopupShown() {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 ///匹配自定义编辑器的下拉GridLookUpEdit153 /// 154 [DXCategory("Properties")]155 [UserRepositoryItem("RegisterMyGridLookUpEdit")]156 public classRepositoryItemMyGridLookUpEdit : RepositoryItemGridLookUpEdit {157 /// 158 ///编辑器的名称159 /// 160 public const string MyGridLookUpEditName = "MyGridLookUpEdit";161 162 /// 163 ///注册编辑器164 /// 165 staticRepositoryItemMyGridLookUpEdit() {166 RegisterCustomGridLookUpEdit();167 }168 169 /// 170 ///创建自定义的编辑器171 /// 172 publicRepositoryItemMyGridLookUpEdit() {173 //不允许自动完成 174 base.AutoComplete = false;175 }176 177 /// 178 ///编辑器名称179 /// 180 public override string EditorTypeName =>MyGridLookUpEditName;181 182 /// 183 ///注册编辑器184 /// 185 public static voidRegisterCustomGridLookUpEdit() {186 EditorRegistrationInfo.Default.Editors.Add(187 newEditorClassInfo(188 MyGridLookUpEditName,189 typeof(MyGridLookUpEdit),190 typeof(RepositoryItemMyGridLookUpEdit),191 typeof(GridLookUpEditBaseViewInfo),192 newButtonEditPainter(),193 true));194 }195 196 /// 197 ///创建自定义GridView198 /// 199 /// 200 protected overrideColumnView CreateViewInstance() {201 return newMyGridView();202 }203 204 /// 205 ///创建自定义GridControl206 /// 207 /// 208 protected overrideGridControl CreateGrid() {209 return newMyGridControl();210 }211 }212 213 public classMyGridLookUpPopupForm : PopupGridLookUpEditForm214 {215 publicMyGridLookUpPopupForm(GridLookUpEdit ownerEdit)216 : base(ownerEdit) {217 }218 219 protected override voidOnKeyDown(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 }

 

版权声明
本文为[惊梦无痕]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/geping/p/10407689.html

编程之旅,人生之路,不止于编程,还有诗和远方。
阅代码原理,看框架知识,学企业实践;
赏诗词,读日记,踏人生之路,观世界之行;

支付宝红包,每日可领