int i; string someValue = "123"; if (Int32.TryParse(someValue, out i)) { // i now = 123 } else MessageBox.Show("Invalid value, must be numeric");
Looks like you are making progress.
You can create a single event handler for the label object to do this, however you could just as easily tag the event into the Drag Drop onto the label.
If you want to allow the user to actually type values into the object, you should replace the label component(s) with a TextBox component.
To turn a string from the label or TextBox (any string) into an integer you can use the parsing methods of that type.
C# Syntax (Toggle Plain Text)
int i; string someValue = "123"; if (Int32.TryParse(someValue, out i)) { // i now = 123 } else MessageBox.Show("Invalid value, must be numeric");
To control the size of your labels, you should set the AutoSize property to false.
// Jerry

private void Form1_Load(object sender, EventArgs e) { int[,] mat = new int[9, 9]; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { Label lbl = new Label(); lbl.Text = (i + 1).ToString(); mat[i,j] = Convert.ToInt32(lbl.Text); lbl.Location = new Point(120 + (j * 100), 70 + (i * 40)); lbl.Size = new Size(label1.Width, label1.Height); lbl.DragDrop += label1_DragDrop; lbl.DragOver += label1_DragOver; lbl.AllowDrop = true; panel.Controls.Add(lbl); } }
(checked it).
lbl.Tag = string.Format("{0}|{1}",i,j); // use of the pipe character as a delimiter is just my choice, you can use any charcter.
private void label1_DragDrop(object sender, DragEventArgs e) { Label target = sender as Label; Button btn = e.Data.GetData(typeof(Button)) as Button; target.Text = btn.Text; string address = (string)target.Tag; int i = Convert.Int32(address.Split('|')[0]); int j = Convert.Int32(address.Split('|')[1]); mat[i,j] = Convert.Int32(target.Text); }

private void label1_DragDrop(object sender, DragEventArgs e) { Label target = sender as Label; Button btn = e.Data.GetData(typeof(Button)) as Button; target.Text = btn.Text; string address = (string)target.Tag; int i = Convert.ToInt32(address.Split('|')[0]); int j = Convert.ToInt32(address.Split('|')[1]); _mat[i, j] = Convert.ToInt32(target.Text); } private void Form1_Load(object sender, EventArgs e) { int[,] _mat = new int[9, 9]; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { Label lbl = new Label(); lbl.Text = (i + 1).ToString(); _mat[i,j] = i+1; lbl.Location = new Point(120 + (j * 100), 70 + (i * 40)); lbl.Size = new Size(label1.Width, label1.Height); lbl.DragDrop += label1_DragDrop; lbl.DragOver += label1_DragOver; lbl.AllowDrop = true; lbl.Tag = string.Format("{0}|{1}", i, j); panel.Controls.Add(lbl); } }
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
// Private class declarations
private int[,] _mat new int[9, 9];
public Form1()
{
InitializeComponent();
}
...
private void button10_Click(object sender, EventArgs e) { public bool isvalid(int []ar) { for (int i=0;i<9;i++) {for (int j=0;j<9;j++) if ((j!=i)&&(ar[i]==ar[j])) return false; }return true; } public bool row(int [,]_mat,int rownum) { int [] ar=new int [9]; for (int j=0;j<mat.GetLength(1);j++) ar[j]=_mat[rownum,j]; if (isvalid(ar)) return true; return false; } public bool col(int [,]_mat,int colnum) { int [] ar=new int [9]; for (int i=0;i<mat.GetLength(1);i++) ar[i]=_mat[i,colnum]; if (isvalid(ar)) return true; return false; } public bool square(int [,]_mat,int rownum,int colnum) { int [] ar=new int [9]; int k=0; for (int i=0;i<3;i++) { for (int j=0;j<3;j++) { ar[k]=_mat[rownum+i,colnum+j]; k++; } } if (isvalid(ar)) return true; return false; } public void printrows(int [,]_mat) { for (int i = 0; i < 9; i++) if (row(_mat, i)==false) MessageBox.Show("|Row {0} is Faulty!|", i+1); } public void printcols(int[,]_mat) { for (int j = 0; j < 9; j++) if (col(_mat, j)==false) MessageBox.Show("|Column {0} is Faulty!|", j+1); } public void printsquares(int [,]_mat) { int k = 0; for (int i = 0; i < 9; i=i+3) { for (int j = 0; j < 9; j=j+3) { k++; if (square(_mat, i,j)==false) MessageBox.Show("|Square {0} is Faulty!|", k); } } } static void main(System[]args) { printrows(_mat); printcols(_mat); printsquares(_mat); } }
?
)
static void Main(string[] args) { }
[STAThread] static void Main() { }
private bool IsValid(int[] ar) { for (int i = 0; i < ar.Length; i++) { for (int j = 0; j < ar.Length; j++) if ((j != i) && (ar[i] == ar[j])) return false; } return true; }
private bool CheckRow(int rownum) { int[] ar = new int[9]; for (int j = 0; j < _mat.GetLength(1); j++) ar[j] = _mat[rownum, j]; return IsValid(ar); }
private bool CheckCol(int colnum) { int[] ar = new int[9]; for (int i = 0; i < _mat.GetLength(1); i++) ar[i] = _mat[i, colnum]; return IsValid(ar); } private bool CheckSquare(int rownum, int colnum) { int[] ar = new int[9]; int k = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { ar[k] = _mat[rownum + i, colnum + j]; k++; } } return IsValid(ar); }
private void btnCheck_Click(object sender, EventArgs e) { int k = 0; string faults = string.Empty; for (int i = 0; i < 9; i++) { if (!CheckRow(i)) faults += string.Format("Row: {0} is Faulty!\r\n", i); for (int j = 0; j < 9; j++) { if (!CheckCol(j)) { faults += string.Format("Col: {0} is Faulty!\r\n", i); break; } } } for (int i = 0; i < 9; i = i + 3) { for (int j = 0; j < 9; j = j + 3) { k++; if (!CheckSquare(i, j)) faults += string.Format("Square: {0} is Faulty!\r\n", k); } } if (!string.IsNullOrEmpty(faults)) MessageBox.Show(faults, "Faults"); else MessageBox.Show("Success"); }
and more important i actually understood everything,I learned so many new things, it's so great! i love programming XD
lbl.Font = new Font("verdana", 12, FontStyle.Bold)

, so I'm safe , thank You for your concern.| DaniWeb Message | |
| Cancel Changes | |