HI all
I am writing a code for creating a cell with numbers one to nine three in
a row is there any problem with the following code?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace Sudoku
{
public partial class SudokuCell : UserControl
{
public enum CELLMODE
{
LighButton = 1,
DarkButton = 2,
ProtectedButton = 3
}
public delegate void FLeftClick(int ID);
public delegate void FRightClick (int ID);
private FLeftClick _LeftClick;
private FRightClick _RightClick;
private CELLMODE _CellMode = CELLMODE.LighButton;
public CELLMODE CellMode
{
get { return _CellMode; }
set
{
_CellMode = value;
switch (_CellMode)
{
case CELLMODE.DarkButton:
this.BackgroundImage = global::Sudoku.Properties.Resources.SudokuCell01;
break;
case CELLMODE.LighButton:
this.BackgroundImage = global::Sudoku.Properties.Resources.SudokuCell02;
break;
case CELLMODE.ProtectedButton:
this.BackgroundImage = global::Sudoku.Properties.Resources.SudokuCell00;
break;
}
}
}
private int NotesCount = 0;
private int _Caption;
private int _ID;
private System.Windows.Forms.Label[] Notes = new Label[6];
public FLeftClick LeftClick
{
get { return _LeftClick; }
set { _LeftClick = value; }
}
public FRightClick RightClick
{
get { return _RightClick; }
set { _RightClick = value; }
}
public int Caption
{
get { return _Caption; }
set
{
_Caption = value;
if (_Caption == 0)
{
this.lblCaption.Text = "";
}
else
{
this.lblCaption.Text = _Caption.ToString();
ClearNotes();
}
this.Refresh();
}
}
public int ID
{
get { return _ID; }
set { _ID = value; }
}
public SudokuCell()
{
InitializeComponent ();
CreateNotes();
}
private void CreateNotes()
{
int x, y;
for (int i = 0; i < Notes.Length ; i++)
{
Notes[i] = new Label();
Notes[i].AutoSize = false;
Notes[i].BackColor = System.Drawing.Color.Transparent;
Notes[i].ForeColor = System.Drawing.Color.FromArgb(64,64,64);
Notes[i].Font = new System.Drawing.Font("Georgia", 8F, ((System.Drawing.FontStyle)((System.Drawing.FontSt yle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(0)));
if (i < 4)
{
x = i;
y = 0;
}
else
{
x = i - 4;
y = 15;
}
Notes[i].Location = new System.Drawing.Point(11*x, y);
Notes[i].Name = "Notes" + i.ToString();
Notes[i].Size = new System.Drawing.Size(12, 8);
Notes[i].TabIndex = 0;
Notes[i].Tag = i.ToString();
Notes[i].Text = "0";
Notes[i].Cursor = System.Windows.Forms.Cursors.Hand;
Notes[i].Visible = false ;
Notes[i].MouseUp += new MouseEventHandler (NotesClick);
Notes[i].MouseEnter += new EventHandler(this.NotesMouseEnter);
Notes[i].MouseLeave += new EventHandler(this.NotesMouseLeave);
this.Controls.Add(Notes[i]);
}
}
public void Display()
{
this.Visible = true;
this.Refresh();
}
private void lblCaption_MouseUp (object sender, MouseEventArgs e)
{
if (this.CellMode == CELLMODE.ProtectedButton)
{
return;
}
if (e.Button == MouseButtons.Left)
{
LeftClick (ID);
}
else
{
RightClick (ID);
}
}
private void SudokuCell_MouseUp (object sender, MouseEventArgs e)
{
if (this.CellMode == CELLMODE.ProtectedButton)
{
return;
}
if (e.Button == MouseButtons.Left)
{
LeftClick (ID);
}
else
{
RightClick (ID);
}
}
private void NotesClick (Object sender, MouseEventArgs e)
{
Label X = (Label)sender;
this.SuspendLayout();
if (e.Button == MouseButtons.Left)
{
Caption = Convert.ToInt16(X.Text);
ClearNotes();
}
else
{
int j = Convert.ToInt16(X.Tag);
for (int i = j; i < Notes.Length-1; i++)
{
Notes[i].Text = Notes[i + 1].Text;
}
Notes[Notes.Length-1].Text = "0";
ShowNotes();
NotesCount--;
}
this.ResumeLayout(false);
}
public void ClearNotes()
{
if (NotesCount > 0)
{
for (int i = 0; i < Notes.Length; i++)
{
Notes[i].Text = "0";
}
ShowNotes();
NotesCount = 0;
}
}
public void AddNote(int NewNote)
{
if (this.CellMode == CELLMODE.ProtectedButton)
{
return;
}
if (NotesCount >= 0 && NotesCount < Notes.Length)
{
for (int i = 0; i < Notes.Length-1; i++)
{
if (Convert.ToInt16(Notes[i].Text) == NewNote)
{
return ;
}
else if (Notes[i].Text == "0" || Convert.ToInt16(Notes[i].Text) > NewNote)
{
for (int j = Notes.Length-1; j > i; j--)
{
Notes[j].Text = Notes[j-1].Text;
}
Notes[i].Text = NewNote.ToString();
Caption = 0;
NotesCount++;
ShowNotes();
return ;
}
}
Notes[Notes.Length-1].Text = NewNote.ToString();
NotesCount++;
Caption = 0;
ShowNotes();
}
}
private void ShowNotes()
{
for (int i = Notes.Length-1; i >= 0; i--)
{
Notes[i].Visible = (Notes[i].Text != "0");
}
}
private void NotesMouseEnter(object sender, EventArgs e)
{
Label X = (Label)sender;
X.BackColor = System.Drawing.Color.Black;
X.ForeColor = System.Drawing.Color.Yellow;
}
private void NotesMouseLeave(object sender, EventArgs e)
{
Label X = (Label)sender;
X.BackColor = System.Drawing.Color.Transparent;
X.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64);
}
}
}