/* * 實現功能: * 1.輸入字符長度超過設定值後,篩選出包含輸入內容的項目顯示在控件下(上)面的列表框中 * 2.得到焦點時在控件下(上)面的列表框中顯示最近的輸入內容(區分不一樣程序/不一樣窗體/不一樣控件/不一樣用戶) * 未實現功能:清理超過必定數量的歷史輸入記錄 使用方法: 1. 建立歷史輸入表 CREATE TABLE [dbo].[UserInputHistory] ( [ProductName] VARCHAR (255) NULL, [FormName] VARCHAR (255) NULL, [ControlName] VARCHAR (255) NULL, [UserID] VARCHAR (255) NULL, [InputText] VARCHAR (255) NULL, [ID] INT IDENTITY (1, 1) NOT NULL, CONSTRAINT [PK_UserInputHistory] PRIMARY KEY ([ID]) ); 2.在設計時填寫控件屬性附加類別裏的內容; 3.在Form_Load里加入Init(); */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Text;
using System.Threading;數據庫
namespace werp
{
public class InputHistoryAndFilterTextBox : System.Windows.Forms.TextBox
{服務器
/*
* 實現功能:
* 1.輸入字符長度超過設定值後,篩選出包含輸入內容的項目顯示在控件下(上)面的列表框中
* 2.得到焦點時在控件下(上)面的列表框中顯示最近的輸入內容(區分不一樣程序/不一樣窗體/不一樣控件/不一樣用戶)
* 未實現功能:清理超過必定數量的歷史輸入記錄
使用方法:
1. 建立歷史輸入表
CREATE TABLE [dbo].[UserInputHistory] (
[ProductName] VARCHAR (255) NULL,
[FormName] VARCHAR (255) NULL,
[ControlName] VARCHAR (255) NULL,
[UserID] VARCHAR (255) NULL,
[InputText] VARCHAR (255) NULL,
[ID] INT IDENTITY (1, 1) NOT NULL,
CONSTRAINT [PK_UserInputHistory] PRIMARY KEY ([ID])
);ide
2.在設計時填寫控件屬性附加類別裏的內容;
3.在Form_Load里加入Init();
*/
/// <summary>
/// 查詢的表名
/// </summary>
/// <value>The name of the table.</value>
[Description("查詢的表名"), Category("附加"), DefaultValue("t_item")]
public string TableName { get; set; } = "t_item";
/// <summary>
/// 篩選的字段名
/// </summary>
/// <value>The name of the field.</value>
[Description("篩選的字段名"), Category("附加"), DefaultValue("item_num")]
public string FieldName { get; set; } = "item_num";
/// <summary>
/// 篩選起始長度
/// </summary>
/// <value>The length of the find.</value>
[Description("篩選起始長度"), Category("附加"), DefaultValue(3)]
public int FindLength { get; set; } = 3;
/// <summary>
/// 附件查詢條件
/// </summary>
/// <value>The addi conditions.</value>
[Description("附件查詢條件"), Category("附加"), DefaultValue("")]
public string AddiConditions { get; set; } = "";
/// <summary>
/// 數據庫名稱
/// </summary>
/// <value>The name of the database.</value>
[Description("數據庫名稱"), Category("附加"), DefaultValue("wsprint")]
public string DatabaseName { get; set; } = "wsprint";
/// <summary>
/// 數據庫用戶名稱
/// </summary>
/// <value>The uid.</value>
[Description("數據庫用戶名稱"), Category("附加"), DefaultValue("sa")]
public string UID { get; set; } = "sa";
/// <summary>
/// 數據庫用戶密碼.
/// </summary>
/// <value>The password.</value>
[Description("數據庫用戶密碼"), Category("附加"), DefaultValue("sa")]
public string PWD { get; set; } = "sa";
/// <summary>
/// 數據庫服務器名稱或IP
/// </summary>
/// <value>The name of the serve.</value>
[Description("數據庫服務器名稱或IP"), Category("附加"), DefaultValue("xx-erpsvr")]
public string ServeName { get; set; } = "xx-erpsvr";ui
[Description("保留歷史輸入數量"), Category("附加"), DefaultValue(10)]
public int KeepHistorys { get; set; } = 10;this
[Description("容許歷史輸入記錄"), Category("附加"), DefaultValue(false)]
public bool EnabledHistory { get; set; } = false;
private System.Windows.Forms.ListBox ItemsListBox { get; set; } = new System.Windows.Forms.ListBox();
/// <summary>
/// 記錄歷史操做的用戶名
/// </summary>
/// <value>The user identifier.</value>
[Description("記錄歷史操做的用戶名"), Category("附加"), DefaultValue("")]
public string UserId { get; set; } = "";spa
System.Data.DataTable dt = new System.Data.DataTable(), dth = new System.Data.DataTable();
System.Data.SqlClient.SqlConnection conn;
System.Data.SqlClient.SqlCommand cmd;
public InputHistoryAndFilterTextBox()
{
}
public void Init()
{設計
ItemsListBox.Parent = this.Parent;
ItemsListBox.Top = this.Top + this.Height;
if (ItemsListBox.Top + ItemsListBox.Height > this.Parent.Height)
ItemsListBox.Top = this.Top - ItemsListBox.Height;
ItemsListBox.Left = this.Left;
ItemsListBox.Width = this.Width;
ItemsListBox.Visible = false;
ItemsListBox.BringToFront();code
conn = new System.Data.SqlClient.SqlConnection("Data Source=" + ServeName + ";Initial Catalog=" + DatabaseName + ";Connect Timeout=30;UID = " + UID + ";PWD=" + PWD);
cmd = new System.Data.SqlClient.SqlCommand();
cmd.Connection = conn;
this.TextChanged += FilterTextBox_TextChanged;
this.LostFocus += FilterTextBox_LostFocus;
this.KeyDown += FilterTextBox_KeyDown;
Thread thread = new Thread(LoadData);
ItemsListBox.LostFocus += ItemsListBox_LostFocus;
ItemsListBox.MouseDoubleClick += ItemsListBox_MouseDoubleClick;
this.Validated += FilterTextBox_Validated;
this.GotFocus += FilterTextBox_GotFocus;
thread.Start();
}orm
private void FilterTextBox_GotFocus(object sender, EventArgs e)
{
if (dth.Rows.Count > 0 && this.Text.Trim() == "")
{
ItemsListBox.Items.Clear();
foreach (System.Data.DataRow row in dth.Rows)
{
ItemsListBox.Items.Add(row[0].ToString());
}blog
ItemsListBox.Visible = true;
}
}
private void FilterTextBox_Validated(object sender, EventArgs e)
{
if (this.Text.Trim().Length >= this.FindLength)
{
try
{
cmd.CommandText = string.Format("insert into UserInputHistory (ProductName,FormName,ControlName,UserID,InputText) values ('{0}','{1}','{2}','{3}','{4}')",
this.ProductName, this.FindForm().Name, this.Name, UserId, this.Text);
if (cmd.Connection.State == System.Data.ConnectionState.Closed) cmd.Connection.Open();
if(cmd.ExecuteNonQuery()>0)
{
Thread thread = new Thread(LoadInputData);
thread.Start();
}
}
catch
{
throw;
}
finally
{
cmd.Connection.Close();
}
}
}
private void ItemsListBox_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (ItemsListBox.SelectedItem != null)
{
this.Text = ItemsListBox.SelectedItem.ToString();
}
ItemsListBox.Visible = false;
this.Focus();
}
private void ItemsListBox_LostFocus(object sender, EventArgs e)
{
ItemsListBox.Visible = false;
}
private void FilterTextBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
switch (e.KeyCode)
{
case System.Windows.Forms.Keys.Up:
if (ItemsListBox.SelectedIndex > 0)
{
ItemsListBox.SelectedIndex -= 1;
}
break;
case System.Windows.Forms.Keys.Down:
if (ItemsListBox.SelectedIndex < ItemsListBox.Items.Count - 1)
ItemsListBox.SelectedIndex += 1;
break;
case System.Windows.Forms.Keys.Enter:
if (ItemsListBox.SelectedItem != null)
{
this.Text = ItemsListBox.SelectedItem.ToString();
}
ItemsListBox.Visible = false;
break;
}
}
private void FilterTextBox_LostFocus(object sender, EventArgs e)
{
if (!ItemsListBox.Focused)
ItemsListBox.Visible = false;
}
private void FilterTextBox_TextChanged(object sender, EventArgs e)
{
string text = this.Text.Trim();
if (text.Length >= FindLength)
{
ItemsListBox.Items.Clear();
foreach (var row in dt.Select(FieldName + " like '%" + text + "%'", FieldName))
{
ItemsListBox.Items.Add(row[0].ToString());
}
ItemsListBox.Visible = true;
}
else
{
ItemsListBox.Visible = false;
}
}
private void LoadData()
{
try
{
//載入篩選內容
cmd.CommandText = "select " + FieldName + " from " + TableName + (AddiConditions.Trim() == "" ? "" : " where ") + AddiConditions + " group by " + FieldName;
new System.Data.SqlClient.SqlDataAdapter(cmd).Fill(dt);
LoadInputData();
}
catch (Exception ex)
{
throw new Exception("使用前請先設置鏈接屬性" + ex.Message);
}
}
private void LoadInputData()
{
try
{
//載入歷史輸入內容
cmd.CommandText = string.Format("select distinct TOP {4} InputText from UserInputHistory where ProductName = '{0}' and FormName = '{1}' and ControlName = '{2}' and UserID = '{3}' order by ID desc ", ProductName, FindForm().Name, Name, UserId, KeepHistorys);
new System.Data.SqlClient.SqlDataAdapter(cmd).Fill(dth);
}
catch (Exception ex)
{
throw new Exception("使用前請先設置鏈接屬性" + ex.Message);
}
}
}
}
using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.Design; using System.Text; using System.Threading; namespace werp { public class InputHistoryAndFilterTextBox : System.Windows.Forms.TextBox { /* * 實現功能: * 1.輸入字符長度超過設定值後,篩選出包含輸入內容的項目顯示在控件下(上)面的列表框中 * 2.得到焦點時在控件下(上)面的列表框中顯示最近的輸入內容(區分不一樣程序/不一樣窗體/不一樣控件/不一樣用戶) * 未實現功能:清理超過必定數量的歷史輸入記錄 使用方法: 1. 建立歷史輸入表 CREATE TABLE [dbo].[UserInputHistory] ( [ProductName] VARCHAR (255) NULL, [FormName] VARCHAR (255) NULL, [ControlName] VARCHAR (255) NULL, [UserID] VARCHAR (255) NULL, [InputText] VARCHAR (255) NULL, [ID] INT IDENTITY (1, 1) NOT NULL, CONSTRAINT [PK_UserInputHistory] PRIMARY KEY ([ID]) ); 2.在設計時填寫控件屬性附加類別裏的內容; 3.在Form_Load里加入Init(); */ /// <summary> /// 查詢的表名 /// </summary> /// <value>The name of the table.</value> [Description("查詢的表名"), Category("附加"), DefaultValue("t_item")] public string TableName { get; set; } = "t_item"; /// <summary> /// 篩選的字段名 /// </summary> /// <value>The name of the field.</value> [Description("篩選的字段名"), Category("附加"), DefaultValue("item_num")] public string FieldName { get; set; } = "item_num"; /// <summary> /// 篩選起始長度 /// </summary> /// <value>The length of the find.</value> [Description("篩選起始長度"), Category("附加"), DefaultValue(3)] public int FindLength { get; set; } = 3; /// <summary> /// 附件查詢條件 /// </summary> /// <value>The addi conditions.</value> [Description("附件查詢條件"), Category("附加"), DefaultValue("")] public string AddiConditions { get; set; } = ""; /// <summary> /// 數據庫名稱 /// </summary> /// <value>The name of the database.</value> [Description("數據庫名稱"), Category("附加"), DefaultValue("wsprint")] public string DatabaseName { get; set; } = "wsprint"; /// <summary> /// 數據庫用戶名稱 /// </summary> /// <value>The uid.</value> [Description("數據庫用戶名稱"), Category("附加"), DefaultValue("sa")] public string UID { get; set; } = "sa"; /// <summary> /// 數據庫用戶密碼. /// </summary> /// <value>The password.</value> [Description("數據庫用戶密碼"), Category("附加"), DefaultValue("sa")] public string PWD { get; set; } = "sa"; /// <summary> /// 數據庫服務器名稱或IP /// </summary> /// <value>The name of the serve.</value> [Description("數據庫服務器名稱或IP"), Category("附加"), DefaultValue("xx-erpsvr")] public string ServeName { get; set; } = "xx-erpsvr"; [Description("保留歷史輸入數量"), Category("附加"), DefaultValue(10)] public int KeepHistorys { get; set; } = 10; [Description("容許歷史輸入記錄"), Category("附加"), DefaultValue(false)] public bool EnabledHistory { get; set; } = false; private System.Windows.Forms.ListBox ItemsListBox { get; set; } = new System.Windows.Forms.ListBox(); /// <summary> /// 記錄歷史操做的用戶名 /// </summary> /// <value>The user identifier.</value> [Description("記錄歷史操做的用戶名"), Category("附加"), DefaultValue("")] public string UserId { get; set; } = ""; System.Data.DataTable dt = new System.Data.DataTable(), dth = new System.Data.DataTable(); System.Data.SqlClient.SqlConnection conn; System.Data.SqlClient.SqlCommand cmd; public InputHistoryAndFilterTextBox() { } public void Init() { ItemsListBox.Parent = this.Parent; ItemsListBox.Top = this.Top + this.Height; if (ItemsListBox.Top + ItemsListBox.Height > this.Parent.Height) ItemsListBox.Top = this.Top - ItemsListBox.Height; ItemsListBox.Left = this.Left; ItemsListBox.Width = this.Width; ItemsListBox.Visible = false; ItemsListBox.BringToFront(); conn = new System.Data.SqlClient.SqlConnection("Data Source=" + ServeName + ";Initial Catalog=" + DatabaseName + ";Connect Timeout=30;UID = " + UID + ";PWD=" + PWD); cmd = new System.Data.SqlClient.SqlCommand(); cmd.Connection = conn; this.TextChanged += FilterTextBox_TextChanged; this.LostFocus += FilterTextBox_LostFocus; this.KeyDown += FilterTextBox_KeyDown; Thread thread = new Thread(LoadData); ItemsListBox.LostFocus += ItemsListBox_LostFocus; ItemsListBox.MouseDoubleClick += ItemsListBox_MouseDoubleClick; this.Validated += FilterTextBox_Validated; this.GotFocus += FilterTextBox_GotFocus; thread.Start(); } private void FilterTextBox_GotFocus(object sender, EventArgs e) { if (dth.Rows.Count > 0 && this.Text.Trim() == "") { ItemsListBox.Items.Clear(); foreach (System.Data.DataRow row in dth.Rows) { ItemsListBox.Items.Add(row[0].ToString()); } ItemsListBox.Visible = true; } } private void FilterTextBox_Validated(object sender, EventArgs e) { if (this.Text.Trim().Length >= this.FindLength) { try { cmd.CommandText = string.Format("insert into UserInputHistory (ProductName,FormName,ControlName,UserID,InputText) values ('{0}','{1}','{2}','{3}','{4}')", this.ProductName, this.FindForm().Name, this.Name, UserId, this.Text); if (cmd.Connection.State == System.Data.ConnectionState.Closed) cmd.Connection.Open(); if(cmd.ExecuteNonQuery()>0) { Thread thread = new Thread(LoadInputData); thread.Start(); } } catch { throw; } finally { cmd.Connection.Close(); } } } private void ItemsListBox_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e) { if (ItemsListBox.SelectedItem != null) { this.Text = ItemsListBox.SelectedItem.ToString(); } ItemsListBox.Visible = false; this.Focus(); } private void ItemsListBox_LostFocus(object sender, EventArgs e) { ItemsListBox.Visible = false; } private void FilterTextBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { switch (e.KeyCode) { case System.Windows.Forms.Keys.Up: if (ItemsListBox.SelectedIndex > 0) { ItemsListBox.SelectedIndex -= 1; } break; case System.Windows.Forms.Keys.Down: if (ItemsListBox.SelectedIndex < ItemsListBox.Items.Count - 1) ItemsListBox.SelectedIndex += 1; break; case System.Windows.Forms.Keys.Enter: if (ItemsListBox.SelectedItem != null) { this.Text = ItemsListBox.SelectedItem.ToString(); } ItemsListBox.Visible = false; break; } } private void FilterTextBox_LostFocus(object sender, EventArgs e) { if (!ItemsListBox.Focused) ItemsListBox.Visible = false; } private void FilterTextBox_TextChanged(object sender, EventArgs e) { string text = this.Text.Trim(); if (text.Length >= FindLength) { ItemsListBox.Items.Clear(); foreach (var row in dt.Select(FieldName + " like '%" + text + "%'", FieldName)) { ItemsListBox.Items.Add(row[0].ToString()); } ItemsListBox.Visible = true; } else { ItemsListBox.Visible = false; } } private void LoadData() { try { //載入篩選內容 cmd.CommandText = "select " + FieldName + " from " + TableName + (AddiConditions.Trim() == "" ? "" : " where ") + AddiConditions + " group by " + FieldName; new System.Data.SqlClient.SqlDataAdapter(cmd).Fill(dt); LoadInputData(); } catch (Exception ex) { throw new Exception("使用前請先設置鏈接屬性" + ex.Message); } } private void LoadInputData() { try { //載入歷史輸入內容 cmd.CommandText = string.Format("select distinct TOP {4} InputText from UserInputHistory where ProductName = '{0}' and FormName = '{1}' and ControlName = '{2}' and UserID = '{3}' order by ID desc ", ProductName, FindForm().Name, Name, UserId, KeepHistorys); new System.Data.SqlClient.SqlDataAdapter(cmd).Fill(dth); } catch (Exception ex) { throw new Exception("使用前請先設置鏈接屬性" + ex.Message); } } } }