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 { /// /// 是否显示添加按钮 /// /// true if [add visible]; otherwise, false. public bool AddVisible { get { return this.btnAdd.Visible; } set { this.btnAdd.Visible = this.pl_bottom.Visible = value; } } /// /// 列配置模块 /// public GridColumnModel model; /// /// 隐藏值 /// public string EditValue = string.Empty; /// /// 输入框 /// public PopupContainerEdit popupContainerEdit; /// /// Gets the TreeView object. /// /// The TreeView object. public TreeView TreeViewObj { get { return this.tvMain.TreeView; } } /// /// Gets the TreeView ex object. /// /// The TreeView ex object. public TreeViewEx TreeViewExObj { get { return this.tvMain; } } /// /// 弹出框回车事件 /// public event KeyEventHandler OnGridViewEnter; /// /// 添加按钮点击事件 /// public event EventHandler OnAddMouseClick; /// /// 弹出框鼠标点击 /// public event EventHandler OnGridViewMouseClick; /// /// 弹出框是否显示 /// /// true if this instance is show popup; otherwise, false. public bool IsShowPopup; /// /// The value field /// public string ValueField = string.Empty; /// /// The value member /// [Description("设置ValueMember")] public string ValueMember = string.Empty; /// /// The text field /// [Description("设置TextMember")] public string TextMember = string.Empty; /// /// 数据源 /// public string SourceSQL = string.Empty; /// /// 不是根节点是否能被选中 /// public bool isRootNode = true; /// /// 是否可以多选 /// /// The label. public bool isMultiClick = true; public PubTreeLookUp() { InitializeComponent(); } /// /// 查询数据源 /// /// The data source. public DataTable DataSourceTable { private set; get; } /// /// 设置数据源 /// /// The data source. 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); } } } /// /// 说明:键盘弹起 /// 创建人:龚宇超 /// 创建日期:2018-03-16 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. 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); } } /// /// 说明:点击GridView /// 创建人:龚宇超 /// 创建日期:2018-03-16 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. 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); } /// /// 说明:设置数据源 /// 创建人:王一帆 /// 创建日期:2021-03-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The data source. 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); } } /// /// 说明:添加操作后 /// 创建人:龚宇超 /// 创建日期:2018-04-03 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. 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); } } /// /// 说明:设置值 /// 创建人:龚宇超 /// 创建日期:2018-03-16 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// 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(); } } /// /// 添加操作 /// /// /// 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); } } /// /// 说明:计算控件Size,只计算一次 /// 创建人:龚宇超 /// 创建日期:2018-04-10 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The object. 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; } } } }