public class TextBoxEx : System.Windows.Forms.TextBox
  {
    DrawState drawState = DrawState.Normal;    /// <summary>
    /// 
    /// </summary>
    public TextBoxEx()
    {
      // Make sure we have the 3D look setting which is the one
      // we are going to paint over
      BorderStyle = BorderStyle.Fixed3D;
    }
    /// <summary>
    /// 
    /// </summary>
    public new BorderStyle BorderStyle 
    {
      // Don't let the user change this property
      // because we are counting on the extra pixels
      // than the 3D look adds to the edit control size
      // to do our painting
      get { return base.BorderStyle; } 
      set 
      {
        if ( value != BorderStyle.Fixed3D )
        {
          // Throw an exception to tell the user
          // that this property needs to be Fixe3D if 
          // he is to use this class
          string message = "BorderStyle can only be Fixed3D for this class";
          ArgumentException argumentException = new ArgumentException("BorderStyle", message);
          throw(argumentException);
        }
        else 
          base.BorderStyle = value;
      }
    }
  
    protected override void OnMouseEnter(EventArgs e)
    {
      // Set state to hot
      base.OnMouseEnter(e);
      drawState = DrawState.Hover;
      Invalidate();
    }    protected override void OnMouseLeave(EventArgs e)
    {
      // Set state to Normal
      base.OnMouseLeave(e);
      if ( !ContainsFocus )
      {
        drawState = DrawState.Normal;
        Invalidate();
      }
    }
      
    protected override void OnGotFocus(EventArgs e)
    {
      // Set state to Hot
      base.OnGotFocus(e);
      drawState = DrawState.Hover;
      Invalidate();
    }
        
    protected override void OnLostFocus(EventArgs e)
    {
      // Set state to Normal
      base.OnLostFocus(e);
      drawState = DrawState.Normal;
      Invalidate();
    }    protected override void OnPaint(PaintEventArgs e)
    {
      base.OnPaint(e);
      // Get window area
      RECT rc = new RECT();
      User32.GetWindowRect(Handle, ref rc);
      // Convert to Rectangle
      Rectangle rect = new Rectangle(0, 0, rc.right - rc.left, rc.bottom - rc.top);      // Create DC for the whole edit window instead of just for the client area
      IntPtr hDC = User32.GetWindowDC(Handle);
      
      using (Graphics g = Graphics.FromHdc(hDC))
      {
        // This rectangle is always drawn for any state
        using ( Pen windowPen = new Pen(SystemBrushes.Window) )
        {
          g.DrawRectangle(windowPen, rect.Left+1, rect.Top+1, rect.Width-3, rect.Height-3);
        }
        
        if ( drawState == DrawState.Normal )
        {
          // draw SystemColos.Window rectangle
          using ( Pen windowPen = new Pen(SystemBrushes.Window) )
          {
            g.DrawRectangle(windowPen, rect.Left, rect.Top, rect.Width-1, rect.Height-1);
          }
        }
        else if ( drawState == DrawState.Hover )
        {
          // draw highlighted rectangle
          g.DrawRectangle(SystemPens.Highlight, rect.Left, rect.Top, rect.Width-1, rect.Height-1);
        }
        else if ( drawState == DrawState.Disable )
        {
          // draw highlighted rectangle
          g.FillRectangle(SystemBrushes.Window, rect);
          Size textSize = DrawHelper.GetTextSize(Text, Font);
          Point point = new Point(rect.Left+1, rect.Top + (rect.Height - textSize.Height)/2);
          g.DrawString(Text, Font, SystemBrushes.ControlDark, point); 
        }
      }      // Release DC
      User32.ReleaseDC(Handle, hDC);
            
    }
  }

解决方案 »

  1.   

    上面就重写了几个事件
    protected override void OnMouseEnter(EventArgs e)
      protected override void OnMouseLeave(EventArgs e)
        protected override void OnGotFocus(EventArgs e)
      protected override void OnLostFocus(EventArgs e)
    protected override void OnPaint(PaintEventArgs e)
      

  2.   

    namespace test
    {
      public delegate void UDEventHandler (object sender, UDEventArgs tea);  public class UDEventArgs : EventArgs
      {
        private string mystring = null;
        public UDEventArgy(string mystring)
        {
          this.mystring = mystring;
        }    public string MyString
        {
          get{
            return mystring;
          }
        }
      }  public class mycontrol : usercontrol
      {
        public event EventHandler myEvent;
        public event UDEventHandler myUDEvent;
      }
    }
      

  3.   

    怎麼加自定義事件
    比如我要加一個Button 在Usercontrol 的左上角
    當單機Button時,關閉UserControl
      

  4.   

    关闭UserControl?是不是隐藏啊
    这样行吗
    button_click(...)
    {
    this.visible = false;
    }