324 lines
10 KiB
C#
324 lines
10 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// 自动搜索器
|
|
/// </summary>
|
|
public partial class AutoSearcherTreeView : TextBox
|
|
{
|
|
|
|
/// <summary>
|
|
/// 控件所在的面板
|
|
/// </summary>
|
|
//public Control SearchControlPanel { get; set; }
|
|
//-----------------------------------------------私有字段--------------
|
|
//树视图控件
|
|
private TreeViewEx FListVAuto;
|
|
//记载上次的选中值
|
|
private string StrOld = "";
|
|
//窗体
|
|
public FrmAutoSearchTreeView FrmAutoShowForm;
|
|
//树节点绑定的值
|
|
public string ValueMember;
|
|
//树节点显示的值
|
|
public string DisplayMember;
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
public AutoSearcherTreeView()
|
|
{
|
|
Init();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 初始化函数
|
|
/// </summary>
|
|
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();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 鼠标按下
|
|
/// </summary>
|
|
private void AutoSearcher_MouseDown(object sender, MouseEventArgs e)
|
|
{
|
|
//this.Text = "";// Text.Trim();
|
|
StrOld = this.Text;
|
|
FrmAutoShowForm.Show();
|
|
this.Focus();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 输入控件失去焦点事件处理
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void OnSelfLostFocus(object sender, EventArgs e)
|
|
{
|
|
if (FrmAutoShowForm.Visible && !FListVAuto.Focused)
|
|
{
|
|
FrmAutoShowForm.Hide();
|
|
}
|
|
}
|
|
|
|
|
|
/// 输入框回车事件的处理
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
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;
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 绑定树结构
|
|
/// </summary>
|
|
/// <param name="TabName"></param>
|
|
/// <param name="treeView"></param>
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 搜索节点递归
|
|
/// </summary>
|
|
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;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 树点击事件
|
|
/// </summary>
|
|
private void TreeViewClick(object sender, TreeViewEventArgs e)
|
|
{
|
|
this.Text = e.Node.Text;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 输入内容变化处理过程
|
|
/// </summary>
|
|
/// <param name="e"></param>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 键入事件
|
|
/// </summary>
|
|
/// <param name="sender">The source of the event.</param>
|
|
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
|
|
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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取窗体
|
|
/// </summary>
|
|
/// <param name="ctr">The CTR.</param>
|
|
/// <returns>Form.</returns>
|
|
private Form GetForm(Control ctr)
|
|
{
|
|
if (ctr is Form)
|
|
{
|
|
return ctr as Form;
|
|
}
|
|
else
|
|
{
|
|
return GetForm(ctr.Parent);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 在父窗体关闭的时候也关闭结果集窗体
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 设置窗体位置
|
|
/// </summary>
|
|
/// <param name="OwnerCtrl"></param>
|
|
/// <param name="bParentSet"></param>
|
|
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;
|
|
}
|
|
}
|
|
} |