程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 制作顯示多列的Combobox控件

制作顯示多列的Combobox控件

編輯:.NET實例教程
一.設計時效果

二.運行時效果

三.簡單談談設計思路

同樣地,類似上篇文章制作DataTimePicker,這裡制作的控件無非是一個復合控件顯示在一個列表框下.主要實現的功能包括雙擊選中行返回,鍵盤回車後返回當前行,將返回行的信息用分隔符號組合起來的字符串顯示在combobox 的text中.感性趣的朋友可以發mail給我.

四.部分源代碼

 



 void FilterTextbox_TextChanged(object sender, EventArgs e)
        ...{
            TextBox txtbox = sender as TextBox;
            if (this.DataSource is DataTable)
            ...{
                DataTable tbl = (DataTable)this.DataSource;
                string s = tbl.Columns[this._ColumnIndex].ColumnName + " like ''%" + txtbox.Text + "%''";
                tbl.DefaultVIEw.RowFilter = s;
            }
            else if (this.DataSource is DataVIEw)
            ...{
                DataView dgv = (DataVIEw)this.DataSource;
                dgv.RowFilter = dgv.Table.Columns[this._ColumnIndex].ColumnName + " like ''%" + txtbox.Text + "%''";
            }
        }

        void jCSShow_CancelButtonClIEntEvent()
        ...{
            this.tsdshow.Hide();
            this.canDropDown = true;
        }

        void jCSShow_OkButtonClickEvent()
        ...{
            GetCurrentValue();
        }
        public string ReturenValue
        ...{
            get ...{ return this.txtbox.Text; }
            set ...{ this.txtbox.Text = value; }
        }
        /**//// <summary>
        /// 分隔符號
        /// </summary>
        public string SeperateString
        ...{
            get ...{ return this._seperatestring; }
            set ...{ this._seperatestring = value; }
        }
        public int ColumnIndex
        ...{
            get ...{ return this._ColumnIndex; }
            set 
            ...{
                if (this.DataSource is DataTable)
                ...{
                    DataTable tbl = (DataTable)this.DataSource;
                    if (value > tbl.Columns.Count - 1)
                    ...{
                        throw new Exception("設置檢索列的索引超出數據源列數!");
                    }
                }
                else if (this.DataSource is DataVIEw)
                ...{
                    DataView dgv = (DataVIEw)this.DataSource;
                    if (value > dgv.Table.Columns.Count - 1)
                    ...{
                        throw new Exception("設置檢索列的索引超出數據源列數!");
                    }
                }
                this._ColumnIndex = value;
            }
        }
        private void GetCurrentValue()
        ...{
            System.Windows.Forms.DataGridViewRow datarowvIEw = this.jCSShow.DataGrid.CurrentRow;
            string s = "";
            for (int i = 0; i < datarowvIEw.Cells.Count; i++)
            ...{
                if (i != datarowvIEw.Cells.Count - 1)
                ...{
                    s += datarowvIEw.Cells[i].Value.ToString() + this._seperatestring;
                }
                else
                ...{
                    s += datarowvIEw.Cells[i].Value.ToString();
                }
            }
            if (s.Length != 0)
            ...{
                this.txtbox.Text = s;
                this.tsdshow.Hide();

                this.txtbox.Focus();
                this.canDropDown = true;
            }
        }
        void DataGrid_MouseDown(object sender, MouseEventArgs e)
        ...{
            if (e.Button == MouseButtons.Left && e.Clicks == 2)
            ...{
                System.Windows.Forms.DataGridVIEw.HitTestInfo hittest = this.jCSShow.DataGrid.HitTest(e.X, e.Y);
                if (hittest.Type == DataGridVIEwHitTestType.Cell)
                ...{
                    GetCurrentValue();
                }
            }
        }

        void txtbox_MouseDown(object sender, MouseEventArgs e)
        ...{
            this.canDropDown = true;
        }
        void gridvIEw_KeyDown(object sender, KeyEventArgs e)
        ...{
            if (e.KeyData == Keys.Enter)
            ...{
                GetCurrentValue();
            }
        }

       

        public Object DataSource
        ...{
            get ...{ return this.jCSShow.DataSource; }
            set 
            ...{
                this.jCSShow.DataSource = value;
            }
        }
        protected override void OnResize(EventArgs e)
        ...{
            base.OnResize(e);
          
            if (this.Width > 21)
            this.txtbox.Width = this.Width - 21;
            dropdownrect = new Rectangle(this.txtbox.Width, 0, 18, 21);
            this.Height = 21;
        }
        protected override void OnPaint(PaintEventArgs e)
        ...{
            base.OnPaint(e);
            Graphics g = e.Graphics;
            if (this._ismousedown)
            ...{
                ControlPaint.DrawComboButton(g, this.dropdownrect, ButtonState.Pushed);
            }
            else
            ...{
                ControlPaint.DrawComboButton(g, this.dropdownrect, ButtonState.Normal);
            }
        }
        protected override void OnMouseDown(MouseEventArgs e)
        ...{
            base.OnMouseDown(e);
            if (this.dropdownrect.Contains(new Point(e.X, e.Y)))
            ...{
                this._ismousedown = true;
                this.Invalidate(this.dropdownrect);
                ShowDataGridVIEw();
            }
        }

        protected override void OnMouseUp(MouseEventArgs e)
        ...{
            base.OnMouseUp(e);
           
            if (this._ismousedown)
            ...{
                this._ismousedown = false;
                this.Invalidate(this.dropdownrect);
            }
        }
  
        private void ShowDataGridVIEw()
        ...{
            if (this.canDropDown)
            ...{
                Point location;
                location = this.Parent.PointToScreen(this.Location);
                location = this.FindForm().PointToClIEnt(location);
                location.Y = location.Y + this.Height;
                this.tsdshow.Show(this.FindForm(), location);
                this.jCSShow.DataGrid.Focus();
            }
            else if (!this.canDropDown && this.tsdshow.Visible)
            ...{
                tsdshow.Hide();
                this.Focus();
            }
            canDropDown = !canDropDown;
        }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved