65.9K
CodeProject 正在变化。 阅读更多。
Home

高级 ListBox

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.19/5 (9投票s)

2006年12月29日

viewsIcon

45769

downloadIcon

1125

带有标题并且可以随意移动的高级 ListBox。

Sample Image - ListBox0.jpg

引言

有时您可能需要一个带有标题并且能够在运行时通过用户点击移动的列表框!
在本文中,我们设计一个带有标题可释放可移动的列表框,...

背景

要设计一个带有标题的列表框,首先我们需要一个面板,然后在其中添加一个 CheckedListBox。
    public class tblListBox : Panel
    {
        private System.Windows.Forms.CheckedListBox listBox1;
        // Class Detail...
    }

为了实现可移动、可释放、标题文本、标题颜色等功能,我们需要一些私有变量。

        bool     isDrag = false;            // For Movable feature
        long     _tblID;                // for table ID
        string     _hText;                // For Header text
        Color     _HeaderColor = Color.Blue;    // For Header Back Color
        bool     _Disposable = false;        // For Disposable feature
        bool     _Movable = false;        // For Movable feature

构造函数

        public tblListBox(long TableID)
        {
            InitializeComponent();
            _tblID = TableID;
            listBox1.Size = new Size(this.Size.Width, this.Size.Height - 30);
            listBox1.Top = 30;
            this.Controls.Add(listBox1);
            this.MouseMove += new MouseEventHandler(ListMouseMove);
            this.MouseDown += new MouseEventHandler(ListMouseDown);
            this.MouseUp += new MouseEventHandler(ListMouseUp);
            this.DoubleClick += new EventHandler(tblListBox_DoubleClick);
        }

实现类


        public int SelectedIndex
        {
            get { return listBox1.SelectedIndex; }
        }
        public bool Disposable
        {
            get { return _Disposable; }
            set { _Disposable = value; }
        }
        public bool Movable
        {
            get { return _Movable; }
            set { _Movable = value; }
        }
        
        public string HeaderText
        {
            get { return _hText; }
            set { _hText = value; }
        }
        public long TableID
        {
            get { return _tblID; }
        }
        public Color HeaderColor
        {
            get { return _HeaderColor; }
            set { _HeaderColor = value; }
        }

现在实现列表框的两个属性:Items、GetItemChecked(int)

        public bool GetItemChecked(int index)
        {
            return listBox1.GetItemChecked(index);
        }
        
        public System.Windows.Forms.ListBox.ObjectCollection Items
        {
            get { return listBox1.Items; }
        }

现在我们需要在面板调整大小时,列表框也随之调整大小并绘制新的特性!

        protected override void OnResize(EventArgs eventargs)
        {
            listBox1.Size = new Size(this.Size.Width, this.Size.Height - 30);
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            
            Brush b=new SolidBrush(_HeaderColor);
            // Paint orginall
            base.OnPaint(e);
            // Draw background and fill with _HeaderColor
            e.Graphics.FillRectangle(b, e.ClipRectangle);
            // Determine size of string
            SizeF strSize=e.Graphics.MeasureString(_hText,new Font("Tahoma",8,FontStyle.Bold));
            // Align Center
            float left = e.ClipRectangle.Width/2 - strSize.Width / 2;
            // Draw string on Center of Header
            e.Graphics.DrawString(_hText, new Font("Tahoma", 8, FontStyle.Bold), System.Drawing.Brushes.Yellow, new PointF(left, 10));
        }

实现可移动功能

        void ListMouseMove(object sender, MouseEventArgs e)
        {
            if (!_Movable) return;
            tblListBox list = (tblListBox)sender;
            if (isDrag)
            {              
                Point endPoint = this.PointToScreen(new Point(e.X - this.Parent.Location.X-100, e.Y - this.Parent.Location.Y-100));
                list.Location = endPoint;
            }
        }
        void ListMouseUp(object sender, MouseEventArgs e)
        {
            if (!_Movable) return;
            isDrag = false;
        }
        void ListMouseDown(object sender, MouseEventArgs e)
        {
            if (!_Movable) return;
            if (e.Button == MouseButtons.Left)
            {
                isDrag = true;
            }
            Control control = (Control)sender;
        }        

属性

Properties in Visual Studio 2005
© . All rights reserved.