Esempio n. 1
0
 /// <summary>
 /// 项目匹配事件
 /// </summary>
 /// <param name="e"></param>
 protected virtual void OnMatch(MatchEventArgs e)
 {
     if (this.Match != null)
     {
         this.Match(this, e);
     }
 }
        /// <summary>
        /// 根据匹配规则,绑定下拉列表, 返回完全匹配项
        /// </summary>
        /// <returns>完全匹配项</returns>
        private List<object> MatchAndSetListItems()
        {
            List<object> result = new List<object>();
            this.box.DisplayMember = this.DisplayMember;
            this.box.ValueMember = this.ValueMember;

            this.box.BeginUpdate();
            this.box.Items.Clear();
            Graphics gh = this.box.CreateGraphics();
            SizeF newSize = new SizeF();

            if ( this.Items != null )
            {
                for ( int i = 0; i < this.Items.Count; i++ )
                {
                    object obj = this.Items[i];

                    if ( obj != null )
                    {
                        object valObj = FilterItemOnProperty( obj, this.ValueMember );
                        string valueText = valObj == null ? "" : valObj.ToString();
                        string displayText = this.innerListBox.GetItemText( obj );

                        MatchEventArgs args = new MatchEventArgs();
                        args.Item = obj;
                        args.MatchText = this.Text;
                        args.MatchResult = displayText.StartsWith( this.Text ) || valueText.StartsWith( this.Text );
                        args.EqualResult = displayText == this.Text || valueText == this.Text;

                        this.OnMatch( args );

                        //部分匹配
                        if ( args.MatchResult )
                        {
                            this.box.Items.Add( obj );

                            if ( this.AutoDropWidth )
                            {
                                //测量当前项目的宽度
                                SizeF currentSize = gh.MeasureString( displayText, this.box.Font );
                                newSize = newSize.Width > currentSize.Width ? newSize : currentSize;
                            }
                        }

                        //完全相等
                        if ( args.EqualResult )
                        {
                            result.Add( obj );
                        }
                    }
                }

                this.box.SelectedItem = this.SelectedItem;
            }

            this.box.EndUpdate();

            //计算宽度
            newSize.Width += (this.box.Items.Count * this.box.ItemHeight > this.box.Height ? 18 : 0);
            this.box.Width = Convert.ToInt32( newSize.Width > this.Width - 2 ? newSize.Width : this.Width -2 );
            //设置列表高度, 小于 DropHeight, 收缩高度到刚好容下所有项目, 大于 DropHeight 或列表为空, 则高度为 DropHeight.
            int sumHight = this.box.Items.Count == 0 ? this.DropHeight : this.box.Items.Count * this.box.ItemHeight;
            this.box.Height = sumHight < this.DropHeight ? sumHight : this.DropHeight;

            return result;
        }
 /// <summary>
 /// 项目匹配事件
 /// </summary>
 /// <param name="e"></param>
 protected virtual void OnMatch( MatchEventArgs e )
 {
     if ( this.Match != null )
     {
         this.Match( this, e );
     }
 }
Esempio n. 4
0
        /// <summary>
        /// 根据匹配规则,绑定下拉列表, 返回完全匹配项
        /// </summary>
        /// <returns>完全匹配项</returns>
        private List <object> MatchAndSetListItems()
        {
            List <object> result = new List <object>();

            this.box.DisplayMember = this.DisplayMember;
            this.box.ValueMember   = this.ValueMember;

            this.box.BeginUpdate();
            this.box.Items.Clear();
            Graphics gh      = this.box.CreateGraphics();
            SizeF    newSize = new SizeF();

            if (this.Items != null)
            {
                for (int i = 0; i < this.Items.Count; i++)
                {
                    object obj = this.Items[i];

                    if (obj != null)
                    {
                        object valObj      = FilterItemOnProperty(obj, this.ValueMember);
                        string valueText   = valObj == null ? "" : valObj.ToString();
                        string displayText = this.innerListBox.GetItemText(obj);

                        MatchEventArgs args = new MatchEventArgs();
                        args.Item        = obj;
                        args.MatchText   = this.Text;
                        args.MatchResult = displayText.StartsWith(this.Text) || valueText.StartsWith(this.Text);
                        args.EqualResult = displayText == this.Text || valueText == this.Text;

                        this.OnMatch(args);

                        //部分匹配
                        if (args.MatchResult)
                        {
                            this.box.Items.Add(obj);

                            if (this.AutoDropWidth)
                            {
                                //测量当前项目的宽度
                                SizeF currentSize = gh.MeasureString(displayText, this.box.Font);
                                newSize = newSize.Width > currentSize.Width ? newSize : currentSize;
                            }
                        }

                        //完全相等
                        if (args.EqualResult)
                        {
                            result.Add(obj);
                        }
                    }
                }

                this.box.SelectedItem = this.SelectedItem;
            }

            this.box.EndUpdate();

            //计算宽度
            newSize.Width += (this.box.Items.Count * this.box.ItemHeight > this.box.Height ? 18 : 0);
            this.box.Width = Convert.ToInt32(newSize.Width > this.Width - 2 ? newSize.Width : this.Width - 2);
            //设置列表高度, 小于 DropHeight, 收缩高度到刚好容下所有项目, 大于 DropHeight 或列表为空, 则高度为 DropHeight.
            int sumHight = this.box.Items.Count == 0 ? this.DropHeight : this.box.Items.Count * this.box.ItemHeight;

            this.box.Height = sumHight < this.DropHeight ? sumHight : this.DropHeight;

            return(result);
        }