主要是繼承原有TextBox;追加編號內容(ValueText)和顯示內容(DisplayText);在編輯完成後查詢數據庫調出DisplayText(如:名稱);並顯示在原TextBox中;當TextBox為編輯狀態是顯示ValueText(如:編號)
當然還可以進一步增強;如:增加對於的數據字典、增加DataRow所有數據(便於開發時根據記錄集更新其他組件)等等
運行效果如下圖
主要代碼:
1.繼承組件
public partial class TTextBox : Infragistics.Win.UltraWinEditors.UltraTextEditor
2.定義屬性
private string displayMember; public string DisplayMember { get { return displayMember; } set { displayMember = value; } } private string valueMember; public string ValueMember { get { return valueMember; } set { valueMember = value; } } private int max_Length; public int Max_Length { get { return max_Length; } set { max_Length = value; } }
3.原始原組件事件;並追加代碼
protected override void OnEndInit() { base.OnEndInit(); max_Length = base.MaxLength; } protected override void OnBeforeEnterEditMode(CancelEventArgs e) { base.OnBeforeEnterEditMode(e); if (valueMember == null) valueMember = ""; if (max_Length != null && max_Length != 0) base.MaxLength = max_Length; base.Value = valueMember; } protected override void OnEnter(EventArgs e) { base.OnEnter(e); base.SelectAll(); } protected override void OnAfterExitEditMode(EventArgs e) { base.OnAfterExitEditMode(e); if (displayMember == null) displayMember = ""; base.MaxLength = 0; base.Value = displayMember; }
protected override void OnValueChanged(EventArgs e) { base.OnValueChanged(e); if (base.Value == null) return; valueMember = base.Value.ToString(); //maxLength = base.MaxLength; DisplayMember = GetValue("select Name from Table"); if (DisplayMember == "") valueMember = ""; else DisplayMember = DisplayMember+ "(" + valueMember + ")"; }
運行效果如下圖: