当前位置:编程学习 > asp >>

.net WinForm用户控件开发--(3)可多选的下拉列表框

     这一节给大家演示一个具有多选功能的下拉列表框,其实就是一个文本框和checkboxlist组合实现的用户控件,换个角度来实现自定义控件。
先来看下效果图吧,我会分几个步骤来具体讲解这个文本框的实现。

                        \               

    1.界面实现

     我们先来看下界面效果怎样实现,然后再讲解具体的功能性实现。

      先创建一个用户控件,当然是继承UserControl类,这个界面需要用到以下控件来实现

     下拉按钮:是一个button控件

     文本框:显示选中的内容,

     多选列表框: checkboxlist

     全选/取消:是两个LABEL控件

      右下角的黑三角:是一个LABEL控件,可实现拉长下拉列表框.

      窗体:点击下拉按钮,实际是弹出一个窗体,下拉列表框中的控件都在这个窗体中显示

    首先看下下拉按钮的实现

   

[csharp]
/// <summary>  
    /// 重写BUTTON  
    /// </summary>  
    public class ButtonS : Button 
    { 
        public ButtonS() 
        { 
            //防止重绘控件出现闪烁  
            this.SetStyle(ControlStyles.UserPaint, true); 
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); 
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); 
        } 
        ButtonState state; 
        //当按钮被按下  
        protected override void OnMouseDown(MouseEventArgs mevent) 
        { 
            state = ButtonState.Pushed; 
            base.OnMouseDown(mevent); 
        } 
 
        //当按钮被释放  
        protected override void OnMouseUp(MouseEventArgs mevent) 
        { 
            state = ButtonState.Normal; 
            base.OnMouseUp(mevent); 
        } 
 
        protected override void OnPaint(PaintEventArgs pevent) 
        { 
            base.OnPaint(pevent); 
            System.Windows.Forms.ControlPaint.DrawComboButton(pevent.Graphics, 0, 0, this.Width, this.Height, state); 
        } 
    } 

/// <summary>
    /// 重写BUTTON
    /// </summary>
    public class ButtonS : Button
    {
        public ButtonS()
        {
            //防止重绘控件出现闪烁
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        }
        ButtonState state;
        //当按钮被按下
        protected override void OnMouseDown(MouseEventArgs mevent)
        {
            state = ButtonState.Pushed;
            base.OnMouseDown(mevent);
        }

        //当按钮被释放
        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            state = ButtonState.Normal;
            base.OnMouseUp(mevent);
        }

        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            System.Windows.Forms.ControlPaint.DrawComboButton(pevent.Graphics, 0, 0, this.Width, this.Height, state);
        }
    }
   接下来再看下具有拉动功能的LABEL

  

[csharp]
/// <summary>  
    /// 重写LABEL  
    /// </summary>  
    public class LabelS : Label 
    { 
        public LabelS() 
        { 
            //控件绘制的时候减少闪烁  
            this.SetStyle(ControlStyles.UserPaint, true); 
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); 
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); 
        } 
 
        protected override void OnPaint(PaintEventArgs e) 
        { 
            base.OnPaint(e); 
            System.Windows.Forms.ControlPaint.DrawSizeGrip(e.Graphics, Color.Black, 1, 0, this.Size.Width, this.Size.Height); 
        } 
    } 

/// <summary>
    /// 重写LABEL
    /// </summary>
    public class

补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,