本文版權歸博主 驚夢無痕 所有,歡迎轉載,但須保留此段聲明,並給出原文鏈接,謝謝合作。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 = 0; 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 }