using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; using System.Drawing.Design; using System.Windows.Forms.Design; using System.Threading; using System.Drawing; using CommonLib; using System.Text.RegularExpressions; using Lskj.MyControl; namespace Lskj.MyControl { /// /// 自动搜索器 /// public partial class AutoSearcherTreeView : TextBox { /// /// 控件所在的面板 /// //public Control SearchControlPanel { get; set; } //-----------------------------------------------私有字段-------------- //树视图控件 private TreeViewEx FListVAuto; //记载上次的选中值 private string StrOld = ""; //窗体 public FrmAutoSearchTreeView FrmAutoShowForm; //树节点绑定的值 public string ValueMember; //树节点显示的值 public string DisplayMember; /// /// 构造函数 /// public AutoSearcherTreeView() { Init(); } /// /// 初始化函数 /// private void Init() { Text = ""; FrmAutoShowForm = new FrmAutoSearchTreeView(); FrmAutoShowForm.TreeView.GetTreeView.CheckBoxes=false; FListVAuto = FrmAutoShowForm.TreeView; this.KeyUp += new KeyEventHandler(onSelfKeyUp); this.Leave += new EventHandler(OnSelfLostFocus); this.Enter += new EventHandler(AutoSearcher_Enter); this.MouseDown += new MouseEventHandler(AutoSearcher_MouseDown); this.MouseUp += new MouseEventHandler(AutoSearcher_MouseUp); FListVAuto.MyAfterSelect += new TreeViewEventHandler(TreeViewClick); } private void AutoSearcher_MouseUp(object sender, MouseEventArgs e) { this.SelectAll(); } /// /// 鼠标按下 /// private void AutoSearcher_MouseDown(object sender, MouseEventArgs e) { //this.Text = "";// Text.Trim(); StrOld = this.Text; FrmAutoShowForm.Show(); this.Focus(); } /// /// 输入控件失去焦点事件处理 /// /// /// private void OnSelfLostFocus(object sender, EventArgs e) { if (FrmAutoShowForm.Visible && !FListVAuto.Focused) { FrmAutoShowForm.Hide(); } } /// 输入框回车事件的处理 /// /// /// private void onSelfKeyUp(object sender, KeyEventArgs e) { if (e.KeyValue == 13) { if (Text.Trim() == "") OnTextChanged(null); if (FrmAutoShowForm.Visible) this.Text = FListVAuto.GetTreeView.SelectedNode.Text; FrmAutoShowForm.Hide(); } if (e.KeyValue == 27) { if (FrmAutoShowForm.Visible) FrmAutoShowForm.Visible = false; } } /// /// 绑定树结构 /// /// /// public void BindTree(DataTable data) { for (int i = 0; i < data.Rows.Count; i++) { DataRow dr = data.Rows[i]; String tnKey = dr[ValueMember] + ""; String tnStr = dr[DisplayMember] + ""; DataColumnCollection dcc = data.Columns; String CodeNo = string.Empty; if (dcc.Count > 2) CodeNo = dr[2].ToString(); TreeNode[] tn = null; if (tnKey.Length >= 4) { String pKey = tnKey.Substring(0, tnKey.Length - 2); tn = FListVAuto.GetTreeView.Nodes.Find(pKey, true); if (tn.Count() > 0) { TreeNode tNode = tn[0].Nodes.Add(tnKey, tnStr); tNode.Tag = dr; } } else { TreeNode tNode = FListVAuto.GetTreeView.Nodes.Add(tnKey, tnStr); } } } /// /// 搜索节点递归 /// public TreeNode FindNode(TreeNode tnParent, string strValue) { if (tnParent == null) return null; for (int i = 0; i < tnParent.Text.Length - strValue.Length + 1; i++) { if (tnParent.Text.Substring(i, strValue.Length) == strValue && (!tnParent.Checked)) { return tnParent; } } TreeNode tnRet = null; foreach (TreeNode tn in tnParent.Nodes) { tnRet = FindNode(tn, strValue); if (tnRet != null) break; } return tnRet; } /// /// 树点击事件 /// private void TreeViewClick(object sender, TreeViewEventArgs e) { this.Text = e.Node.Text; } /// /// 输入内容变化处理过程 /// /// protected override void OnTextChanged(EventArgs e) { TreeNode tnRet = null; foreach (TreeNode tn in FListVAuto.GetTreeView.Nodes) { tnRet = FindNode(tn, this.Text); if (tnRet != null) { tnRet.EnsureVisible(); FListVAuto.GetTreeView.Select(); FListVAuto.GetTreeView.SelectedNode = tnRet; break; } } this.Focus(); this.Select(Text.Length, 0); base.OnTextChanged(e); } /// /// 键入事件 /// /// The source of the event. /// The instance containing the event data. private void AutoSearcher_Enter(object sender, EventArgs e) { //没数据也进行隐藏 if (!string.IsNullOrEmpty(StrOld) && StrOld == this.Text || FListVAuto.GetTreeView.Nodes.Count==0) { FrmAutoShowForm.Hide(); return; } StrOld = this.Text; SetFrmPosition(this, true); FrmAutoShowForm.Show(); } /// /// 获取窗体 /// /// The CTR. /// Form. private Form GetForm(Control ctr) { if (ctr is Form) { return ctr as Form; } else { return GetForm(ctr.Parent); } } /// /// 在父窗体关闭的时候也关闭结果集窗体 /// /// /// private void ParentFormClose(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing) { FrmAutoShowForm.Hide(); if (FrmAutoShowForm.Parent != null) { (FrmAutoShowForm.Parent as Form).FormClosing -= new FormClosingEventHandler(ParentFormClose); } } } /// /// 设置窗体位置 /// /// /// private void SetFrmPosition(Control OwnerCtrl, bool bParentSet) { Form form = null; Control F; int iTop; int iLeft; int iWidth; form = GetForm(OwnerCtrl.Parent); FrmAutoShowForm.TopLevel = false; FrmAutoShowForm.Parent = form; for (int k = 0; k < 20; k++) FrmAutoShowForm.BringToFront(); F = OwnerCtrl.Parent; iTop = OwnerCtrl.Top + OwnerCtrl.Height; iLeft = OwnerCtrl.Left; while (F != form) { iTop = iTop + F.Top; iLeft = iLeft + F.Left; F = F.Parent; } if (iTop + this.Height + FrmAutoShowForm.Height > F.Height) { if (iTop - this.Height > F.Height - iTop) { if (FrmAutoShowForm.Height > iTop - OwnerCtrl.Height) { FrmAutoShowForm.Height = iTop - OwnerCtrl.Height; iTop = 0; } else iTop = iTop - this.Height - FrmAutoShowForm.Height; } else FrmAutoShowForm.Height = F.Height - iTop - 30; } if (iLeft + FrmAutoShowForm.Width > F.Width) { if (iLeft + this.Width > F.Width - iLeft) { if (FrmAutoShowForm.Width > iLeft + OwnerCtrl.Width) { FrmAutoShowForm.Width = iLeft + OwnerCtrl.Width; iLeft = 0; } else iLeft = iLeft + OwnerCtrl.Width - FrmAutoShowForm.Width; } else FrmAutoShowForm.Width = F.Width - iLeft - 30; } FrmAutoShowForm.Left = iLeft; FrmAutoShowForm.Top = iTop; } } }