zoukankan      html  css  js  c++  java
  • 一个根据根据输入内容自动匹配的下来列表框,且支持拼音字母

    DropdownListNew.designer.cs

    namespace GPOS.Controls
    {
        partial class DropdownListNew
        {
            /// <summary> 
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary> 
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region 组件设计器生成的代码
    
            /// <summary> 
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.tbxControl = new DevComponents.DotNetBar.Controls.TextBoxX();
                this.lbRecordList = new System.Windows.Forms.ListBox();
                this.SuspendLayout();
                // 
                // tbxControl
                // 
                // 
                // 
                // 
                this.tbxControl.Border.BorderBottom = DevComponents.DotNetBar.eStyleBorderType.Solid;
                this.tbxControl.Border.BorderBottomWidth = 1;
                this.tbxControl.Border.BorderColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.DockSiteBackColor;
                this.tbxControl.Border.BorderLeft = DevComponents.DotNetBar.eStyleBorderType.Solid;
                this.tbxControl.Border.BorderLeftWidth = 1;
                this.tbxControl.Border.BorderRight = DevComponents.DotNetBar.eStyleBorderType.Solid;
                this.tbxControl.Border.BorderRightWidth = 1;
                this.tbxControl.Border.BorderTop = DevComponents.DotNetBar.eStyleBorderType.Solid;
                this.tbxControl.Border.BorderTopWidth = 1;
                this.tbxControl.Border.Class = "";
                this.tbxControl.Border.CornerType = DevComponents.DotNetBar.eCornerType.Square;
                this.tbxControl.Dock = System.Windows.Forms.DockStyle.Top;
                this.tbxControl.Location = new System.Drawing.Point(0, 0);
                this.tbxControl.Name = "tbxControl";
                this.tbxControl.Size = new System.Drawing.Size(189, 16);
                this.tbxControl.TabIndex = 0;
                this.tbxControl.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tbxControl_KeyDown);
                this.tbxControl.Leave += new System.EventHandler(this.tbxControl_Leave);
                this.tbxControl.TextChanged += new System.EventHandler(this.tbxControl_TextChanged);
                // 
                // lbRecordList
                // 
                this.lbRecordList.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                this.lbRecordList.Dock = System.Windows.Forms.DockStyle.Fill;
                this.lbRecordList.FormattingEnabled = true;
                this.lbRecordList.ItemHeight = 12;
                this.lbRecordList.Location = new System.Drawing.Point(0, 16);
                this.lbRecordList.Name = "lbRecordList";
                this.lbRecordList.Size = new System.Drawing.Size(189, 2);
                this.lbRecordList.TabIndex = 1;
                this.lbRecordList.Leave += new System.EventHandler(this.lbRecordList_Leave);
                this.lbRecordList.DoubleClick += new System.EventHandler(this.lbRecordList_DoubleClick);
                this.lbRecordList.KeyDown += new System.Windows.Forms.KeyEventHandler(this.lbRecordList_KeyDown);
                // 
                // DropdownListNew
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.Controls.Add(this.lbRecordList);
                this.Controls.Add(this.tbxControl);
                this.Name = "DropdownListNew";
                this.Size = new System.Drawing.Size(189, 23);
                this.Load += new System.EventHandler(this.TextBoxWithDataPick_Load);
                this.ResumeLayout(false);
    
            }
    
            #endregion
    
            private DevComponents.DotNetBar.Controls.TextBoxX tbxControl;
            private System.Windows.Forms.ListBox lbRecordList;  
        }
    }
    View Code

    DropdownListNew.cs

    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;
    
    namespace GPOS.Controls
    {
        public partial class DropDownListSelect : UserControl
        {
            private string _ControlValue;
            private int _ControlHeight = 21;
            private int _ControlWidth = 189;
            private int _CurrentCellIndex = 0;
            private bool _IsViewColValue = false;
            private bool _IsSupportPinYin = false;
            #region 公有变量
            /// <summary>
            /// 文本
            /// </summary>
            public string ControlText
            {
                get { return tbxControl.Text; }
                set { tbxControl.Text = value;
                if (ControlValueChanged != null)
                {
                    ControlValueChanged(this);
                }
                }
            }
            public delegate void ControlValueChangedHandel(object sender);
            public event ControlValueChangedHandel ControlValueChanged;
            /// <summary>
            /// 获取值
            /// </summary>
            public string ControlValue
            {
                get { return GetControlValue(); }
                set
                {
                    _ControlValue = value;
                    SetControlText();
                    if (ControlValueChanged != null)
                    {
                        ControlValueChanged(this);
                    }
                }
            }
            public int ControlWidth
            {
                get { return _ControlWidth; }
                set
                {
                    _ControlWidth = value;
                    this.Width = _ControlWidth;
                    tbxControl.Width = _ControlWidth;
                }
            }
            protected override Size DefaultSize
            {
                get
                {
                    return new Size(100, 50);
                }
            }
            /// <summary>
            /// 暂时无用
            /// </summary>
            public int ControlHeight
            {
                get { return _ControlHeight; }
                set
                {
                    _ControlHeight = value;
                    this.Height = _ControlHeight;
                    tbxControl.Height = _ControlHeight;
                }
            }
            /// <summary>
            /// 是否显示Value列
            /// </summary>
            public bool IsViewColValue
            {
                get { return _IsViewColValue; }
                set
                {
                    _IsViewColValue = value;
                }
            }
            /// <summary>
            /// 是否支持拼音检索(如果支持需加AllSpelling,FirstSpelling)
            /// </summary>
            public bool IsSupportPinYin
            {
                get { return _IsSupportPinYin; }
                set
                {
                    _IsSupportPinYin = value;
                }
            }
            /// <summary>
            /// 可供用户选择的数据集
            /// </summary>
            public DataTable ControlDataSource = null;
            /// <summary>
            /// 在ControlDataSource中的列名
            /// </summary>
            public string ControlColumnName = "";
    
            /// <summary>
            /// 获取的值
            /// </summary>
            public string ControlValueColumnName = "";
            #endregion
            public DropDownListSelect()
            {
                InitializeComponent();
            }
            #region 自定义函数
            private string GetControlValue()
            {
                if (!string.IsNullOrEmpty(tbxControl.Text))
                {
                    //这些情况下,不弹出选择框  
                    if (ControlDataSource == null || ControlColumnName == "" || !ControlDataSource.Columns.Contains(ControlColumnName))
                        return "";
                    //根据用户当前输入的内容,筛选出与内容相符的记录,显示在列表框中  
                    for (int i = 0; i < ControlDataSource.Rows.Count; i++)
                    {
                        if (ControlDataSource.Rows[i][ControlColumnName].ToString().Trim() == tbxControl.Text.Trim())
                        {
                            return ControlDataSource.Rows[i][ControlValueColumnName].ToString();
                        }
                    }
                }
                return "";
            }
    
            private void SetControlText()
            {
                if (!string.IsNullOrEmpty(_ControlValue))
                {
                    //这些情况下,不弹出选择框  
                    if (ControlDataSource == null || ControlColumnName == "" || !ControlDataSource.Columns.Contains(ControlColumnName))
                        return;
                    //根据用户当前输入的内容,筛选出与内容相符的记录,显示在列表框中  
                    for (int i = 0; i < ControlDataSource.Rows.Count; i++)
                    {
                        if (ControlDataSource.Rows[i][ControlValueColumnName].ToString().Trim() == _ControlValue)
                        {
                            tbxControl.Text = ControlDataSource.Rows[i][ControlColumnName].ToString();
                            this.Height = tbxControl.Height;
                            this.plRecordList.Visible = false;
                            this.SendToBack();
                        }
                    }
                }
            }
            #endregion
            #region tbxControl事件
            private void tbxControl_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyValue == (int)Keys.Down)
                {
                    if (dgvRecordList.Rows.Count != 0)
                    {
                        dgvRecordList.Focus();
                    }
                }
                int currentrowindex = -1;
                if (dgvRecordList.SelectedRows.Count > 0)
                {
                    currentrowindex = dgvRecordList.SelectedRows[0].Index;
                }
                else if (dgvRecordList.SelectedCells.Count > 0)
                {
                    currentrowindex = dgvRecordList.SelectedCells[0].RowIndex;
                }
                switch (e.KeyCode)
                {
                    case Keys.Up:
                        if (plRecordList.Visible)
                        {
                            dgvRecordList.Focus();
                        }
                        if (currentrowindex > 0)
                        {
                            dgvRecordList.Rows[currentrowindex].Selected = false;
                            dgvRecordList.Rows[currentrowindex - 1].Selected = true;
                        }
    
                        break;
                    case Keys.Down:
                        if (plRecordList.Visible)
                        {
                            dgvRecordList.Focus();
                        }
                        if (currentrowindex == -1)
                        {
                            if (dgvRecordList.Rows.Count > 0)
                            {
                                dgvRecordList.Rows[0].Selected = true;
                            }
                        }
                        else if (currentrowindex < dgvRecordList.Rows.Count - 1)
                        {
                            dgvRecordList.Rows[currentrowindex].Selected = false;
                            dgvRecordList.Rows[currentrowindex + 1].Selected = true;
                        }
                        else if (currentrowindex > 0)
                        {
                            dgvRecordList.Rows[currentrowindex].Selected = false;
                            dgvRecordList.Rows[0].Selected = true;
                        }
                        break;
                    case Keys.Enter:
                        if (plRecordList.Visible)
                        {
                            dgvRecordList.Focus();
                        }
                        if (currentrowindex > -1)
                        {
                            tbxControl.Text = dgvRecordList[1, currentrowindex].Value.ToString();
                        }
                        this.Height = tbxControl.Height;
                        this.plRecordList.Visible = false;
                        this.SendToBack();
                        break;
                    case Keys.Space:
                        if (currentrowindex > -1)
                        {
                            tbxControl.Text = dgvRecordList[1, currentrowindex].Value.ToString();
                        }
                        this.Height = tbxControl.Height;
                        this.plRecordList.Visible = false;
                        this.SendToBack();
                        break;
                    case Keys.Escape:
                        this.Height = tbxControl.Height;
                        this.plRecordList.Visible = false;
                        this.SendToBack();
                        break;
                }
            }
    
            private void tbxControl_Leave(object sender, EventArgs e)
            {
                if (dgvRecordList.Focused == false)
                {
                    this.Height = tbxControl.Height;
                    this.plRecordList.Visible = false;
                    this.SendToBack();
                }
            }
    
            private void tbxControl_TextChanged(object sender, EventArgs e)
            {
                if (this.tbxControl.Text == "")
                {
                    this.Height = tbxControl.Height;
                    this.plRecordList.Visible = false;
                    this.SendToBack();
                    return;
                }
                //这些情况下,不弹出选择框  
                if (ControlDataSource == null || ControlColumnName == "" || !ControlDataSource.Columns.Contains(ControlColumnName))
                    return;
                //根据用户当前输入的内容,筛选出与内容相符的记录,显示在列表框中  
                this.dgvRecordList.Columns.Clear();
    
                for (int i = 0; i < ControlDataSource.Columns.Count; i++)
                {
                    dgvRecordList.Columns.Add(ControlDataSource.Columns[i].Caption, ControlDataSource.Columns[i].Caption);
                    dgvRecordList.Columns[i].DataPropertyName = ControlDataSource.Columns[i].Caption;
                    dgvRecordList.Columns[i].ReadOnly = true;
                    if (ControlDataSource.Columns[i].Caption == ControlColumnName)
                    {
                        _CurrentCellIndex = i;
                    }
                    if (ControlDataSource.Columns[i].Caption == ControlValueColumnName || ControlDataSource.Columns[i].Caption == "AllSpelling" || ControlDataSource.Columns[i].Caption == "FirstSpelling")
                    {
                        if (!IsViewColValue)
                            dgvRecordList.Columns[i].Visible = false;
                    }
    
                }
                DataRow[] drArr = ControlDataSource.Select(ControlColumnName + " like '%" + tbxControl.Text + "%'");
                if (IsSupportPinYin)
                {
                    drArr = ControlDataSource.Select(ControlColumnName + " like '%" + tbxControl.Text + "%' or SUBSTRING(AllSpelling,1," + tbxControl.Text.Length + ")='" + tbxControl.Text + "' or SUBSTRING(FirstSpelling,1," + tbxControl.Text.Length + ")='" + tbxControl.Text + "'");
                }
                DataTable dtNew = ControlDataSource.Clone();
                for (int i = 0; i < drArr.Length; i++)
                {
                    dtNew.ImportRow(drArr[i]);
    
                }
                dgvRecordList.DataSource = dtNew;
                //如果记录数不为0,则将列表显示出来  
                if (dgvRecordList.Rows.Count == 0)
                {
                    this.Height = tbxControl.Height;
                    this.plRecordList.Visible = false;
                    this.SendToBack();
                }
                else
                {
                    if (dgvRecordList.Rows.Count == 1)
                    {
                        this.Height = tbxControl.Height + 40 * dgvRecordList.Rows.Count;
                        if (this.Width < plRecordList.Width)
                        {
                            this.Width = plRecordList.Width;
                        }
                        this.plRecordList.Visible = true;
                        this.BringToFront();
                    }
                    else if (dgvRecordList.Rows.Count < 8)
                    {
                        this.Height = tbxControl.Height + 24 * dgvRecordList.Rows.Count;
                        if (this.Width < plRecordList.Width)
                        {
                            this.Width = plRecordList.Width;
                        }
                        this.plRecordList.Visible = true;
                        this.BringToFront();
                    }
                    else
                    {
                        this.Height = tbxControl.Height + 208;
                        if (this.Width < plRecordList.Width)
                        {
                            this.Width = plRecordList.Width;
                        }
                        this.plRecordList.Visible = true;
                        this.BringToFront();
                    }
                }
            }
            #endregion
            #region dgvRecordList事件
            private void dgvRecordList_KeyDown(object sender, KeyEventArgs e)
            {
                int currentrowindex = -1;
                if (dgvRecordList.SelectedRows.Count > 0)
                {
                    currentrowindex = dgvRecordList.SelectedRows[0].Index;
                }
                else if (dgvRecordList.SelectedCells.Count > 0)
                {
                    currentrowindex = dgvRecordList.SelectedCells[0].RowIndex;
                }
                switch (e.KeyCode)
                {
                    case Keys.Up:
                        if (plRecordList.Visible)
                        {
                            dgvRecordList.Focus();
                        }
                        if (currentrowindex > 0)
                        {
                            dgvRecordList.Rows[currentrowindex].Selected = false;
                            dgvRecordList.Rows[currentrowindex - 1].Selected = true;
                        }
    
                        break;
                    case Keys.Down:
                        if (plRecordList.Visible)
                        {
                            dgvRecordList.Focus();
                        }
                        if (currentrowindex == -1)
                        {
                            if (dgvRecordList.Rows.Count > 0)
                            {
                                dgvRecordList.Rows[0].Selected = true;
                            }
                        }
                        else if (currentrowindex < dgvRecordList.Rows.Count - 1)
                        {
                            dgvRecordList.Rows[currentrowindex].Selected = false;
                            dgvRecordList.Rows[currentrowindex + 1].Selected = true;
                        }
                        else if (currentrowindex > 0)
                        {
                            dgvRecordList.Rows[currentrowindex].Selected = false;
                            dgvRecordList.Rows[0].Selected = true;
                        }
                        break;
                    case Keys.Enter:
                        if (plRecordList.Visible)
                        {
                            dgvRecordList.Focus();
                        }
                        if (currentrowindex > -1)
                        {
                            tbxControl.Text = dgvRecordList[_CurrentCellIndex, currentrowindex].Value.ToString();
                        }
                        this.Height = tbxControl.Height;
                        this.plRecordList.Visible = false;
                        this.SendToBack();
                        break;
                    case Keys.Space:
                        if (currentrowindex > -1)
                        {
                            tbxControl.Text = dgvRecordList[_CurrentCellIndex, currentrowindex].Value.ToString();
                        }
                        this.Height = tbxControl.Height;
                        this.plRecordList.Visible = false;
                        this.SendToBack();
                        break;
                    case Keys.Escape:
                        this.Height = tbxControl.Height;
                        this.plRecordList.Visible = false;
                        this.SendToBack();
                        break;
                }
            }
    
            private void dgvRecordList_Click(object sender, EventArgs e)
            {
                int currentrowindex = -1;
                if (dgvRecordList.SelectedRows.Count > 0)
                {
                    currentrowindex = dgvRecordList.SelectedRows[0].Index;
                }
                else if (dgvRecordList.SelectedCells.Count > 0)
                {
                    currentrowindex = dgvRecordList.SelectedCells[0].RowIndex;
                }
                if (currentrowindex > -1)
                {
                    tbxControl.Text = dgvRecordList[_CurrentCellIndex, currentrowindex].Value.ToString();
                }
                this.Height = tbxControl.Height;
                this.plRecordList.Visible = false;
                this.SendToBack();
            }
    
            private void dgvRecordList_DoubleClick(object sender, EventArgs e)
            {
                int currentrowindex = -1;
                if (dgvRecordList.SelectedRows.Count > 0)
                {
                    currentrowindex = dgvRecordList.SelectedRows[0].Index;
                }
                else if (dgvRecordList.SelectedCells.Count > 0)
                {
                    currentrowindex = dgvRecordList.SelectedCells[0].RowIndex;
                }
                if (currentrowindex > -1)
                {
                    tbxControl.Text = dgvRecordList[_CurrentCellIndex, currentrowindex].Value.ToString();
                }
                this.Height = tbxControl.Height;
                this.plRecordList.Visible = false;
                this.SendToBack();
            }
    
            private void dgvRecordList_Leave(object sender, EventArgs e)
            {
                if (tbxControl.Focused == false)
                {
                    this.Height = tbxControl.Height;
                    this.plRecordList.Visible = false;
                    this.SendToBack();
                }
            }
            #endregion
            #region 窗体装载
            private void DropDownListSelect_Load(object sender, EventArgs e)
            {
                //控件载入时,将高度缩为与TextBox相等  
                //this.Height = tbxControl.Height;
                #region 控制datagridview的样式
                dgvRecordList.BackColor = System.Drawing.Color.AliceBlue;
                //dgvRecordList.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle;
                dgvRecordList.BackgroundColor = System.Drawing.Color.Beige;
                dgvRecordList.ColumnHeadersVisible = false;
                dgvRecordList.RowHeadersVisible = false;
                this.dgvRecordList.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dgvRecordList.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
                this.dgvRecordList.AllowUserToOrderColumns = false;
                this.dgvRecordList.AllowUserToAddRows = false;
                this.dgvRecordList.AllowUserToDeleteRows = false;
                this.dgvRecordList.AllowUserToResizeRows = false;
                this.dgvRecordList.MultiSelect = false;
                dgvRecordList.BorderStyle = BorderStyle.None;
                dgvRecordList.ScrollBars = ScrollBars.Both;
                dgvRecordList.AllowUserToResizeColumns = false;
                dgvRecordList.AutoGenerateColumns = false;
                this.dgvRecordList.Columns.Clear();
                #endregion
            }
            #endregion
        }
    }
    View Code
  • 相关阅读:
    MSSQL2008 R2 数据库展开报错:值不能为空 参数名:viewInfo
    疑似Windows server自动更新引发的sqlserver宕机
    SQL SERVER 数据库被标记为“可疑”的解决办法
    SQL SERVER日志中报错:等待闩锁时出现超时:类 log_manager
    sqlserver服务因登陆失败无法启动1069
    sqlserver事务日志增长过快
    SQL Server – “Could not connect because the maximum number of ‘1’ user connections has already been reached.”
    SQL SERVER 2012评估期过期
    sqlserver服务启动失败1067
    SQL Server 检测到基于一致性的逻辑 I/O 错误 pageid 不正确(应为 1:10202320,但实际为 0:0)
  • 原文地址:https://www.cnblogs.com/JimZhang/p/3614034.html
Copyright © 2011-2022 走看看