415 lines
13 KiB
C#
415 lines
13 KiB
C#
/******************************
|
|
* 说明:自定义下拉选择框控件
|
|
* 创建人:龚宇超
|
|
* 创建日期:2017-08-07
|
|
* 修改人:
|
|
* 修改日期:
|
|
* 修改备注:
|
|
* 版本:1.0.0.0
|
|
******************************/
|
|
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 DevExpress.XtraEditors;
|
|
using DevExpress.XtraEditors.Controls;
|
|
using Lskj.Control.Model;
|
|
using Lskj.Business.Impl;
|
|
|
|
namespace Lskj.Control
|
|
{
|
|
/// <summary>
|
|
/// 自定义下拉选择框
|
|
/// </summary>
|
|
public partial class LabelComboxEdit : BaseUserControl
|
|
{
|
|
/// <summary>
|
|
/// 数据源
|
|
/// </summary>
|
|
private List<ComboBoxModel> _sources = new List<ComboBoxModel>();
|
|
/// <summary>
|
|
/// 绑定ValueMember
|
|
/// </summary>
|
|
public string ValueMember = string.Empty;
|
|
/// <summary>
|
|
/// 隐藏值字段
|
|
/// </summary>
|
|
public string ValueField = string.Empty;
|
|
/// <summary>
|
|
/// 显示值字段
|
|
/// </summary>
|
|
public string TextField = string.Empty;
|
|
/// <summary>
|
|
/// 下拉选择框控件
|
|
/// </summary>
|
|
public ComboBoxEdit TextEdit { get { return txtEdit; } }
|
|
/// <summary>
|
|
/// 文本
|
|
/// </summary>
|
|
/// <value>The label.</value>
|
|
public Label Label { get { return lblText; } }
|
|
/// <summary>
|
|
/// The contro object
|
|
/// </summary>
|
|
public MyControl ControlObj;
|
|
/// <summary>
|
|
/// 数据源
|
|
/// </summary>
|
|
public string SourceSQL = string.Empty;
|
|
/// <summary>
|
|
/// 是否带参数
|
|
/// </summary>
|
|
public bool Isparameters = false;
|
|
|
|
|
|
public LabelComboxEdit()
|
|
{
|
|
InitializeComponent();
|
|
// 绑定事件
|
|
txtEdit.QueryPopUp += TxtEdit_QueryPopUp;
|
|
|
|
}
|
|
|
|
private void TxtEdit_QueryPopUp(object sender, CancelEventArgs e)
|
|
{
|
|
this.RefreshData();
|
|
}
|
|
|
|
public void RefreshData()
|
|
{
|
|
if (Isparameters && !string.IsNullOrWhiteSpace(this.SourceSQL) && this.ControlObj != null)
|
|
{
|
|
string oldValue = this.EditValue;
|
|
|
|
string sqlValue = this.ControlObj.ReplaceControlValue(this.SourceSQL);
|
|
sqlValue = sqlValue.Replace("#", "");
|
|
DataTable table = BaseImpl.GetDataTableResult(sqlValue);
|
|
this.SetDataSource(table, true);
|
|
this.EditText = oldValue;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// <para>说明:设置控件显示文本</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-08-07 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="text">显示Lebel文本</param>
|
|
public override string LabelText
|
|
{
|
|
get
|
|
{
|
|
return this.lblText.Text;
|
|
}
|
|
set
|
|
{
|
|
this.lblText.Text = value;
|
|
if (FontSize > 0)
|
|
{
|
|
this.plLeft.Dock = DockStyle.Left;
|
|
this.plLeft.AutoSize = false;
|
|
this.lblText.AutoSize = false;
|
|
this.lblText.Dock = System.Windows.Forms.DockStyle.Fill;
|
|
this.lblText.Location = new System.Drawing.Point(0, 0);
|
|
this.lblText.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
|
|
this.txtEdit.Properties.AutoHeight = false;
|
|
this.txtEdit.Properties.AppearanceDropDown.Font = this.txtEdit.Properties.Appearance.Font;//下拉框里的字体和选中框中的保持一致
|
|
this.plLeft.Width = value.Length * GetCharWidth();
|
|
|
|
GetCharWidthMultilingual(this.lblText, this.lblText.Text, this.plLeft);
|
|
}
|
|
else
|
|
{
|
|
this.plLeft.Width = this.lblText.Width + PaddingLeft;
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 字体大小
|
|
/// </summary>
|
|
/// <value>The size of the control.</value>
|
|
public override float FontSize
|
|
{
|
|
get
|
|
{
|
|
return base.FontSize;
|
|
}
|
|
set
|
|
{
|
|
base.FontSize = value;
|
|
if (value > 0)
|
|
{
|
|
this.lblText.Font = new Font(this.lblText.Font.FontFamily, value);
|
|
this.TextEdit.Font = new Font(this.TextEdit.Font.FontFamily, value);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:设置控件显示值</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-08-07 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
public override string EditText
|
|
{
|
|
get
|
|
{
|
|
return txtEdit.Text;
|
|
}
|
|
set
|
|
{
|
|
|
|
ComboBoxModel item = ValueMember.Equals(ValueField)
|
|
? this._sources.FirstOrDefault(x => x.RowItem[x.ValueMember] + "" == value + "")
|
|
: this._sources.FirstOrDefault(x => x.RowItem[x.DisplayMember] + "" == value + "");
|
|
|
|
if (item != null)
|
|
{
|
|
txtEdit.SelectedItem = item;
|
|
}
|
|
else
|
|
{
|
|
txtEdit.EditValue = null;
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 获取隐藏值
|
|
/// </summary>
|
|
/// <value>The edit value.</value>
|
|
public string EditValue
|
|
{
|
|
get
|
|
{
|
|
ComboBoxModel model = this.txtEdit.SelectedItem as ComboBoxModel;
|
|
if (model != null)
|
|
{
|
|
return ValueMember.Equals(ValueField)
|
|
? model.RowItem[model.ValueMember] + ""
|
|
: model.RowItem[model.DisplayMember] + "";
|
|
}
|
|
else
|
|
{
|
|
return string.Empty;
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:设置控件提示文本</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-08-07 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <returns>返回控件值</returns>
|
|
public override string NullText
|
|
{
|
|
get
|
|
{
|
|
return this.txtEdit.Properties.NullText;
|
|
|
|
}
|
|
set
|
|
{
|
|
this.txtEdit.Properties.NullText = value;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 只读文本颜色
|
|
/// </summary>
|
|
/// <value>The color of the read only label.</value>
|
|
public override bool ReadOnly
|
|
{
|
|
get
|
|
{
|
|
return base.ReadOnly;
|
|
}
|
|
set
|
|
{
|
|
base.ReadOnly = value;
|
|
this.txtEdit.Properties.ReadOnly = value;
|
|
this.lblText.ForeColor = value ? base.ReadOnlyLabelForceColor : Required ? base.RequiredLabelForceColor : base.DefaultLabelForceColor;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 必填文本颜色
|
|
/// </summary>
|
|
/// <value>The color of the required label.</value>
|
|
public override bool Required
|
|
{
|
|
get
|
|
{
|
|
return base.Required;
|
|
}
|
|
set
|
|
{
|
|
base.Required = value;
|
|
if (value)
|
|
{
|
|
this.lblText.ForeColor = base.RequiredLabelForceColor;
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:需要单独验证是否修改</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-11-08 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <returns><c>true</c> if this instance is update; otherwise, <c>false</c>.</returns>
|
|
public override bool IsUpdate()
|
|
{
|
|
if (Model.FieldType == ControlType.LabComboxValue ||
|
|
Model.FieldType == ControlType.LabAutoCompleteValueParam)
|
|
{
|
|
return !this.Model.Text.Equals(this.EditValue, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
else
|
|
{
|
|
return base.IsUpdate();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:设置数据源</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-08-08 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="dataSource">数据源</param>
|
|
public void SetDataSource(DataTable dataSource)
|
|
{
|
|
SetDataSource(dataSource, false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// <para>说明:设置数据源</para>
|
|
/// </summary>
|
|
/// <param name="dataSource">数据源</param>
|
|
/// <param name="clearBeforeBind">绑定前是否清空原数据源</param>
|
|
public void SetDataSource(DataTable dataSource, bool clearBeforeBind)
|
|
{
|
|
try
|
|
{
|
|
if (clearBeforeBind)
|
|
{
|
|
ClearDataSource();
|
|
}
|
|
if (dataSource == null) return;
|
|
|
|
foreach (DataRow item in dataSource.Rows)
|
|
{
|
|
ComboBoxModel model = new ComboBoxModel
|
|
{
|
|
ValueMember = ValueField,
|
|
DisplayMember = TextField,
|
|
RowItem = item
|
|
};
|
|
this._sources.Add(model);
|
|
this.txtEdit.Properties.Items.Add(model);
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 清空下拉数据源和已选项,避免带参数下拉每次弹出重复追加。
|
|
/// </summary>
|
|
private void ClearDataSource()
|
|
{
|
|
this._sources.Clear();
|
|
this.txtEdit.SelectedItem = null;
|
|
this.txtEdit.EditValue = null;
|
|
this.txtEdit.Properties.Items.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 背景颜色
|
|
/// </summary>
|
|
/// <value>The color of the required label.</value>
|
|
public override Color BackgroundColor
|
|
{
|
|
get
|
|
{
|
|
return TextEdit.Properties.Appearance.BackColor;
|
|
}
|
|
set
|
|
{
|
|
TextEdit.Properties.Appearance.BackColor = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 前景颜色
|
|
/// </summary>
|
|
/// <value>The color of the required label.</value>
|
|
public override Color ForeColor
|
|
{
|
|
get
|
|
{
|
|
return TextEdit.Properties.Appearance.ForeColor;
|
|
}
|
|
set
|
|
{
|
|
TextEdit.Properties.Appearance.ForeColor = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 内容加粗
|
|
/// </summary>
|
|
/// <value>The color of the required label.</value>
|
|
public override bool ContentBold
|
|
{
|
|
get
|
|
{
|
|
return TextEdit.Properties.Appearance.Font.Bold;
|
|
}
|
|
set
|
|
{
|
|
Font oldFont = TextEdit.Properties.Appearance.Font;
|
|
TextEdit.Properties.Appearance.Font = new Font(oldFont.FontFamily, oldFont.Size, value ? FontStyle.Bold : FontStyle.Regular);
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
class ComboBoxModel
|
|
{
|
|
public string ValueMember;
|
|
public string DisplayMember;
|
|
public DataRow RowItem;
|
|
|
|
public override string ToString()
|
|
{
|
|
return this.RowItem[DisplayMember] + "";
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|