Advertisement
thilemann

User Control with button and button events

Nov 29th, 2012
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.05 KB | None | 0 0
  1. public class CustomPictureBox : PictureBox
  2. {
  3.     bool hovered = false;
  4.     Button btnClear;
  5.  
  6.     public CustomPictureBox()
  7.     {
  8.         this.MouseEnter += new EventHandler(CustomPictureBox_MouseEnter);
  9.         this.MouseLeave += new EventHandler(CustomPictureBox_MouseLeave);
  10.  
  11.         btnClear = new Button();
  12.         btnClear.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  13.         btnClear.BackColor = System.Drawing.Color.White;
  14.         btnClear.Width = 20;
  15.         btnClear.Height = 20;
  16.         btnClear.Location = new Point(this.Width - btnClear.Width + 1, -1);
  17.         btnClear.Visible = false;
  18.         btnClear.Text = "X";
  19.         this.Controls.Add(btnClear);
  20.     }
  21.  
  22.     void CustomPictureBox_MouseLeave(object sender, EventArgs e)
  23.     {
  24.         Invalidate();
  25.         hovered = false;
  26.         btnClear.Visible = false;
  27.     }
  28.  
  29.     void CustomPictureBox_MouseEnter(object sender, EventArgs e)
  30.     {
  31.         Invalidate();
  32.         hovered = true;
  33.         this.Cursor = System.Windows.Forms.Cursors.Hand;
  34.         btnClear.Visible = true;
  35.     }
  36.  
  37.     protected override void OnPaint(PaintEventArgs e)
  38.     {
  39.         //base.OnPaint(e);
  40.         this.BorderStyle = System.Windows.Forms.BorderStyle.None;
  41.  
  42.         if (hovered)
  43.         {
  44.             Pen pen = new Pen(Color.DarkGray, 2);
  45.             pen.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;
  46.             pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
  47.             Rectangle rectangle = new Rectangle(0, 0, this.Width, this.Height);
  48.             e.Graphics.DrawRectangle(pen, rectangle);
  49.         }
  50.         else if (!hovered)
  51.         {
  52.             Pen pen = new Pen(Color.Gray, 1);
  53.             pen.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;
  54.             pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
  55.             Rectangle rectangle = new Rectangle(0, 0, this.Width -1, this.Height -1);
  56.             e.Graphics.DrawRectangle(pen, rectangle);
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement