编写查询功能TextBox
主要是继承原有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 + ")"; }
运行效果如下图:
IvanDesign-快速开发平台
下载地址:www.cnblogs.com/DesignIvan/archive/2012/06/12/DesignSetup.html