Files
lserp_cs_6.0/插件库/Lskj.Control/PubTreeLookUp.cs
T
2026-07-02 10:11:39 +00:00

306 lines
11 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 Lskj.Control.Model;
using Lskj.Business.Impl;
using DevExpress.XtraEditors;
using Lskj.Util;
namespace Lskj.Control
{
public partial class PubTreeLookUp : UserControl
{ /// <summary>
/// 是否显示添加按钮
/// </summary>
/// <value><c>true</c> if [add visible]; otherwise, <c>false</c>.</value>
public bool AddVisible
{
get { return this.btnAdd.Visible; }
set
{
this.btnAdd.Visible = this.pl_bottom.Visible = value;
}
}
/// <summary>
/// 列配置模块
/// </summary>
public GridColumnModel model;
/// <summary>
/// 隐藏值
/// </summary>
public string EditValue = string.Empty;
/// <summary>
/// 输入框
/// </summary>
public PopupContainerEdit popupContainerEdit;
/// <summary>
/// Gets the TreeView object.
/// </summary>
/// <value>The TreeView object.</value>
public TreeView TreeViewObj { get { return this.tvMain.TreeView; } }
/// <summary>
/// Gets the TreeView ex object.
/// </summary>
/// <value>The TreeView ex object.</value>
public TreeViewEx TreeViewExObj { get { return this.tvMain; } }
/// <summary>
/// 弹出框回车事件
/// </summary>
public event KeyEventHandler OnGridViewEnter;
/// <summary>
/// 添加按钮点击事件
/// </summary>
public event EventHandler OnAddMouseClick;
/// <summary>
/// 弹出框鼠标点击
/// </summary>
public event EventHandler OnGridViewMouseClick;
/// <summary>
/// 弹出框是否显示
/// </summary>
/// <value><c>true</c> if this instance is show popup; otherwise, <c>false</c>.</value>
public bool IsShowPopup;
/// <summary>
/// The value field
/// </summary>
public string ValueField = string.Empty;
/// <summary>
/// The value member
/// </summary>
[Description("设置ValueMember")]
public string ValueMember = string.Empty;
/// <summary>
/// The text field
/// </summary>
[Description("设置TextMember")]
public string TextMember = string.Empty;
/// <summary>
/// 数据源
/// </summary>
public string SourceSQL = string.Empty;
/// <summary>
/// 不是根节点是否能被选中
/// </summary>
public bool isRootNode = true;
/// <summary>
/// 是否可以多选
/// </summary>
/// <value>The label.</value>
public bool isMultiClick = true;
public PubTreeLookUp()
{
InitializeComponent();
}
/// <summary>
/// 查询数据源
/// </summary>
/// <value>The data source.</value>
public DataTable DataSourceTable { private set; get; }
/// <summary>
/// 设置数据源
/// </summary>
/// <value>The data source.</value>
public object DataSource
{
get
{
return this != null ? this.TreeViewExObj.DataSource : null;
}
set
{
if (value != null)
{
this.TreeViewExObj.TreeNodeKeyField = ValueMember;
this.TreeViewExObj.TreeNodeTextField = TextMember;
this.TreeViewExObj.BindTreeView(value as DataTable);
}
}
}
/// <summary>
/// <para>说明:键盘弹起</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2018-03-16 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="KeyEventArgs"/> instance containing the event data.</param>
protected void OnGridViewKeyUp(object sender, KeyEventArgs e)
{
try
{
if (this.OnGridViewEnter != null)
this.OnGridViewEnter(this.TreeViewObj, e);
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message, ex.Message);
}
}
/// <summary>
/// <para>说明:点击GridView</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2018-03-16 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
protected void OnGridViewMouseDown(object sender, MouseEventArgs e)
{
//int rowHandle = this.gridView.CalcHitInfo(e.X, e.Y).RowHandle;
//this.gridView.FocusedRowHandle = rowHandle;
//this.gridView.SelectRow(rowHandle);
//if (rowHandle > -1 && this.OnGridViewMouseClick != null)
// this.OnGridViewMouseClick(this.gridView, null);
}
/// <summary>
/// <para>说明:设置数据源</para>
/// <para>创建人:王一帆</para>
/// <para>创建日期:2021-03-24 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="dataSource">The data source.</param>
public void SetDataSource(DataTable dataSource)
{
this.DataSource = dataSource;
this.DataSourceTable = dataSource.Copy();
CalcPopupSize();
if (this.isMultiClick)
{
this.AddVisible = true;
this.OnAddMouseClick += new EventHandler(Popup_OnAddMouseClick);
}
}
/// <summary>
/// <para>说明:添加操作后</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2018-04-03 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
protected void Popup_OnAddMouseClick(object sender, EventArgs e)
{
try
{
this.SetValue();
popupContainerEdit.ClosePopup();
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message, ex.Message);
}
}
/// <summary>
/// <para>说明:设置值</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2018-03-16 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
public void SetValue()
{
//string ids = model.TreeCheckModel.Equals("1") ? this.TreeViewExObj.GetCheckValues(true) : this.TreeViewExObj.GetCheckValues();
string ids = isRootNode ? this.TreeViewExObj.GetCheckValues(true) : this.TreeViewExObj.GetCheckValues(false);
if (ids != null)
{
ids = ids.Replace("'", "");
popupContainerEdit.Properties.OwnerEdit.Text = ids;
this.Focus();
}
}
/// <summary>
/// 添加操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
if (this.OnAddMouseClick != null)
this.OnAddMouseClick(sender, e);
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message, ex.Message);
}
}
/// <summary>
/// <para>说明:计算控件Size,只计算一次</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2018-04-10 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="obj">The object.</param>
protected virtual void CalcPopupSize()
{
try
{
if (this.TreeViewObj != null)
{
if (this.InvokeRequired)
{
this.Invoke((EventHandler)delegate
{
DataTable table = this.DataSource as DataTable;
int calcLen = table.Rows.Count > 20 ? 20 : table.Rows.Count;
int iValueWidth = 0, iTextWidth = 0, iRemarkWidth = 0, iOtherWidth = 0;
for (int i = 0; i < calcLen; i++)
{
string valueValue = ValueMember.Substring(0, 1) != "_" ? table.Rows[i][ValueMember] + "" : "";
string textValue = TextMember.Substring(0, 1) != "_" ? table.Rows[i][TextMember] + "" : "";
int valueWidth = GraphicsText.GetTextWidth(valueValue);
int textWidth = GraphicsText.GetTextWidth(textValue);
if (iValueWidth < valueWidth) iValueWidth = valueWidth;
if (iTextWidth < textWidth) iTextWidth = textWidth;
}
// 由于计算有误差,所以计算列结果都加10px
iValueWidth = iValueWidth < 50 ? 50 : iValueWidth + 10;
iTextWidth = iTextWidth + 10;
int popupWidth = +iValueWidth + iTextWidth + iRemarkWidth + iOtherWidth + 20;
this.Width = popupWidth;
});
}
}
}
catch (Exception)
{
throw;
}
}
}
}