123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- using System;
- using System.Data;
- using System.Windows.Forms;
- using UAS_LabelMachine.PublicMethod;
- namespace UAS_LabelMachine.CustomControl
- {
- public partial class SearchTextBox : UserControl
- {
- DataHelper dh = new DataHelper();
- #region 构造函数
- public SearchTextBox()
- {
- InitializeComponent();
- }
- #endregion
- #region 变量
- /// <summary>
- /// 是否通过Leave事件找到数据
- /// </summary>
- public bool LeaveFindData;
- /// <summary>
- /// DBFind窗体弹出的标题
- /// </summary>
- private string DBTitle1;
- /// <summary>
- /// 输入框是否可编辑
- /// </summary>
- private bool TextBoxEnable1;
- /// <summary>
- /// 业务逻辑的标识符
- /// </summary>
- private string caller;
- /// <summary>
- /// 发起DbFind的窗体
- /// </summary>
- private string formName;
- /// <summary>
- /// 需要赋值的字段
- /// </summary>
- private string[] setValueField;
- /// <summary>
- /// 初始的筛选条件
- /// </summary>
- private string condition;
- /// <summary>
- /// 权限标识
- /// </summary>
- private string Power1;
- /// <summary>
- /// 查询的表名
- /// </summary>
- private string tableName;
- /// <summary>
- /// 查询的字段
- /// </summary>
- private string selectField;
- DataTable dt = new DataTable();
- DbFind db;
- private DataTable returnData;
- public override string Text
- {
- get
- {
- return TextBox.Text;
- }
- set
- {
- TextBox.Text = value;
- }
- }
- string AllPower1;
- public string AllPower
- {
- get
- {
- return AllPower1;
- }
- set
- {
- AllPower1 = value;
- }
- }
- public string Caller
- {
- get
- {
- return caller;
- }
- set
- {
- caller = value;
- }
- }
- public string FormName
- {
- get
- {
- return formName;
- }
- set
- {
- formName = value;
- }
- }
- public string[] SetValueField
- {
- get
- {
- return setValueField;
- }
- set
- {
- setValueField = value;
- }
- }
- public string Condition
- {
- get
- {
- return condition;
- }
- set
- {
- condition = value;
- }
- }
- public string TableName
- {
- get
- {
- return tableName;
- }
- set
- {
- tableName = value;
- }
- }
- public string SelectField
- {
- get
- {
- return selectField;
- }
- set
- {
- selectField = value;
- }
- }
- public string Power
- {
- get
- {
- return Power1;
- }
- set
- {
- Power1 = value;
- }
- }
- public string DBTitle
- {
- get
- {
- return DBTitle1;
- }
- set
- {
- DBTitle1 = value;
- }
- }
- public bool TextBoxEnable
- {
- get
- {
- return TextBoxEnable1;
- }
- set
- {
- TextBoxEnable1 = value;
- }
- }
- public DataTable ReturnData
- {
- get
- {
- return returnData;
- }
- set
- {
- returnData = value;
- }
- }
- #endregion
- #region 自定义事件
- public delegate void OnTextChange(object sender, EventArgs e);
- //定义事件
- public event OnTextChange UserControlTextChanged;
- //定义委托
- public delegate void Icon_Click(object sender, EventArgs e);
- /// <summary>
- /// 图标点击事件
- /// </summary>
- public event Icon_Click SearchIconClick;
- public delegate void DBSourceChange(object sender, EventArgs e);
- public event DBSourceChange DbChange;
- public delegate void OnTextKeyDown(object sender, KeyEventArgs e);
- public event OnTextKeyDown TextKeyDown;
- #endregion
- //定义委托
- private void TextBox_TextChanged(object sender, EventArgs e)
- {
- UserControlTextChanged?.Invoke(sender, new EventArgs());
- }
- //Key先发起失去焦点事件,在执行用户自定义的事件
- private void TextBox_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Enter)
- {
- TextBox_Leave(sender, e);
- TextKeyDown?.Invoke(sender, new KeyEventArgs(e.KeyCode));
- }
- }
- private void Search_Icon_Click(object sender, EventArgs e)
- {
- SearchIconClick?.Invoke(sender, new EventArgs());
- db = new DbFind(Name, tableName, selectField, setValueField, caller, formName, condition);
- db.Text = DBTitle1;
- LogManager.DoLog("DbFind查询,发起窗口【" + formName + "】,查询表【" + tableName + "】,字段" + selectField + ",条件" + condition);
- if (db.IsAbleDbFind1)
- {
- db.ShowDialog();
- GetData();
- }
- else
- {
- MessageBox.Show("该字段未配置DbFind", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- }
- /// <summary>
- /// 触发给界面的
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public void ReturnDataToForm()
- {
- returnData = dt;
- DbChange?.Invoke(new object(), new EventArgs());
- }
- private void TextBox_Enter(object sender, EventArgs e)
- {
- TextBox.BackColor = System.Drawing.Color.GreenYellow;
- }
- /// <summary>
- /// 重新刷新数据
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public void RefreshDB(object sender, EventArgs e)
- {
- TextBox_Leave(sender, e);
- GetData();
- }
- public void TextBox_Leave(object sender, EventArgs e)
- {
- TextBox.BackColor = System.Drawing.Color.White;
- GetData();
- }
- private void SearchTextBox_Load(object sender, EventArgs e)
- {
- if (!TextBoxEnable1)
- TextBox.BackColor = System.Drawing.Color.White;
- TextBox.Enabled = TextBoxEnable1;
- }
- private void SearchTextBox_SizeChanged(object sender, EventArgs e)
- {
- TextBox.Width = this.Width - Search_Icon.Width - 3;
- }
- public void GetData()
- {
- string sql = "";
- //将查询到的结果返回界面
- if (TextBox.Text != "")
- {
- sql = "select " + BaseUtil.AddField(setValueField) + " from " + tableName + " where " + Name + "='" + TextBox.Text + "'";
- }
- else if (db != null)
- {
- sql = "select " + BaseUtil.AddField(setValueField) + " from " + tableName + " where " + Name + "='" + db.TextBoxValue1 + "'";
- }
- if (condition != null)
- {
- sql += " and " + condition;
- }
- try
- {
- Console.WriteLine(sql);
- dt = (DataTable)dh.ExecuteSql(sql, "select");
- if (dt.Rows.Count > 0)
- ReturnDataToForm();
- }
- catch (Exception e)
- {
- LogManager.DoLog(e.Message);
- }
- }
- }
- }
|