鳴謝微信
隨着前面幾個章節對控件封裝與擴展的分享,很多小夥伴兒們在做者公衆號上反饋,並聯系做者,表示經過這些系列和源碼能學到很多細節上的東西,並運用到了本身的實際項目當中,也有很多夥伴兒反饋更好更優的處理方式。做者在此感謝你們的陪伴與探討,但願能與你們一塊兒學習,一塊兒進步。ide
工欲善其事必先利其器學習
公衆號反饋最多的是《玩轉控件:封裝Dev的LabelControl和TextEdit》,表示運用到本身實際項目後,確實大大減小了本身的工做量,並但願能有更多這種類型的博文。爲了知足小夥伴兒的心願,做者後續會分享更多本身實際項目運用到的小技巧,但願能對更多小夥伴兒有更多的幫助和啓發。最後,也但願小夥伴兒們能從做者分享對不一樣類型控件的封裝中觸類旁通,擴展知足本身實際業務的插件。有好的想法,別忘記分享給做者哦,三人行,必有我師嘛~測試
Talk is Cheapthis
廢話很少說,今天做者要分享的也是做者實際項目中遇到的問題——有數據源的下拉搜索框。很多大的企業反饋,公司職員比較多,數據量比較大,鼠標下拉尋找太過繁瑣和耗時,能不能提供個更優的處理方式。通過做者一番思索,以迅雷不及掩耳盜鈴響叮噹之勢就找到了符合客戶的處理方式——就是Dev的SearchLookupEdit。spa
你們也能夠直接用Dev的SearchLookupEdit控件,效果還不錯,固然爲了方便起見,減小本身的操做量,也能夠模仿《玩轉控件:封裝Dev的LabelControl和TextEdit》同樣,本身根據實際狀況作個封裝,來吧!做者陪你們一塊兒重溫下封裝的樂趣。插件
Show me the Codecode
和往常同樣,新建一個類用來封裝和擴展本身實際要用到的屬性和事件:orm
public partial class KzxSearchComboboxEdit : KzxBaseControl
初始化的時候,就能夠封裝好本身要用到的事件:對象
public KzxSearchComboboxEdit() { InitializeComponent(); this.ValueControl.QueryPopUp += new CancelEventHandler(lookUpEdit_QueryPopUp); this.ValueControl.Closed += new ClosedEventHandler(lookUpEdit_Closed); this.CaptionControl.AutoSizeMode = LabelAutoSizeMode.Vertical; this.CaptionControl.SizeChanged += new EventHandler(SetSize); this.ValueControl.Enter -= new EventHandler(ValueControl_Enter); this.ValueControl.Enter += new EventHandler(ValueControl_Enter); this._SearchLookUpEditView.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus; this._SearchLookUpEditView.Name = "gridLookUpEdit1View"; this._SearchLookUpEditView.OptionsSelection.EnableAppearanceFocusedCell = false; this._SearchLookUpEditView.OptionsView.ShowGroupPanel = false; this.ValueControl.Properties.PopupFormSize = new System.Drawing.Size((int)(this.ValueControl.Width * 2), (int)(this.ValueControl.Width * 1.5)); if (this.DesignMode == true) { this.Size = new Size(284, 21); } }
把本身實際須要用到的屬性作好封裝,舉個栗子:
private int _ItemIndex = -1; /// <summary> /// 選中項的下標 /// </summary> [Category("自定義"), Description("ItemIndex,選中項的下標"), Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [McDisplayName("ItemIndex")] public int ItemIndex { get { return this._ItemIndex; } protected set { this._ItemIndex = value; } } private DataRow _CurrentItem = null; /// <summary> /// 選中項的DataRow對象 /// </summary> [Category("自定義"), Description("CurrentItem,選中項的DataRow對象"), Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [McDisplayName("CurrentItem")] public DataRow CurrentItem { get { return this._CurrentItem; } protected set { this._CurrentItem = value; } } private DataSet _BillDataSet = new DataSet(); /// <summary> /// 單據的數據源 /// </summary> [Category("自定義"), Description("BillDataSet,單據的數據源"), Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [McDisplayName("BillDataSet")] public DataSet BillDataSet { get { return this._BillDataSet; } set { this._BillDataSet = value; } } /// <summary> /// 沒有多語言的狀況下的默認顯示標題 /// </summary> [Category("多語言"), Description("DesigeCaption,沒有多語言的狀況下的默認顯示標題"), Browsable(true)] [McDisplayName("DesigeCaption")] public override string DesigeCaption { get { return this.CaptionControl.Text.Trim(); } set { this.CaptionControl.Text = value; } }
也能夠根據本身喜愛(固然是符合客戶習慣的),多此一舉:
private bool _IsNull = true; /// <summary> /// 可空性 /// </summary> [Category("驗證"), Description("IsNull,可空性"), Browsable(true)] [McDisplayName("IsNull")] public override bool IsNull { get { SetBackColor(); return this._IsNull; } set { this._IsNull = value; SetBackColor(); } } ... /// <summary> /// 設置背景色 /// </summary> private void SetBackColor() { if (this.ValueControl.Properties.ReadOnly == true) { this.ValueControl.BackColor = Color.FromArgb(242, 242, 243); } else { if (this._IsNull.Equals(true) == false) { this.ValueControl.BackColor = Color.Yellow; } else { this.ValueControl.BackColor = this._TextBackColor; } } }
當設置控件必填的時候,設置控件的背景色用於區分,具體效果如圖:
封裝好控件取值,填充值的方法,以及控件數據源綁定的方法(具體根據本身實際項目應用爲準,此處只是舉個栗子)
/// <summary> /// 取控件的值 /// </summary> /// <return>Object</return> public override object GetValue() { DataRowView rowview = null; BindingSource bs = null; object v = null; v = this.ValueControl.EditValue == null || this.ValueControl.EditValue == DBNull.Value ? string.Empty : this.ValueControl.EditValue.ToString(); return v; } /// <summary> /// 設置控件的值 /// </summary> /// <param name="value">控件的值</param> /// <return>int</return> public override int SetValue(object value) { this.ValueControl.EditValue = value == null || value == DBNull.Value ? string.Empty : value; return 1; } /// <summary> /// 設置數據綁定 /// </summary> /// <param name="binding">數據綁定對象</param> /// <return>int</return> public override int SetDataBinding(object binding) { this.BindingObject = this.ValueControl.DataBindings.Add("EditValue", binding, this.Field, true, DataSourceUpdateMode.OnValidation, string.Empty, this.FormatString); SetColumnDisplayFormat(); if (binding is BindingSource) { int maxlength = 0; if (((BindingSource)binding).DataSource is DataView) { if (((DataView)(((BindingSource)binding).DataSource)).Table.Columns[this.Field].DataType == typeof(string)) { maxlength = ((DataView)(((BindingSource)binding).DataSource)).Table.Columns[this.Field].MaxLength; if (maxlength >= 0) { this.MaxLength = maxlength; } } } else if (((BindingSource)binding).DataSource is DataTable) { if (((DataTable)(((BindingSource)binding).DataSource)).Columns[this.Field].DataType == typeof(string)) { maxlength = ((DataTable)(((BindingSource)binding).DataSource)).Columns[this.Field].MaxLength; if (maxlength >= 0) { this.MaxLength = maxlength; } } } } return 1; } /// <summary> /// 設置下拉框的數據源 /// </summary> /// <param name="binding">下拉框的數據源</param> /// <param name="displayMember">顯示值字段名</param> /// <param name="valueMember">實際值字段名</param> /// <returns>int</returns> public override int SetSourceTableBinding(object binding, string displayMember, string valueMember) { this.DisplayMemberPath = displayMember; this.SelectedValuePath = valueMember; this.ValueControl.Properties.DataSource = binding; this._ResourceDataSource = binding; return 1; }
完成!爲測試效果,在窗體Load事件中造個測試數據,看看效果:
DataTable dataTable = new DataTable("Student"); dataTable.Columns.Add("編號", typeof(String)); dataTable.Columns.Add("暱稱", typeof(String)); dataTable.Columns.Add("名稱", typeof(String)); dataTable.Rows.Add(new String[] { "1", "James", "張三" }); dataTable.Rows.Add(new String[] { "2", "Mary", "李四" }); dataTable.Rows.Add(new String[] { "3", "Jack", "王五" }); dataTable.Rows.Add(new String[] { "4", "joy", "趙六" }); dataTable.Rows.Add(new String[] { "5", "jay", "錢七"}); dataTable.Rows.Add(new String[] { "6", "stephen", "康忠鑫"}); kzxSearchCbbeSupperStar.SetSourceTableBinding(dataTable, "名稱", "暱稱");
和原始Dev控件同樣,支持篩選功能,媽媽不再用擔憂客戶反饋因數據量大的問題查找不便了!
結束語
因爲後續全部重寫/重繪控件都在同一個項目使用,並且Dev系統引用文件較多,壓縮後源碼文件仍然很大,若是有須要源碼的朋友,能夠微信公衆號聯繫博主,源碼能夠免費贈予~!有疑問的也能夠CALL我一塊兒探討。
最後,感謝您的耐心陪伴!若是以爲本篇博文對您或者身邊朋友有幫助的,麻煩點個關注!贈人玫瑰,手留餘香,您的支持就是我寫做最大的動力,感謝您的關注,期待和您一塊兒探討!再會!