It mainly inherits the original TextBox; append number content (ValueText) and display content (DisplayText); after editing, query the database to bring up DisplayText (for example, name); and display it in the original TextBox; when the TextBox is edited, it displays ValueText (for example, number)
Of course, it can be further enhanced; for example, adding data dictionaries and adding all data of DataRow (to facilitate the updating of other components based on the record set during development ).
The running effect is as follows:
Main Code:
1. inherit Components
public partial class TTextBox : Infragistics.Win.UltraWinEditors.UltraTextEditor
2. Define attributes
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. original component events, and additional code
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 + ")"; }
The running effect is as follows: