/*******************************************************************************
* Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
* 类的信息:
* 1.程序名称:dkPostSearchBox.cs
* 2.功能描述:职务选择控件
* 编辑履历:
* 作者 日期 版本 修改内容
* 冯雪 2014/09/13 1.00 新建
*******************************************************************************/
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Dongke.IBOSS.PRD.Basics.BaseControls;
using Dongke.IBOSS.PRD.WCF.Proxys;
namespace Dongke.IBOSS.PRD.Client.Controls
{
///
/// 职务选择控件
///
public partial class dkPostSearchBox : UserControl
{
#region 成员变量
///
/// 数据选择窗体
///
private S_CMN_004 _frmPost;
///
/// 控件是否可用
///
private bool _isEnabled = true;
///
/// 数据源
///
private DataTable _dataSource = null;
///
/// 职务ID
///
private int? _postID;
///
/// 职务编号
///
private string _postCode;
///
/// 职务名称
///
private string _postName;
///
/// 控件是否是必须输入项目
///
private bool _isMustInput;
///
/// 是否只显示有效数据
///
private bool _IsOnlyShowValid = true;
#endregion
#region 构造函数
public dkPostSearchBox()
{
InitializeComponent();
this.ReadOnly = true;
}
#endregion
#region 属性
///
/// 是否只显示有效数据
///
[Description("设置控件是否只显示有效数据。")]
[DefaultValue(true)]
public bool IsOnlyShowValid
{
get
{
return _IsOnlyShowValid;
}
set
{
_IsOnlyShowValid = value;
}
}
///
/// 获取或者设定控件是否是必须输入项目
///
[DefaultValue("False")]
[Description("获取或者设定控件是否是必须输入项目。")]
public bool IsMustInput
{
get
{
return _isMustInput;
}
set
{
_isMustInput = value;
// 项目为必须输入项时,需要修改字体颜色
if (_isMustInput)
{
this.lblPost.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(155)))), ((int)(((byte)(39)))), ((int)(((byte)(39)))));
}
else
{
this.lblPost.ForeColor = System.Drawing.SystemColors.ControlText;
}
}
}
///
/// Lable标签文本
///
[DefaultValue("职务名称")]
[Description("获取或者设定Lable标签字符。")]
public string Title
{
get
{
return this.lblPost.Text;
}
set
{
this.lblPost.Text = value;
AdjustControl();
}
}
///
/// 获取或者设定控件的数据源。
///
[Description("获取或者设定控件的数据源。")]
public DataTable DataSource
{
get
{
return _dataSource;
}
set
{
DataTable dataSource = SetFilter(value);
_dataSource = dataSource;
}
}
///
/// 获取或者设定控件的尺寸大小。
///
[Description("获取或者设定控件的尺寸大小。")]
public new Size Size
{
get
{
return base.Size;
}
set
{
base.Size = value;
AdjustControl();
}
}
///
/// 获取或者设定控件的文本框的文本字符串。
///
[Description("获取或者设定控件的文本框的文本字符串。")]
public override string Text
{
get
{
return this.txtPost.Text;
}
set
{
this.txtPost.Text = value;
}
}
///
/// 获取或者设定控件的文本框的背景颜色。
///
[Description("获取或者设定控件的文本框的背景颜色。")]
public Color TxtUserBackColor
{
get
{
return this.txtPost.BackColor;
}
set
{
this.txtPost.BackColor = value;
}
}
///
/// 获取或者设定控件的职务ID。
///
[Description("获取或者设定控件的职务ID。")]
[DefaultValue(null)]
public int? PostID
{
get
{
return _postID;
}
set
{
this._postID = null;
this._postCode = string.Empty;
this._postName = string.Empty;
this.Text = string.Empty;
if (value != null)
{
// 当数据源为null时,查询数据源
if (DataSource == null)
{
// 查询数据源
DataSource = GetPostInfo();
}
if (value != null && value != 0 && this.DataSource != null)
{
DataTable userTable = this.DataSource.Copy();
DataRow[] dataRows = userTable.Select("PostID = " + value);
if (dataRows.Length == 1)
{
this._postID = value;
this._postCode = dataRows[0]["PostCode"] as string;
this._postName = dataRows[0]["PostName"] as string;
this.Text = dataRows[0]["PostName"] as string;
}
}
}
}
}
///
/// 获取或者设定控件的职务Code
///
[Description("获取或者设定控件的职务Code。")]
public string PostCode
{
get
{
return _postCode;
}
set
{
_postCode = value;
}
}
///
/// 获取或者设定控件的职务名称
///
[Description("获取或者设定控件的职务名称。")]
public string PostName
{
get
{
return _postName;
}
set
{
_postName = value;
}
}
///
/// 获取或者设定控件是否可用。
///
[System.ComponentModel.DefaultValue(null)]
[Description("获取或者设定控件是否可用。")]
public new bool Enabled
{
get
{
return _isEnabled;
}
set
{
_isEnabled = value;
this.txtPost.Enabled = _isEnabled;
this.TabStop = _isEnabled;
}
}
///
/// 获取或者设定控件是否只读。
///
[System.ComponentModel.DefaultValue(null)]
[Description("获取或者设定控件是否只读。")]
public bool ReadOnly
{
set
{
this.txtPost.ReadOnly = value;
}
}
///
/// 范围权限
///
public byte PurviewType
{
get;
set;
}
#endregion
#region 定义委托事件
public delegate void TextBoxChangedHandle(object sender, TextChangeEventArgs e);
public event TextBoxChangedHandle UserValueChanged;
#endregion
#region 控件事件
///
/// 控件尺寸大小改变事件
///
///
///
private void dkPostSearchBox_SizeChanged(object sender, System.EventArgs e)
{
AdjustControl();
}
///
/// 查询按钮按下事件
///
///
///
private void btnSearch_Click(object sender, System.EventArgs e)
{
// 如果属性是不可用,是不能进行点击事件的
if (!Enabled)
{
return;
}
// 当数据源为null时,查询数据源
if (DataSource == null)
{
// 查询数据源
DataSource = GetPostInfo();
}
// 释放窗体资源
if (null != _frmPost)
{
_frmPost.Dispose();
_frmPost = null;
}
// 打开查询窗体
_frmPost = new S_CMN_004(this.txtPost.Text);
_frmPost.DataSource = this.DataSource;
DialogResult dialogResult = _frmPost.ShowDialog();
// 查询窗体返回值给控件赋值
if (dialogResult.Equals(DialogResult.OK))
{
if (_frmPost.PostRow != null)
{
_postID = Convert.ToInt32(_frmPost.PostRow["PostID"]);
_postCode = _frmPost.PostRow["PostCode"].ToString();
_postName = _frmPost.PostRow["PostName"].ToString();
this.Text = _frmPost.PostRow["PostName"].ToString();
}
}
}
///
/// 清除控件的值
///
///
///
private void txtUser_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete)
{
this.txtPost.Text = "";
this._postID = null;
this._postCode = string.Empty;
this._postName = string.Empty;
}
}
///
/// 文本框文本改变事件
///
///
///
private void txtUser_TextChanged(object sender, EventArgs e)
{
if (UserValueChanged != null)
{
UserValueChanged(this, new TextChangeEventArgs(this.txtPost.Text));
}
}
public class TextChangeEventArgs : EventArgs
{
public TextChangeEventArgs(string message)
{
}
}
#endregion
#region 私有方法
///
/// 设置控件的样式
///
protected void AdjustControl()
{
// 取得按钮控件的宽度和高度
int buttonWidth = (this.btnSearch.Visible) ? this.btnSearch.Width : 0;
this.Height = this.btnSearch.Height;
// 设置控件的尺寸和位置。
this.txtPost.Location = new System.Drawing.Point(this.lblPost.Width +
ControlsConst.CONTROLSPACE, (this.Height - this.txtPost.Height) / 2);
this.txtPost.Size = new System.Drawing.Size(this.Width - this.btnSearch.Width -
(this.lblPost.Width + ControlsConst.CONTROLSPACE * 2), this.Height);
this.btnSearch.Location =
new System.Drawing.Point(this.Width - this.btnSearch.Width,
(this.Height - this.btnSearch.Height) / 2);
// 设置标签的尺寸和位置。
this.lblPost.Location =
new System.Drawing.Point(0, (this.Height - this.lblPost.Height) / 2);
this.lblPost.Size = new System.Drawing.Size(this.lblPost.Width, this.lblPost.Height);
}
///
/// 过滤数据源
///
///
///
protected DataTable SetFilter(DataTable dataSource)
{
return dataSource;
}
///
/// 调用服务方法获取用户信息
///
///
private DataTable GetPostInfo()
{
DataSet userDataSet = CommonModuleProxy.Service.GetMSTPostInfo();
if (userDataSet == null)
{
return new DataTable();
}
if (userDataSet.Tables.Count <= 0)
{
return new DataTable();
}
return userDataSet.Tables[0];
}
#endregion
}
}