Files
lserp_cs_5.0/插件库/Lskj.MyControl/LookUp/LabelLookUp.cs
T
2026-07-10 15:25:05 +08:00

287 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommonLib;
using DevExpress.XtraEditors;
using System.Text.RegularExpressions;
using Lskj.PubUtils;
using ControlLib.inh;
namespace Lskj.MyControl.LookUp {
public partial class LabelLookUp : UserControl {
public LabelLookUp() {
InitializeComponent();
}
[Description("是否多选")]
public bool IsMultiple { get; set; }
[Description("SQL语句")]
public string SQL { get; set; }
[Description("设置列")]
public Dictionary<string, string> Columns { get; set; }
[Description("设置Value")]
public string ValueField { get; set; }
[Description("设置TextFiled")]
public string TextField { get; set; }
[Description("设置Value值")]
public string ReturnValue { get; set; }
[Description("设置Text值")]
public string ReturnText { get; set; }
[Description("窗体高度")]
public int WinHeight { get; set; }
[Description("窗体宽度")]
public int WinWidth { get; set; }
/// <summary>
/// Btn More
/// </summary>
public SimpleButton GetButton { get { return btnMore; } }
public TextBox GetTextBox { get { return txtBox; } }
#region 获取参数
public static List<string> GetParamValue(string param)
{
string SqlParamRegex = "{([^{])+}";
List<string> list = new List<string>();
Regex regex = new Regex(SqlParamRegex);
MatchCollection mcs = regex.Matches(param);
foreach (Match item in mcs)
{
if (list.IndexOf(item.Value) == -1)
list.Add(item.Value);
}
return list;
}
#endregion
/// <summary>
/// 取指定组件值
/// </summary>
/// <param name="parent"></param>
/// <param name="list"></param>
public string GetControlText( Control control)
{
if (control == null)
return "";
ControlModelTag cmTag = control.Tag as ControlModelTag;
if (cmTag.fieldTypeId == ControlType.LabAutoSeacherText)
{
return control.Text;
}
else if (cmTag.fieldTypeId == ControlType.LabAutoSeacherValue)
{
AutoSearcher te = control as AutoSearcher;
return te.ReturnValue.ToString();
}
else if (cmTag.fieldTypeId == ControlType.LabComboxValue)
{
LabelComboxEx Cb = control as LabelComboxEx;
return Cb.getComboBox.SelectedValue + "";
}
else if (cmTag.fieldTypeId == ControlType.LabComboxText)
{
LabelComboxEx Cb = control as LabelComboxEx;
return Cb.getComboBox.Text;
}
else if (cmTag.fieldTypeId == ControlType.LabDate ||
cmTag.fieldTypeId == ControlType.LabDateTime ||
cmTag.fieldTypeId == ControlType.LabDateTimeShort ||
cmTag.fieldTypeId == ControlType.LabTime ||
cmTag.fieldTypeId == ControlType.LabShortTime)
{
LabelDateEx Cb = control as LabelDateEx;
return Cb.GetDateTimePicker.Value.ToString("yyyy-MM-dd HH:mm:ss");
}
else if (cmTag.fieldTypeId == ControlType.LabTextInt)
{
if ((control.Text.Trim() == ""))
return "0";
else
return control.Text;
}
else if (cmTag.fieldTypeId == ControlType.LabMultiSelectValue || cmTag.fieldTypeId == ControlType.LabMultiSelectValueParam)
{
LabelLookUp lookup = control as LabelLookUp;
return lookup.ReturnValue;
}
else
{
if (cmTag.fieldTypeId != 99)
{
return control.Text;
}
}
return "0";
}
private void btnMore_Click(object sender, EventArgs e) {
FrmMain frmMain = new FrmMain();
frmMain.IsMultiple = IsMultiple;
var ctrs = this.Parent.Controls;
string tmpsql = SQL.Replace("#", "").Replace("#", "");
var fields = GetParamValue(tmpsql);
if (fields != null && fields.Count > 0)
{
for (int i = 0; i < fields.Count; i++)
{
string field = fields[i];
string fieldName = field.Replace("{", "").Replace("}", "");
Control control = ctrs.OfType<Control>().FirstOrDefault(x => (x.Tag as ControlModelTag).fieldName == fieldName);
if (control is LabelAutoSearcherEx) control = (control as LabelAutoSearcherEx).GetAutoSearcher;
string fieldValue = GetControlText(control);
tmpsql = tmpsql.Replace(field, fieldValue);
ControlModelTag controlModel = control.Tag as ControlModelTag;
frmMain.IsText = controlModel.fieldTypeId == ControlType.LabAutoSeacherValue?13:0;
}
}
frmMain.SQL = tmpsql;
frmMain.Columns = Columns;
frmMain.ValueField = ValueField;
frmMain.TextField = TextField;
frmMain.WinHeight = WinHeight;
frmMain.WinWidth = WinWidth;
frmMain.ReturnValue = ReturnValue;
frmMain.ReturnText=ReturnText;
frmMain.ShowDialog();
//接受返回来值
ReturnValue = frmMain.ReturnValue;
ReturnText = frmMain.ReturnText;
Text = frmMain.ReturnText;
}
[Description("设置值")]
public override string Text {
get { return txtBox.Text; }
set {
txtBox.Text = value;
}
}
/// <summary>
/// 设置Value值
/// </summary>
/// <param name="ids"></param>
public void SetValueText(string ids) {
if (string.IsNullOrEmpty(ids))
{
this.Text = "";
ReturnText = "";
ReturnValue = "";
return;
}
//string tmpsql = Regex.Replace(SQL, "#[^##]+#", " 1=1 "); ;
var ctrs = this.Parent.Controls;
string tmpsql = SQL.Replace("#", "").Replace("#", "");
var fields = GetParamValue(tmpsql);
if (fields != null && fields.Count > 0)
{
for (int i = 0; i < fields.Count; i++)
{
string field = fields[i];
string fieldName = field.Replace("{", "").Replace("}", "");
Control control = ctrs.OfType<Control>().FirstOrDefault(x => (x.Tag as ControlModelTag).fieldName == fieldName);
if (control is LabelAutoSearcherEx) control = (control as LabelAutoSearcherEx).GetAutoSearcher;
string fieldValue = GetControlText(control);
tmpsql = tmpsql.Replace(field, fieldValue);
}
}
//string sql2 = "select * from (" + tmpsql + ") as tab where " + ValueField + " in ('" + ids.Replace(",", "','") + "')";
DataTable dt = SqlHelper.ExecuteDataSet(tmpsql).Tables[0];
string text = string.Empty;
string[] spilts = ids.Trim(',').Split(',');
for (int i = 0; i < spilts.Length; i++)
{
DataRow rowItem = dt.Rows.Cast<DataRow>().FirstOrDefault(x => x[ValueField] + "" == spilts[i]);
if(rowItem != null)
text += rowItem[TextField] + ",";
}
this.Text = text.Trim(',');
ReturnText = text;
ReturnValue = ids;
}
public void SetTextValue(string ids)
{
if (string.IsNullOrEmpty(ids))
{
this.Text = "";
ReturnText = "";
ReturnValue = "";
return;
}
string tmpIds = ids.Replace(",", "','");
tmpIds = "'" + tmpIds + "'";
string tmpsql = Regex.Replace(SQL, "#[^##]+#", " 1=1 ");
DataTable dt = SqlHelper.ExecuteDataSet(tmpsql).Tables[0];
string text = string.Empty;
string[] spilts = ids.Trim(',').Split(',');
for (int i = 0; i < spilts.Length; i++)
{
DataRow rowItem = dt.Rows.Cast<DataRow>().FirstOrDefault(x => x[TextField] + "" == spilts[i]);
if (rowItem != null)
text += rowItem[ValueField] + ",";
}
this.Text = ids;
ReturnText = ids;
ReturnValue = text;
}
private string labelText;
[Description("Label值")]
public string LabelText {
get { return labelText; }
set {
labelText = value;
lbMsg.Text = value;
pl_left.Width = lbMsg.Width + 7;
}
}
private void txtBox_KeyDown(object sender, KeyEventArgs e)
{
if (sender != null && e != null)
{
if (e.Control)
{
if (e.KeyCode == Keys.V) // 复制
{
if (txtBox.Enabled)
{
//txtBox.Text = Clipboard.GetData(DataFormats.StringFormat).ToString();
object objData = Clipboard.GetData(DataFormats.StringFormat);
if (objData==null)
return;
//this.ReturnValue = Clipboard.GetData(DataFormats.StringFormat).ToString();
this.SetValueText(objData.ToString());
//MessageBox.Show(Clipboard.GetData(DataFormats.StringFormat).ToString());
}
}
}
}
}
}
}