This is my second Custom Control I have made, a dice. The code below has worked 100% in all my tests, program contains 1 dice control and seven buttons, 6 for setting a number directly, and one for randomizing it. Feedback and improvement suggestions are greatly appreciated.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace DiceControl
{
class Dice : Control
{
public int RollNumber { get { return rollNumber; } set { rollNumber = value; Invalidate(); } }
private int rollNumber;
private Point[][] points = new Point[6][];
public Dice(int rollNumber = 0) : base()
{
this.rollNumber = rollNumber;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
Rectangle rc = ClientRectangle;
Pen border = new Pen(Brushes.Black, 3.0f);
Point[] RollOne = { new Point(((rc.Width / 2) - rc.Width / 8), (rc.Height / 2) - rc.Height / 8) };
Point[] RollTwo = { new Point((rc.Left + rc.Width / 4) - rc.Width / 8, (rc.Top + rc.Height / 4) - rc.Width / 8), new Point((rc.Right - rc.Width / 4) - rc.Width / 8, (rc.Bottom - rc.Height / 4) - rc.Width / 8) };
Point[] RollThree = { new Point((rc.Left + rc.Width / 4) - rc.Width / 8, (rc.Top + rc.Height / 4) - rc.Width / 8), new Point((rc.Width / 2) - rc.Width / 8, (rc.Height / 2) - rc.Width / 8), new Point((rc.Right - rc.Width / 4) - rc.Width / 8, (rc.Bottom - rc.Height / 4) - rc.Width / 8) };
Point[] RollFour = { new Point((rc.Left + rc.Width / 4) - rc.Width / 8, (rc.Top + rc.Height / 4) - rc.Width / 8), new Point((rc.Right - rc.Width / 4) - rc.Width / 8, (rc.Top + rc.Height / 4) - rc.Width / 8), new Point((rc.Left + rc.Width / 4) - rc.Width / 8, (rc.Bottom - rc.Height / 4) - rc.Width / 8), new Point((rc.Right - rc.Width / 4) - rc.Width / 8, (rc.Bottom - rc.Height / 4) - rc.Width / 8) };
Point[] RollFive = { new Point((rc.Left + rc.Width / 4) - rc.Width / 8, (rc.Top + rc.Height / 4) - rc.Width / 8), new Point((rc.Right - rc.Width / 4) - rc.Width / 8, (rc.Top + rc.Height / 4) - rc.Width / 8), new Point(((rc.Width / 2) - rc.Width / 8), (rc.Height / 2) - rc.Height / 8), new Point((rc.Left + rc.Width / 4) - rc.Width / 8, (rc.Bottom - rc.Height / 4) - rc.Width / 8), new Point((rc.Right - rc.Width / 4) - rc.Width / 8, (rc.Bottom - rc.Height / 4) - rc.Width / 8) };
Point[] RollSix = { new Point((rc.Left + rc.Width / 4) - rc.Width / 8, (rc.Top + rc.Height / 4) - rc.Width / 8), new Point((rc.Right - rc.Width / 4) - rc.Width / 8, (rc.Top + rc.Height / 4) - rc.Width / 8), new Point((rc.Right - rc.Width / 4) - rc.Width / 8, (rc.Width / 2) - rc.Width / 8), new Point((rc.Left + rc.Width / 4) - rc.Width / 8, (rc.Width / 2) - rc.Width / 8), new Point((rc.Left + rc.Width / 4) - rc.Width / 8, (rc.Bottom - rc.Height / 4) - rc.Width / 8), new Point((rc.Right - rc.Width / 4) - rc.Width / 8, (rc.Bottom - rc.Height / 4) - rc.Width / 8) };
points[0] = RollOne;
points[1] = RollTwo;
points[2] = RollThree;
points[3] = RollFour;
points[4] = RollFive;
points[5] = RollSix;
Point[] edges = {new Point(rc.Left,rc.Top),new Point(rc.Right-1,rc.Top),new Point(rc.Right-1,rc.Bottom-1),new Point(rc.Left,rc.Bottom-1)};
g.FillRectangle(Brushes.White, rc);
if (rollNumber >= 1 && rollNumber <= 6)
{
foreach (Point pt in points[rollNumber-1])
{
g.FillEllipse(Brushes.Black, pt.X, pt.Y, rc.Width / 4, rc.Height / 4);
}
}
else
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
g.DrawString("?", new Font("Arial",(float)rc.Height*0.5f,FontStyle.Bold,GraphicsUnit.Pixel), Brushes.Black, rc, sf);
}
g.DrawPolygon(border, edges);
}
}
class MainWindow : Form
{
Dice d;
Button one, two, three, four, five, six, roll;
public MainWindow()
{
this.Text = "Dice Control";
this.Height = 175;
this.Width = 240;
d = new Dice();
d.Height = 120;
d.Width = 120;
d.Top = 10;
d.Left = 10;
this.Controls.Add(d);
one = new Button();
one.Left = 155;
one.Top = 17;
one.Text = "1";
one.Click += (s, e) => d.RollNumber = 1;
one.Width = 20;
this.Controls.Add(one);
two = new Button();
two.Top = one.Top;
two.Left = one.Right + 5;
two.Text = "2";
two.Click += (s, e) => d.RollNumber = 2;
two.Width = 20;
this.Controls.Add(two);
three = new Button();
three.Top = one.Bottom + 5;
three.Left = one.Left;
three.Text = "3";
three.Click += (s, e) => d.RollNumber = 3;
three.Width = 20;
this.Controls.Add(three);
four = new Button();
four.Top = one.Bottom + 5;
four.Left = three.Right + 5;
four.Text = "4";
four.Click += (s, e) => d.RollNumber = 4;
four.Width = 20;
this.Controls.Add(four);
five = new Button();
five.Top = three.Bottom + 5;
five.Left = three.Left;
five.Text = "5";
five.Click += (s, e) => d.RollNumber = 5;
five.Width = 20;
this.Controls.Add(five);
six = new Button();
six.Top = four.Bottom + 5;
six.Left = five.Right + 5;
six.Text = "6";
six.Click += (s, e) => d.RollNumber = 6;
six.Width = 20;
this.Controls.Add(six);
roll = new Button();
roll.Text = "Roll";
roll.Left = 140;
roll.Top = six.Bottom + 5;
roll.Click += roll_Click;
this.Controls.Add(roll);
}
void roll_Click(object sender, EventArgs e)
{
Random rand = new Random();
d.RollNumber = rand.Next(1, 7);
}
}
class Start
{
public static void Main()
{
MainWindow main = new MainWindow();
Application.EnableVisualStyles();
Application.Run(main);
}
}
}