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 变量
///
/// 是否通过Leave事件找到数据
///
public bool LeaveFindData;
///
/// DBFind窗体弹出的标题
///
private string DBTitle1;
///
/// 输入框是否可编辑
///
private bool TextBoxEnable1;
///
/// 业务逻辑的标识符
///
private string caller;
///
/// 发起DbFind的窗体
///
private string formName;
///
/// 需要赋值的字段
///
private string[] setValueField;
///
/// 初始的筛选条件
///
private string condition;
///
/// 权限标识
///
private string Power1;
///
/// 查询的表名
///
private string tableName;
///
/// 查询的字段
///
private string selectField;
///
/// 设置绑定的CheckBox
///
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);
///
/// 图标点击事件
///
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);
}
}
}