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; namespace ControlLib.inh { #region AutoSearcherEditor /// /// 属性编辑器 /// public class AutoSearcherEditor : UITypeEditor { public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { if (context != null && context.Instance != null) { return UITypeEditorEditStyle.Modal; } return base.GetEditStyle(context); } public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { IWindowsFormsEditorService editorService = null; if (context != null && context.Instance != null && provider != null) { editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)); if (editorService != null && context.Instance != null) { //取出设计器所在的窗体(组件/控件所在窗体) //IDesignerHost host = (IDesignerHost)provider.GetService(typeof(IDesignerHost)); //if (host == null) return value; //context.Instance:可以得到当前的Component组件。 AutoSearcher control = (AutoSearcher)context.Instance; SearcherPropertyCollector spc = control.searcherPropertyCollector; if (spc == null) { spc = new SearcherPropertyCollector(); spc.FieldPropertyList = new TFieldPropertyData[0]; } inh.autosearcher_Res.FrmPropertySet dlg = new inh.autosearcher_Res.FrmPropertySet(spc); if (editorService.ShowDialog(dlg) == DialogResult.OK) { spc = dlg.searcherPropertyCollector; } dlg.Dispose(); //重新序列化内容到.Designer.cs文件 context.PropertyDescriptor.SetValue(context.Instance, value); //context.PropertyDescriptor.SetValue(context.Instance, value); return spc; } } return value; } } #endregion /// /// 收缩匹配模式 /// public enum TSearchMatchWay { swFuzzy = 0, //全模糊匹配 swLeftFuzzy = 1, //左边模糊匹配 swRightFuzzy = 2, //右边模糊匹配 swFullEqual = 3//相等 } #region TFieldPropertyData /// /// 字段属性结构 /// public class TFieldPropertyData { /// /// 字段描述 /// private string FFieldCaption = ""; public string FieldCaption { get { return FFieldCaption; } set { FFieldCaption = value; } } /// /// 字段在listview中显示的宽度 /// private int FFieldWidth = 0; public int FieldWidth { get { return FFieldWidth; } set { FFieldWidth = value; } } /// /// 字段名称 /// private string FFieldName = ""; public string FieldName { get { return FFieldName; } set { FFieldName = value; } } /// /// 是否在listview上显示该字段信息 /// private bool FShowColumn = true; public bool ShowColumn { get { return FShowColumn; } set { FShowColumn = value; } } } #endregion #region SearcherPropertyCollector /// /// 搜索控件属性集合器 /// public class SearcherPropertyCollector { private TFieldPropertyData[] FFieldPropertyList; public TFieldPropertyData[] FieldPropertyList// = new SortedList(); { get { return FFieldPropertyList; } set { FFieldPropertyList = value; } } //----------private---------------- /// /// 分析好的查询语句 /// string FQuerySQL = ""; public string QuerySQL { get { return FQuerySQL; } set { FQuerySQL = value; } } /// /// sql语句 /// String FSQL = ""; public string SQL { get { return FSQL; } set { FSQL = value; } } /// /// 初始化时候的SQL /// public string InitSQL { get; set; } /// /// 结果集主键 /// String FKeyField = ""; public string KeyField { get { return FKeyField; } set { FKeyField = value; } } /// /// 查询字段名称 /// String FQueryField = ""; public string QueryField { get { return FQueryField; } set { FQueryField = value; } } /// /// 查询字段名称 /// String FRemarkField = ""; public string RemarkField { get { return FRemarkField; } set { FRemarkField = value; } } /// /// 返回字段名称 /// String FReturnValueField = ""; public string ReturnValueField { get { return FReturnValueField; } set { FReturnValueField = value; } } /// /// 限制匹配最大数量 /// int FReturnMaxCount = 50; public int ReturnMaxCount { get { return FReturnMaxCount; } set { FReturnMaxCount = value; } } /// /// 是否转拼音 /// bool FConvertToChineseSpell = false; public bool ConvertToChineseSpell { get { return FConvertToChineseSpell; } set { FConvertToChineseSpell = value; } } /// /// 窗体显示的高度 /// int FShowFormHeight = 230; public int ShowFormHeight { get { return FShowFormHeight; } set { FShowFormHeight = value; } } /// /// 窗体显示的宽度 /// int FShowFormWidth = 230; public int ShowFormWidth { get { return FShowFormWidth; } set { FShowFormWidth = value; } } /// /// 匹配模式 /// TSearchMatchWay FSearchMatchWay = TSearchMatchWay.swFuzzy; public TSearchMatchWay SearchMatchWay { get { return FSearchMatchWay; } set { FSearchMatchWay = value; } } #region GetQuerySql public bool IsChina(string CString) { Console.WriteLine("{0}", CString.Length); bool BoolValue = false; for (int i = 0; i < CString.Length; i++) { if (Convert.ToInt32(Convert.ToChar(CString.Substring(i, 1))) < 128) { BoolValue = false; } else { BoolValue = true; } } return BoolValue; } /// /// 构造查询字符串 /// /// /// public string GetQuerySql(string QueryValue, string text) { string sWhere = ""; string sOrderBy = ""; string sGroupBy = ""; string QueryString = ""; string sTemp1 = ""; string sTemp2 = ""; string sTemp3 = ""; string QueryCondition = ""; int indexGroupBy; int indexOrderBy; char[] charArr; string strRet = ""; if (FRemarkField + "" == "") FRemarkField = FQueryField; FSQL = Regex.Replace(FSQL, "#[^##]+#", " 1=1 "); if (false) { strRet = "set nocount on " + FSQL; } else { QueryCondition = ""; switch (FSearchMatchWay) { case TSearchMatchWay.swFuzzy: { if (IsChina(text)) { foreach (TFieldPropertyData fpd in this.FieldPropertyList) { if (string.IsNullOrWhiteSpace(QueryCondition)) QueryCondition += fpd.FieldName + " like '%" + text + "%'"; else QueryCondition += " or "+fpd.FieldName + " like '%" + text + "%'"; } //QueryCondition = "(" + FQueryField + " like '%" + text + "%' or " + FRemarkField + " like '%" + text + "%') "; } else { //QueryCondition = "dbo.p_getpy(" + FQueryField + ") like '%" + QueryValue + "%'"; foreach (TFieldPropertyData fpd in this.FieldPropertyList) { if (string.IsNullOrWhiteSpace(QueryCondition)) QueryCondition += "dbo.p_getpy("+fpd.FieldName+")" + " like '%" + text + "%'"; else QueryCondition += " or dbo.p_getpy(" + fpd.FieldName + ") like '%" + text + "%'"; } //QueryCondition = "(dbo.p_getpy(" + FQueryField + ") like '%" + QueryValue + "%' or dbo.p_getpy(" + FRemarkField + ") like '%" + QueryValue + "%')"; } break; } case TSearchMatchWay.swLeftFuzzy: { if (IsChina(text)) { //QueryCondition = FQueryField + " like '%" + text + "' "; foreach (TFieldPropertyData fpd in this.FieldPropertyList) { if (string.IsNullOrWhiteSpace(QueryCondition)) QueryCondition += fpd.FieldName + " like '%" + text + "'"; else QueryCondition += " or " + fpd.FieldName + " like '%" + text + "'"; } //QueryCondition = "(" + FQueryField + " like '%" + text + "' or " + FRemarkField + " like '%" + text + "')"; } else { //QueryCondition = "dbo.p_getpy(" + FQueryField + ") like '%" + QueryValue + "'"; foreach (TFieldPropertyData fpd in this.FieldPropertyList) { if (string.IsNullOrWhiteSpace(QueryCondition)) QueryCondition += "dbo.p_getpy("+fpd.FieldName + ") like '%" + text + "'"; else QueryCondition += " or dbo.p_getpy(" + fpd.FieldName + ") like '%" + text + "'"; } //QueryCondition = "(dbo.p_getpy(" + FQueryField + ") like '%" + QueryValue + "' or dbo.p_getpy(" + FRemarkField + ") like '%" + QueryValue + "')"; } break; } case TSearchMatchWay.swRightFuzzy: { if (IsChina(text)) { //QueryCondition = FQueryField + " like '" + text + "%' "; foreach (TFieldPropertyData fpd in this.FieldPropertyList) { if (string.IsNullOrWhiteSpace(QueryCondition)) QueryCondition += fpd.FieldName + " like '" + text + "%'"; else QueryCondition += " or " + fpd.FieldName + " like '" + text + "%'"; } //QueryCondition = "(" + FQueryField + " like '" + text + "%' or " + FRemarkField + " like '" + text + "%') "; } else { //QueryCondition = "dbo.p_getpy(" + FQueryField + ") like '" + QueryValue + "%' "; foreach (TFieldPropertyData fpd in this.FieldPropertyList) { if (string.IsNullOrWhiteSpace(QueryCondition)) QueryCondition += "dbo.p_getpy(" + fpd.FieldName + ") like '" + text + "%'"; else QueryCondition += " or dbo.p_getpy(" + fpd.FieldName + ") like '" + text + "%'"; } //QueryCondition = "(dbo.p_getpy(" + FQueryField + ") like '" + QueryValue + "%' or dbo.p_getpy(" + FRemarkField + ") like '" + QueryValue + "%') "; } break; } case TSearchMatchWay.swFullEqual: { if (IsChina(text)) { //QueryCondition = FQueryField + " ='" + text + "' "; foreach (TFieldPropertyData fpd in this.FieldPropertyList) { if (string.IsNullOrWhiteSpace(QueryCondition)) QueryCondition += fpd.FieldName + " = '" + text + "'"; else QueryCondition += " or " + fpd.FieldName + " = '" + text + "'"; } //QueryCondition = "(" + FQueryField + " ='" + text + "' or " + FRemarkField + " ='" + text + "') "; } else { //QueryCondition = "dbo.p_getpy(" + FQueryField + ")='" + QueryValue + "'"; foreach (TFieldPropertyData fpd in this.FieldPropertyList) { if (string.IsNullOrWhiteSpace(QueryCondition)) QueryCondition += "dbo.p_getpy("+fpd.FieldName + ") = '" + text + "'"; else QueryCondition += " or dbo.p_getpy(" + fpd.FieldName + ") = '" + text + "'"; } //QueryCondition = "(dbo.p_getpy(" + FQueryField + ")='" + QueryValue + "' or dbo.p_getpy(" + FRemarkField + ")='" + QueryValue + "')"; } break; } } QueryCondition = "(" + QueryCondition + ")"; //MessageBox.Show(QueryCondition); QueryString = "select * from (" + FSQL + ") tab"; //MessageBox.Show(QueryString + " where " + QueryCondition); return QueryString + " where " + QueryCondition; //QueryString = FSQL; //int indexWhere = QueryString.IndexOf("where"); //if (indexWhere != -1)//有where条件的 //{ // sWhere = QueryString.Substring(indexWhere, QueryString.Length - indexWhere); // strRet = QueryString.Substring(0, indexWhere) + " where ("; // sTemp1 = sWhere; // sTemp1 = sTemp1.Remove(0, 5); // charArr = sTemp1.ToCharArray(); // Array.Reverse(charArr); // sTemp2 = new string(charArr); // sTemp1 = sWhere; // QueryString = sTemp2; // sTemp3 = sTemp1; // sTemp3 = sTemp3.Remove(0, 5); // indexGroupBy = QueryString.IndexOf("yb puorg"); // if (indexGroupBy != -1) { // sGroupBy = QueryString.Substring(indexGroupBy, QueryString.Length - indexGroupBy); // strRet = strRet + sTemp3.Substring(0, sGroupBy.Length - 9) + ") and " + QueryCondition + sTemp3.Substring(sTemp3.Length - sGroupBy.Length + 8, sTemp3.Length - sGroupBy.Length + 9); // } // else { // indexOrderBy = QueryString.IndexOf("yb redro"); // if (indexOrderBy != -1) { // sOrderBy = QueryString.Substring(indexOrderBy, QueryString.Length - indexOrderBy); // strRet = strRet + sTemp3.Substring(0, sOrderBy.Length - 9) + ") and " + QueryCondition + sTemp3.Substring(sTemp3.Length - sOrderBy.Length + 8, sTemp3.Length - sOrderBy.Length + 9); // } // else // strRet = strRet + sTemp3 + ") and " + QueryCondition; // } //} //else { //--没有where条件的 sTemp1 = QueryString; charArr = sTemp1.ToCharArray(); Array.Reverse(charArr); sTemp2 = new string(charArr); QueryString = sTemp2; indexGroupBy = QueryString.IndexOf("yb puorg"); if (indexGroupBy != -1)//--有group by { sGroupBy = QueryString.Substring(indexGroupBy, QueryString.Length - indexGroupBy); strRet = sTemp1.Substring(0, sGroupBy.Length - 8) + " where " + QueryCondition + sTemp1.Substring(sTemp1.Length - sGroupBy.Length + 7, sTemp1.Length - sGroupBy.Length + 8); } else {//--没有group by indexOrderBy = QueryString.IndexOf("yb redro"); if (indexOrderBy != -1)//--有order by { sOrderBy = QueryString.Substring(indexOrderBy, QueryString.Length - indexOrderBy); strRet = sTemp1.Substring(0, sOrderBy.Length - 8) + " where " + QueryCondition + sTemp1.Substring(sTemp1.Length - sOrderBy.Length + 7, sTemp1.Length - sOrderBy.Length + 8); } else strRet = sTemp1 + " where " + QueryCondition; } strRet = "set nocount on " + strRet; //} } //MessageBox.Show(strRet); return strRet.Replace("50", "2000"); //return strRet; } #endregion } #endregion /// /// 自动搜索器 /// public partial class AutoSearcher : TextBox { //-----------------------属性--------------------------------------- SearcherPropertyCollector FSearcherPropertyCollector; [Browsable(true)] [Editor(typeof(AutoSearcherEditor), typeof(UITypeEditor))] public SearcherPropertyCollector searcherPropertyCollector { get { return FSearcherPropertyCollector; } set { FSearcherPropertyCollector = value; } } /// /// 文本框改变之前 /// public event EventHandler OnTextChangedBefore; public void testFun() { ControlLib.inh.autosearcher_Res.FrmPropertySet ss = new autosearcher_Res.FrmPropertySet(FSearcherPropertyCollector); ss.ShowDialog(); } //---------------------------公开字段----------------------- public bool bNotCheck = false; public string ReturnValue = ""; public string addDllModuleId = ""; public string addModuleCaption = ""; public string addDllName = ""; public string addResultValue = ""; public string OperatorName = ""; public string OperatorId = ""; //默认分类 智能搜索组件添加时用 public string SpeciesNo = "01"; /// /// 控件所在的面板 /// public Control SearchControlPanel { get; set; } //-----------------------------------------------私有字段--------------- private string strPreWords = ""; private string stText = ""; private int iQueryFieldSeq = -1; private List m_FieldList; private ListView FListVAuto; private string StrOld = ""; private FrmAutoSearch FrmAutoShowForm; private bool FbSetOldEvent = false; private int iReturnFieldSeq = -1; private Thread TheadHandle = null; private bool bChanged = false; /// /// 构造函数 /// public AutoSearcher() { Init(); } /// /// 析构函数 /// ~AutoSearcher() { FSearcherPropertyCollector = null; if (m_FieldList != null) m_FieldList.Clear(); m_FieldList = null; if (FrmAutoShowForm != null) { FrmAutoShowForm.CanClose = true; FrmAutoShowForm.Close(); } FrmAutoShowForm = null; if (TheadHandle != null) TheadHandle.Abort(); TheadHandle = null; } /// /// 初始化函数 /// private void Init() { Text = ""; FrmAutoShowForm = new FrmAutoSearch(); FListVAuto = FrmAutoShowForm.ListV; FListVAuto.Click += new EventHandler(OnListVClick); FListVAuto.KeyUp += new KeyEventHandler(OnProKeyUp); this.Leave += new EventHandler(OnSelfLostFocus); this.KeyUp += new KeyEventHandler(onSelfKeyUp); this.Enter += new EventHandler(AutoSearcher_Enter); this.MouseDown += new MouseEventHandler(AutoSearcher_MouseDown); this.MouseUp += new MouseEventHandler(AutoSearcher_MouseUp); m_FieldList = new List(); } void AutoSearcher_Enter(object sender, EventArgs e) { if (!string.IsNullOrEmpty(StrOld)) return; if ((sender as AutoSearcher).ReadOnly) return; if (OnTextChangedBefore != null) OnTextChangedBefore(sender, e); SetFormPos(FrmAutoShowForm.ListV, this, true); stText = "";// Text.Trim(); StrOld = stText; FrmAutoShowForm.Show(); if (TheadHandle == null) { TheadHandle = new Thread(QueryFillList); TheadHandle.IsBackground = true; TheadHandle.Start(FrmAutoShowForm.ListV); //QueryFillList(FrmAutoShowForm.ListV); } else bChanged = true; this.Focus(); } void AutoSearcher_MouseDown(object sender, MouseEventArgs e) { if ((sender as AutoSearcher).ReadOnly) return; if (OnTextChangedBefore != null) OnTextChangedBefore(sender, e); SetFormPos(FrmAutoShowForm.ListV, this, true); stText = "";// Text.Trim(); StrOld = stText; FrmAutoShowForm.Show(); if (TheadHandle == null) { TheadHandle = new Thread(QueryFillList); TheadHandle.IsBackground = true; TheadHandle.Start(FrmAutoShowForm.ListV); //QueryFillList(FrmAutoShowForm.ListV); } else bChanged = true; this.Focus(); } void AutoSearcher_MouseUp(object sender, MouseEventArgs e) { this.SelectAll(); } /// /// 输入内容变化处理过程 /// /// protected override void OnTextChanged(EventArgs e) { //if (DesignMode)MessageBox.Show("设计状态"); if (OnTextChangedBefore != null) OnTextChangedBefore(this, e); if (DesignMode == false && bNotCheck == false && this.Focused) { try { if (!SetFormPos(FrmAutoShowForm.ListV, this, true)) return; //MessageBox.Show("OK"); stText = Text.Trim(); StrOld = stText; //FrmAutoShowForm.Show(); if (TheadHandle == null) { TheadHandle = new Thread(QueryFillList); TheadHandle.IsBackground = true; TheadHandle.Start(FrmAutoShowForm.ListV); //QueryFillList(FrmAutoShowForm.ListV); } else bChanged = true; this.Focus(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } SetAutoSearchWidth(); base.OnTextChanged(e); } private void SetAutoSearchWidth() { try { int parentWidth = 0; for (int i = 0; i < FrmAutoShowForm.widthList.Count; i++) { ListView lv = FrmAutoShowForm.ListV; if (i == 0) lv.Columns[i].Width = 0; else { lv.Columns[i].Width = FrmAutoShowForm.widthList[i]; lv.Columns[i].Width = -1; } parentWidth += FrmAutoShowForm.widthList[i]; } if (parentWidth > 0 && FrmAutoShowForm.Width < parentWidth) FrmAutoShowForm.Width = parentWidth; } catch (Exception) { } } private void AddShowColumn(ListView listv, string caption, int w) { ColumnHeader header = new ColumnHeader(); header.Text = caption; header.Width = w; listv.Columns.Add(header); } private string MakeSpellCode(string strSrc, int max) { return SpellCodeFunction.MakeSpellCode(strSrc, max); } #region QueryFillList /// /// 线程处理过程 /// /// private void QueryFillList(object obj) { ListView ListV = (ListView)obj; int i; int iCounter; ListViewItem Item; String ResValue; Boolean bMatched; FrmAutoShowForm.Invoke(new FrmAutoSearch.FunctionNameNoParams(FrmAutoShowForm.StartQuerying)); do { bChanged = false; try { iCounter = 0; if (FSearcherPropertyCollector.ConvertToChineseSpell) stText = MakeSpellCode(stText, 1000); //if (ListV.Items.Count > 0 && strPreWords != "" && stText.StartsWith(strPreWords) // && (FSearcherPropertyCollector.SearchMatchWay != TSearchMatchWay.swRightFuzzy) && // (FSearcherPropertyCollector.SearchMatchWay != TSearchMatchWay.swFullEqual)) //{ // i = 0; // while (i < ListV.Items.Count) // { // if (this.InvokeRequired) // { // Item = (ListViewItem)FrmAutoShowForm.Invoke(new FrmAutoSearch.FunctionName(FrmAutoShowForm.DoGetItem), new object[] { i }); // } // else // { // Item = ListV.Items[i]; // } // if (FSearcherPropertyCollector.ConvertToChineseSpell) // { // ResValue = MakeSpellCode(Item.SubItems[iQueryFieldSeq].Text, 1000); // } // else // { // ResValue = Item.SubItems[iQueryFieldSeq].Text; // } // //bMatched = false; // //switch (FSearcherPropertyCollector.SearchMatchWay) // //{ // // case TSearchMatchWay.swFuzzy: // // { // // bMatched = ResValue.IndexOf(stText) != -1; // // break; // // } // // case TSearchMatchWay.swLeftFuzzy: // // { // // bMatched = ResValue.StartsWith(stText); // // break; // // } // //} // //if (!bMatched) // //{ // if (this.InvokeRequired) // { // Action dRemoveAt = ListV.Items.RemoveAt; // this.FListVAuto.Invoke(dRemoveAt, i); // } // else // { // ListV.Items.RemoveAt(i); // } // //} // //else // //{ // i++; // //} // } //} //else //{ string strSql = FSearcherPropertyCollector.GetQuerySql(stText, this.Text); //MessageBox.Show( strSql ); //SqlCommand cmd = new SqlCommand(); //cmd.Connection = conn; //cmd.CommandType = CommandType.Text; //cmd.CommandText = strSql; this.Invoke(new EventHandler(delegate { ListV.BeginUpdate(); })); using (SqlDataReader dr = SqlHelper.ExecuteReader(CommandType.Text, strSql, null)) { if (this.InvokeRequired) { Action dClear = ListV.Items.Clear; this.FListVAuto.Invoke(dClear); } else { ListV.Items.Clear(); } while (dr.Read() && iCounter < FSearcherPropertyCollector.ReturnMaxCount) { // 去除匹配模式 //bMatched = false; //if (FSearcherPropertyCollector.ConvertToChineseSpell && stText.Trim() != "") { // ResValue = MakeSpellCode(dr[FSearcherPropertyCollector.QueryField].ToString(), 1000); // switch (FSearcherPropertyCollector.SearchMatchWay) { // case TSearchMatchWay.swFuzzy: { // bMatched = ResValue.IndexOf(stText) != -1; // break; // } // case TSearchMatchWay.swLeftFuzzy: { // bMatched = ResValue.StartsWith(stText); // break; // } // case TSearchMatchWay.swRightFuzzy: { // bMatched = ResValue.EndsWith(stText); // break; // } // case TSearchMatchWay.swFullEqual: { // bMatched = stText == ResValue; // break; // } // } //} //else { // ResValue = dr[FSearcherPropertyCollector.QueryField].ToString(); // bMatched = true; //} //--add matched data //if (bMatched) { Item = new ListViewItem(); Item.Text = ""; for (i = 0; i < m_FieldList.Count; i++) { Item.SubItems.Add(dr[m_FieldList[i]].ToString()); } if (this.InvokeRequired) { FrmAutoShowForm.Invoke(new FrmAutoSearch.FunctionName(FrmAutoShowForm.AddListVItem), new object[] { Item }); } else { ListV.Items.Add(Item); } iCounter++; //} } } this.Invoke(new EventHandler(delegate { ListV.EndUpdate(); })); //cmd.Dispose(); //cmd = null; //} FrmAutoShowForm.Invoke(new FrmAutoSearch.FunctionNameNoParams(FrmAutoShowForm.DoSelectItem)); if (FSearcherPropertyCollector.ConvertToChineseSpell) { strPreWords = stText; if (iCounter == FSearcherPropertyCollector.ReturnMaxCount) strPreWords = ""; } } catch (Exception ex) { MessageBox.Show(ex.Message); } } while (bChanged); TheadHandle = null; FrmAutoShowForm.Invoke(new FrmAutoSearch.FunctionNameNoParams(FrmAutoShowForm.EndQuerying)); SetAutoSearchWidth(); } #endregion /// /// 在父窗体关闭的时候也关闭结果集窗体 /// /// /// 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); //FrmAutoShowForm.Parent.Controls.Remove(FrmAutoShowForm); } //FrmAutoShowForm.Parent = null; } } #region SetFormPos /// /// 设置窗体位置 /// /// /// /// /// private bool SetFormPos(ListView ShowCtrl, Control OwnerCtrl, bool bParentSet) { Control F; Form form = null; int iTop; int iLeft; int iWidth; String strInputValue = (OwnerCtrl as TextBox).Text.Trim(); bool bRet = false; //--在原来的基础上不断加上空格不管 //comment //if (ReturnValue != string.Empty && strInputValue != string.Empty && strInputValue == StrOld) // return bRet; //comment // 叠词输入也要搜索例如:StrOld为x,在输入x时需要搜索 //if (strInputValue.StartsWith(StrOld) && StrOld != "" && // ShowCtrl.Items.Count <= 0 && FSearcherPropertyCollector.SearchMatchWay != TSearchMatchWay.swRightFuzzy // && FSearcherPropertyCollector.SearchMatchWay != TSearchMatchWay.swFullEqual) // return bRet; //comment //if (ReturnValue != string.Empty && ShowCtrl.Items.Count > 0 && strInputValue == StrOld) // return bRet; //comment //if (conn == null) { // MessageBox.Show("数据库连接器不能为空!", "Error"); // return bRet; //} //if (conn.State == ConnectionState.Closed) { // MessageBox.Show("数据库连接器不能为关闭状态!", "Error"); // return bRet; //} if (FSearcherPropertyCollector.SQL == string.Empty) { MessageBox.Show("没有配置有查询语句!", "Error"); return bRet; } if (ShowCtrl.Columns.Count == 0) { FrmAutoShowForm.Height = FSearcherPropertyCollector.ShowFormHeight; FrmAutoShowForm.Width = FSearcherPropertyCollector.ShowFormWidth; iWidth = 0; AddShowColumn(ShowCtrl, "", 1); for (int i = 0; i < FSearcherPropertyCollector.FieldPropertyList.Length; i++) { TFieldPropertyData kv = FSearcherPropertyCollector.FieldPropertyList[i]; if (kv.ShowColumn) { AddShowColumn(ShowCtrl, kv.FieldCaption, kv.FieldWidth); iWidth = iWidth + kv.FieldWidth; m_FieldList.Add(kv.FieldName); if (kv.FieldName == FSearcherPropertyCollector.ReturnValueField) iReturnFieldSeq = m_FieldList.Count;//第一列抛开不算 if (kv.FieldName == FSearcherPropertyCollector.QueryField) iQueryFieldSeq = m_FieldList.Count;//第一列抛开不算 } } if (FSearcherPropertyCollector.ShowFormWidth == 0) FrmAutoShowForm.Width = iWidth + 20; } try { //ReturnValue = ""; form = GetForm(OwnerCtrl.Parent); if (form != null) //--如果没有输入任何信息也要查询 { if (!FbSetOldEvent && bParentSet) { (form as Form).FormClosing += new FormClosingEventHandler(ParentFormClose); FrmAutoShowForm.TopLevel = false; FrmAutoShowForm.Parent = form; //form.Controls.Add(FrmAutoShowForm); for (int k = 0; k < 20; k++) FrmAutoShowForm.BringToFront(); FbSetOldEvent = true; } 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; FrmAutoShowForm.addDllName = addDllName; FrmAutoShowForm.addModuleId = addDllModuleId; FrmAutoShowForm.FormText = addModuleCaption; FrmAutoShowForm.OperatorId = OperatorId; FrmAutoShowForm.OperatorName = OperatorName; FrmAutoShowForm.SpeciesNo = SpeciesNo; FrmAutoShowForm.addResultValue = addResultValue; FrmAutoShowForm.autos = this; bRet = true; } //FrmAutoShowForm.Width = 400; } catch (Exception ex) { MessageBox.Show(ex.Message); } return bRet; } #endregion private Form GetForm(Control ctr) { if (ctr is Form) { return ctr as Form; } else { return GetForm(ctr.Parent); } } /// /// 点击数据集listview控件事件处理 /// /// /// private void OnListVClick(object sender, EventArgs e) { if (FListVAuto.Items.Count <= 0 || FListVAuto.SelectedItems.Count <= 0) return; ReturnValue = FListVAuto.SelectedItems[0].SubItems[iReturnFieldSeq].Text; bNotCheck = true; Text = FListVAuto.SelectedItems[0].SubItems[iQueryFieldSeq].Text; StrOld = Text; bNotCheck = false; FrmAutoShowForm.Hide(); //added by dl 2015-6-29 Control ctl = this.Parent.Parent; if (ctl != null) { //this.Parent.Parent.Parent.SelectNextControl(this, true, false, true, false); try { //Control ct = this.Parent.Parent.Parent.GetNextControl(this, true); //if (ct != null) // ct.Focus(); } catch (Exception) { } ctl.Focus(); //MessageBox.Show(ct.Name); } SetEventActive(); this.OnTextChanged(e); // 选择表格数据后,通知文本框数据已经改变 } /// /// 数据集listview控件回车事件处理 /// /// /// private void OnProKeyUp(object sender, KeyEventArgs e) { switch (e.KeyValue) { case 13: { try { if (FListVAuto.Items.Count <= 0 && FListVAuto.SelectedItems.Count <= 0) return; ReturnValue = FListVAuto.SelectedItems[0].SubItems[iReturnFieldSeq].Text; bNotCheck = true; Text = FListVAuto.SelectedItems[0].SubItems[iQueryFieldSeq].Text; StrOld = Text; bNotCheck = false; FrmAutoShowForm.Hide(); //added by dl 2015-6-29 Control ctl = this.Parent.Parent; if (ctl != null) this.Parent.Parent.Parent.SelectNextControl(this, true, false, true, false); SetEventActive(); this.OnTextChanged(e); // 选择表格数据后,通知文本框数据已经改变 ctl.Focus(); } catch (Exception) { } break; } case 27: { FrmAutoShowForm.Hide(); break; } } } public bool IsEmpet { get; set; } public bool IsClear = true; /// /// 输入控件失去焦点事件处理 /// /// /// private void OnSelfLostFocus(object sender, EventArgs e) { if (!FListVAuto.Focused && ReturnValue == "" && this.Text == "") { bNotCheck = true; if (IsClear) this.Text = ""; bNotCheck = false; } if (FrmAutoShowForm.Visible && !FListVAuto.Focused) { //ReturnValue = ""; SetValueSilent("", this.Text); FrmAutoShowForm.Hide(); SetEventActive(); } if ((this.Text != "") && (this.Enabled) && (this.ReadOnly)) { //ReturnValue = ""; SetValueSilent("", this.Text); } } /// 输入框回车事件的处理 /// /// /// private void onSelfKeyUp(object sender, KeyEventArgs e) { if (e.KeyValue == 13) { if (Text.Trim() == "") OnTextChanged(null); if (FrmAutoShowForm.Visible) FListVAuto.Focus(); } if (e.KeyValue == 27) { if (FrmAutoShowForm.Visible) FrmAutoShowForm.Visible = false; } } /// /// 定义事件类型 /// /// /// public delegate void GetReturnValueEventHandler(object sender, EventArgs e); /// /// 定义事件变量 /// public event GetReturnValueEventHandler GetReturnValue; /// /// 触发事件,通知外部方法已经得到数据 /// private void SetEventActive() { if (GetReturnValue != null) GetReturnValue(this, new EventArgs()); } /// /// 不弹出搜索窗口模式下设置字符串和值 /// /// /// public void SetValueSilent(string sValue, string sText) { bool preV = this.bNotCheck; this.bNotCheck = true; Text = sText.Trim(); ReturnValue = sValue.Trim(); if (ReturnValue == "" && Text != "") { try { bool preConvert = FSearcherPropertyCollector.ConvertToChineseSpell; FSearcherPropertyCollector.ConvertToChineseSpell = false; string strSql = FSearcherPropertyCollector.SQL.Replace("top 50", "top 500"); strSql = Regex.Replace(strSql, "#[^##]+#", " 1=1 "); if (!string.IsNullOrEmpty(strSql)) { if (strSql.IndexOf("order by", StringComparison.OrdinalIgnoreCase) != -1) { int indexOrder = strSql.IndexOf("order by", StringComparison.OrdinalIgnoreCase); string sOrder = string.Empty; if (indexOrder != -1) { sOrder = strSql.Substring(indexOrder, strSql.Length - indexOrder); strSql = "select * from (" + strSql.Substring(0, indexOrder - 1) + ") as t where " + FSearcherPropertyCollector.QueryField + "='" + sText + "' " + sOrder; } } else { strSql = "select * from (" + strSql + ") as t where " + FSearcherPropertyCollector.QueryField + "='" + sText + "'"; } } Text = ""; FSearcherPropertyCollector.ConvertToChineseSpell = preConvert; DataTable dt = SqlHelper.ExecuteDataSet(strSql).Tables[0]; if ((dt != null) && (dt.Rows.Count > 0)) { ReturnValue = dt.Rows[0][FSearcherPropertyCollector.ReturnValueField].ToString(); SetValueSilent(ReturnValue, ""); } } catch (Exception ex) { //MessageBox.Show(ex.Message); } } else if (ReturnValue != "") { try { bool preConvert = FSearcherPropertyCollector.ConvertToChineseSpell; FSearcherPropertyCollector.ConvertToChineseSpell = false; string strSql = FSearcherPropertyCollector.SQL; strSql = Regex.Replace(strSql, "#[^##]+#", " 1=1 "); if (!string.IsNullOrEmpty(strSql)) { if (strSql.IndexOf("order by", StringComparison.OrdinalIgnoreCase) != -1) { int indexOrder = strSql.IndexOf("order by", StringComparison.OrdinalIgnoreCase); string sOrder = string.Empty; if (indexOrder != -1) { sOrder = strSql.Substring(indexOrder, strSql.Length - indexOrder); strSql = "select * from (" + strSql.Substring(0, indexOrder - 1) + ") as t where " + FSearcherPropertyCollector.KeyField + "='" + ReturnValue + "' " + sOrder; } } else { strSql = "select * from (" + strSql + ") as t where " + FSearcherPropertyCollector.KeyField + "='" + ReturnValue + "'"; } } FSearcherPropertyCollector.ConvertToChineseSpell = preConvert; DataTable dt = SqlHelper.ExecuteDataSet(strSql).Tables[0]; if ((dt != null) && (dt.Rows.Count > 0)) { this.Text = dt.Rows[0][FSearcherPropertyCollector.QueryField].ToString(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } this.bNotCheck = preV; } } }