Advertisement
andypaxo

Winforms button sample

Jul 5th, 2012
957
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.06 KB | None | 0 0
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5.  
  6. public class MyClass
  7. {
  8.     Button btn;
  9.     bool isValid = true;
  10.    
  11.     public static void Main()
  12.     {
  13.         new MyClass();
  14.     }
  15.    
  16.     public MyClass()
  17.     {
  18.         var form = new Form();
  19.         btn = new Button();
  20.         btn.Size = new Size(50, 50);
  21.         btn.Paint += Button_Paint;
  22.         btn.Click += Button_Click;
  23.         form.Controls.Add(btn);
  24.         Application.Run(form);
  25.     }
  26.  
  27.     private void Button_Paint(object sender, PaintEventArgs e)
  28.     {
  29.         Graphics g = e.Graphics;
  30.         if (isValid) {
  31.             g.DrawString("This is a diagonal line drawn on the control",
  32.                 new Font("Arial", 10), System.Drawing.Brushes.Blue, new Point(30, 30));
  33.             g.DrawLine(System.Drawing.Pens.Red, btn.Left, btn.Top,
  34.                 btn.Right, btn.Bottom);
  35.         }
  36.         else {
  37.             g.FillRectangle(
  38.                 new LinearGradientBrush(PointF.Empty, new PointF(0, btn.Height), Color.White, Color.Red),
  39.                 new RectangleF(PointF.Empty, btn.Size));
  40.         }
  41.     }
  42.  
  43.     private void Button_Click(object sender, EventArgs e)
  44.     {
  45.         isValid = false;
  46.         btn.Invalidate();
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement