using System;
using System.Data;
using System.Windows.Forms;
using UAS_LabelMachine.Entity;
using UAS_LabelMachine.PublicMethod;
namespace UAS_LabelMachine.CustomControl
{
public partial class SearchTextBox : UserControl
{
DataHelper dh = SystemInf.dh;
#region 构造函数
public SearchTextBox()
{
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;
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);
///
/// 图标点击事件
///
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);
}
}
///
/// 触发给界面的
///
///
///
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;
}
///
/// 重新刷新数据
///
///
///
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
{
dt = (DataTable)dh.ExecuteSql(sql, "select");
if (dt.Rows.Count > 0)
ReturnDataToForm();
}
catch (Exception e)
{
LogManager.DoLog(e.Message);
}
}
}
}