123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Windows.Forms;
- using UAS_MES_NEW.CustomControl.CustomCheckBox;
- using UAS_MES_NEW.DataOperate;
- using UAS_MES_NEW.Entity;
- using UAS_MES_NEW.PublicMethod;
- namespace UAS_MES_NEW.CustomControl.TextBoxWithIcon
- {
- public partial class MaCodeSearchTextBox : UserControl
- {
- #region 构造函数
- public MaCodeSearchTextBox()
- {
- 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;
- /// <summary>
- /// 设置绑定的CheckBox
- /// </summary>
- LockCheckBox LockCheckBox;
- public delegate void DBSourceChange(object sender, EventArgs e);
- public event DBSourceChange DbChange;
- DataTable dt = new DataTable();
- DbFind db;
- DataTable ReturnData1;
- public override string Text
- {
- get
- {
- return TextBox.Text.ToUpper();
- }
- 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 ReturnData1;
- }
- set
- {
- ReturnData1 = 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;
- #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)
- GetData(true);
- }
- public void SetLockCheckBox(LockCheckBox ctl)
- {
- LockCheckBox = ctl;
- }
- public void GetData(bool LeaveOrEnter)
- {
- DataHelper dh = SystemInf.dh;
- string sql = "";
- //将查询到的结果返回界面
- if (TextBox.Text != "")
- {
- sql = "select " + BaseUtil.AddField(setValueField) + " from " + tableName + " where " + Name + "='" + TextBox.Text.ToUpper() + "'";
- }
- else if (db != null)
- {
- sql = "select " + BaseUtil.AddField(setValueField) + " from " + tableName + " where " + Name + "='" + db.TextBoxValue1 + "'";
- }
- if (condition != null)
- {
- sql += " and " + condition;
- }
- try
- {
- dt = (DataTable)dh.ExecuteSql(sql, "select");
- if (dt.Rows.Count > 0)
- ReturnDataToFrom();
- }
- catch (Exception e)
- {
- LogManager.DoLog(e.Message);
- }
- if (LeaveOrEnter || TextBox.Text != "")
- LockCheckBox.Checked = true;
- }
- private void fillControl(int i, Control ct)
- {
- for (int j = 0; j < setValueField.Length; j++)
- {
- if (ct.Controls.Count > 0 && ct.Name.ToString() != setValueField[j])
- {
- Control.ControlCollection controls = ct.Controls;
- for (int k = 0; k < ct.Controls.Count; k++)
- {
- fillControl(i, controls[k]);
- }
- }
- else
- {
- if ((setValueField[j] == dt.Columns[i].Caption.ToLower() || setValueField[j] == dt.Columns[i].ColumnName.ToLower() || setValueField[j].Contains(dt.Columns[i].Caption.ToLower()) || (ct != null && ct.Tag != null && ct.Tag.ToString() == dt.Columns[i].Caption.ToLower())) && ct.Name.ToString() == setValueField[j])
- ct.Text = dt.Rows[0][dt.Columns[i].ColumnName].ToString();
- }
- }
- }
- 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(true);
- }
- else
- {
- MessageBox.Show("该字段未配置DbFind", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- }
- private void ReturnDataToFrom()
- {
- ReturnData1 = dt;
- DbChange?.Invoke(new object(), new EventArgs());
- }
- public void TextBox_Leave(object sender, EventArgs e)
- {
- //GetData(false);
- }
- 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;
- }
- private void MaCodeSearchTextBox_Leave(object sender, EventArgs e)
- {
- if (TextBox.Text != "")
- GetData(false);
- }
- }
- }
|