联想控件类

  实现功能描述:1、单击小三角显示所有人员供用户选择。2、输入汉字首字母显示相应的汉字。3、支持用户手工输入和智能联想。

  实现效果如下:

  

 

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    * 
    *            功能描述:联想控件类
    * 
    *            参数变量:参看构造函数
    * 
    *            创建日期:2015-04-15
    * 
    *            创 建 者:荣码一生
    * 
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    public class LevenLupAndEdit : XtraForm
    {
        private readonly LookUpEdit _lupEdit;
        private readonly TextEdit _edit;
        private readonly CheckEdit _chkPmsUp;
        private DataTable _dateTableAll;
        private readonly string _name;
        private readonly string _personname;
        private readonly string _ffirstletter;

        /// <summary>
        /// 联想控件类
        /// </summary>
        /// <param name="type">控件类型</param>
        /// <param name="chkPmsUp">CheckEdit控件</param>
        /// <param name="lupedit">LookUpEdit控件</param>
        /// <param name="edit">TextEdit控件</param>
        /// <param name="dataTableAll">数据表</param>
        /// <param name="name">用户ID</param>
        /// <param name="personname">用户名字</param>
        public LevenLupAndEdit(string type, CheckEdit chkPmsUp, LookUpEdit lupedit, TextEdit edit, DataTable dataTableAll, string name, string personname)
        {
            bool flag = true;

            _lupEdit = lupedit;
            _edit = edit;
            _dateTableAll = dataTableAll;
            _name = name;
            _personname = personname;
            _ffirstletter = "FFIRSTLETTER";
            if (chkPmsUp != null)
            {
                _chkPmsUp = chkPmsUp;
            }
            Action<KeyEventArgs> lupKeyDownAction = e => { _edit.Visible = e.KeyCode != Keys.Enter; };

            _lupEdit.KeyDown += (sender, e) => lupKeyDownAction(e);    //LookUpEdit的KeyDown事件

            _lupEdit.Click += delegate { _edit.Visible = true; };      //LookUpEdit的点击事件

            _lupEdit.TextChanged += delegate                           //LookUpEdit的内容改变事件
            {
                if (_lupEdit.Text.Length > 0)
                    _edit.Visible = false;
            };

            _edit.KeyDown += (sender, e) =>                             //TextEdit的KeyDown事件
            {
                if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up)
                {
                    _lupEdit.Focus();
                    return;
                }
                if (e.KeyCode == Keys.Enter)
                {
                    _lupEdit.Focus();
                    lupKeyDownAction(e);
                    return;
                }
                if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
                {
                    TextEdit_Changed(ref _dateTableAll);
                }
                else
                {
                    _edit.Focus();
                }
            };
            _edit.TextChanged += delegate                            //TextEdit的内容改变事件
            {
                if (!flag) return;
                TextEdit_Changed(ref _dateTableAll);
            };

            Action checkChangedAction = () =>
            {
                _chkPmsUp.CheckStateChanged += delegate              //注册选择改变事件
                {
                    if (_chkPmsUp.Checked)
                    {
                        flag = true;
                        _edit.Enabled = true;
                        _lupEdit.Enabled = true;
                        return;
                    }

                    flag = false;
                    _edit.Enabled = false;
                    _edit.Text = "";

                    _lupEdit.Enabled = false;
                    _lupEdit.ItemIndex = 0;
                };
            };
            //此处为工厂方法,针对不同的情况,添加不同事件处理程序(以后再重构)
            switch (type)
            {
                case null:
                case "":
                    break;
                case "上传PSM":
                    _chkPmsUp.Checked = true;
                    checkChangedAction();
                    break;
                case "操作步数查询":
                    _chkPmsUp.Checked = false;
                    checkChangedAction();
                    break;
                case "正令执行":
                    _chkPmsUp.Checked = true;
                    checkChangedAction();
                    break;
                default:
                    return;
            }

        }

        private DataTable GetDtTable()   //构造新的数据表
        {
            var dt = new DataTable();
            dt.Columns.Add(_name);
            dt.Columns.Add(_personname);
            dt.Columns.Add(_ffirstletter);
            dt.Clear();
            return dt;
        }

        private void TextEdit_Changed(ref DataTable dtTable)
        {
            var dt = GetDtTable();
            string name = _edit.Text.ToUpper();
            if (!string.IsNullOrEmpty(name))
            {
                if (char.IsLower(name[0]) || char.IsUpper(name[0])) //是字母
                {
                    for (int i = 0; i < dtTable.Rows.Count; i++)
                    {
                        if (dtTable.Rows[i][_name].ToString().Trim() == "-1")
                        {
                            dt.Rows.Add(dtTable.Rows[i][_name].ToString(),
                                dtTable.Rows[i][_personname].ToString(),
                                dtTable.Rows[i][_ffirstletter].ToString());
                        }
                        else if (dtTable.Rows[i][_ffirstletter].ToString().StartsWith(name))
                        {
                            dt.Rows.Add(dtTable.Rows[i][_name].ToString(),
                                dtTable.Rows[i][_personname].ToString(),
                                dtTable.Rows[i][_ffirstletter].ToString());
                        }
                    }
                }
                else //是汉字
                {
                    for (int i = 0; i < dtTable.Rows.Count; i++)
                    {
                        if (dtTable.Rows[i][_name].ToString().Trim() == "-1")
                        {
                            dt.Rows.Add(dtTable.Rows[i][_name].ToString(),
                                dtTable.Rows[i][_personname].ToString(),
                                dtTable.Rows[i][_ffirstletter].ToString());
                        }
                        else if (dtTable.Rows[i][_personname].ToString().StartsWith(name))
                        {
                            dt.Rows.Add(dtTable.Rows[i][_name].ToString(),
                                dtTable.Rows[i][_personname].ToString(),
                                dtTable.Rows[i][_ffirstletter].ToString());
                        }
                    }
                }
                _lupEdit.Properties.DataSource = dt;
            }
            else
            {
                _lupEdit.Properties.DataSource = _dateTableAll;
            }

            _lupEdit.ItemIndex = _lupEdit.ItemIndex == -1 ? 0 : -1;
            _lupEdit.Text = "";
        }

    }

 

posted @ 2015-04-15 14:01  荣码一生  阅读(496)  评论(0编辑  收藏  举报