| | |
Hello,windows applications,help please
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2009
Posts: 44
Reputation:
Solved Threads: 0
Wow great ! thank you!
now it looks like a soduko(more or less) :}
ok now I'll try to implant my program from console app to this.
I'll report if i encounter problems.
Thank you so much ,really!
JooClops
EDIT:
Can i get the information from the labels as numbers? i need to gather them to a matrix.. but they are strings XD
and i want to get the infromation from the label when it changes, so i need to make an event handler that if lbl.text.change==true mat[i,j]=lbl.text?
now it looks like a soduko(more or less) :}
ok now I'll try to implant my program from console app to this.
I'll report if i encounter problems.
Thank you so much ,really!
JooClops
EDIT:
Can i get the information from the labels as numbers? i need to gather them to a matrix.. but they are strings XD
and i want to get the infromation from the label when it changes, so i need to make an event handler that if lbl.text.change==true mat[i,j]=lbl.text?
Last edited by JooClops; Apr 4th, 2009 at 11:36 am.
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
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.
To control the size of your labels, you should set the AutoSize property to false.
// Jerry
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
•
•
Join Date: Apr 2009
Posts: 44
Reputation:
Solved Threads: 0
•
•
•
•
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
first of all i cant stop telling you how much i appreciate your help, I've started this from almost 0% knowledge on winApp, basically I know some basic stuff and that's all, your really a life saver.
now OnTopic:
"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."
There are 2 reasons why this is not an option for me:
1.Every box in the soduku table should hold a number between 1 to 9,so if i'll make it as a textbox, I'll have to do more "input checks"...
2.Drag n' Drop sounds cooler, and as far as i knows in the past years nobody done that in our school,so it is more special than normal text box.
I had no luck succeeding to convert to integer with the method you wrote... don't know why, i must be really noob

but i searched on the web and i found the convert to int method,
so this is the code i have so far:
C# Syntax (Toggle Plain 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] = 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).I've also attached a picture of how the form looks like,the drag n' drop is easy cause i made the size of the label fit exactly to the table's box size(done the autosize=false as you've said!).
Now some problems i thought of:
1.once the label is changed ,how can i change it in tthe Matrix , i need to get it's place,which i can't think of a way to do it :\
2.in a later stage when i'll have to make buttons for "sumbit to check" and "clear", if the "submit" is pressed i'll have to make an event handler,and than we can say i'll just need to rewrite my program from console app to the method,will i be able to send the matrix there?
Thank You,and sorry for my noob questions

JooClops
can't attach it, so here is a link [img=http://img207.imageshack.us/img207/6326/34666300.jpg]
Last edited by JooClops; Apr 4th, 2009 at 2:05 pm.
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
Okay,
No problem on the newB questions, everyone is a NewB at some point.
Does not look like you need to convert the text to integer, the (i+1) that you are assigning to the Text, is already an integer, so just set the array value to that value: mat[i,j] = i +1;
"1.once the label is changed ,how can i change it in tthe Matrix , i need to get it's place,which i can't think of a way to do it :"
For this, I will show you another little trick called Tag.
All components (well 99% of them) have a Tag property of type object. You can store anything you want in there. For this (and to keep it simple) lets place the address in this property as a string:
Now to use this you will need to split the address back out and convert them back to Integer.
BTW: you will learn this later, but code formatting will help your code stand out. For example all class private variables should start with an underscore (if you wish to follow the Microsoft coding standards). Example: your mat array is a class declared private variable. To give it that professional touch you should name it _mat. This way as someone is looking at your code and see a variable that starts with an underscore, they will know it is a class based private variable. It is just a standards thing, and no one can force you to do it (unless you work where I do
// Jerry
No problem on the newB questions, everyone is a NewB at some point.
Does not look like you need to convert the text to integer, the (i+1) that you are assigning to the Text, is already an integer, so just set the array value to that value: mat[i,j] = i +1;
"1.once the label is changed ,how can i change it in tthe Matrix , i need to get it's place,which i can't think of a way to do it :"
For this, I will show you another little trick called Tag.
All components (well 99% of them) have a Tag property of type object. You can store anything you want in there. For this (and to keep it simple) lets place the address in this property as a string:
C# Syntax (Toggle Plain Text)
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.
Now to use this you will need to split the address back out and convert them back to Integer.
C# Syntax (Toggle Plain 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.Int32(address.Split('|')[0]); int j = Convert.Int32(address.Split('|')[1]); mat[i,j] = Convert.Int32(target.Text); }
BTW: you will learn this later, but code formatting will help your code stand out. For example all class private variables should start with an underscore (if you wish to follow the Microsoft coding standards). Example: your mat array is a class declared private variable. To give it that professional touch you should name it _mat. This way as someone is looking at your code and see a variable that starts with an underscore, they will know it is a class based private variable. It is just a standards thing, and no one can force you to do it (unless you work where I do

// Jerry
•
•
Join Date: Apr 2009
Posts: 44
Reputation:
Solved Threads: 0
Hello again Jerry,
Thank you for the tricks :> and advices(from now on i use underscore before a class private variable) I'm learning so many new things(and i like it!) :>
Now,
I want to make my matrix not private, so i can use it and change it in every function i want(I'll need to use it when checking it[On sumbit]) how can i do that? where do i declare this statement:
" int[,] _mat = new int[9, 9];"
this is what i have so far(and it doesnt work cause the matrix is only declared in the form1_load function) maybe making it public instead of private will help... or not XD..
Thank You,
JooClops
Thank you for the tricks :> and advices(from now on i use underscore before a class private variable) I'm learning so many new things(and i like it!) :>
Now,
I want to make my matrix not private, so i can use it and change it in every function i want(I'll need to use it when checking it[On sumbit]) how can i do that? where do i declare this statement:
" int[,] _mat = new int[9, 9];"
C# Syntax (Toggle Plain 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); } }
Thank You,
JooClops
Last edited by JooClops; Apr 4th, 2009 at 6:00 pm.
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
You need to move the declaration to the top of the class.
When declared inside the class but outside of any methods it is a class variable. Any method within the class can access it.
Use public variables when classes outside of this class need to have access to it. You do not have any of those yet.
Also something to know is that any method declared inside of a method is only accessable from within the method it is declared. Also, the instance will die the moment the method exits.
BTW, what country are you residing ? just curious as to the time difference. I am in Idaho USA
// Jerry
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
// Private class declarations
private int[,] _mat new int[9, 9];
public Form1()
{
InitializeComponent();
}
...When declared inside the class but outside of any methods it is a class variable. Any method within the class can access it.
Use public variables when classes outside of this class need to have access to it. You do not have any of those yet.
Also something to know is that any method declared inside of a method is only accessable from within the method it is declared. Also, the instance will die the moment the method exits.
BTW, what country are you residing ? just curious as to the time difference. I am in Idaho USA
// Jerry
Last edited by JerryShaw; Apr 4th, 2009 at 7:34 pm.
•
•
Join Date: Apr 2009
Posts: 44
Reputation:
Solved Threads: 0
Hi,
I'm not from the US :< I currently live in Israel.
and im 16.5 years old
Now,
I've made two new buttons:
1."Check me".
2."Clear".
about the Clear:
Instead of thinking a lot and writing a lot, I will just make an event for it clicked and reload the form(if it's possible).
about the "Check me"
I thought this one will be the easiest... but It's not, I thought i should just copy my Console.app functions with minor changes and that's all, but nope...
Here is the code(Don't read anything, just the functions' topic)
I think the "main" function is what making a mess here.
Am I doing something wrong? well of course i do , but what
?
BTW Don't mind the MessageBox.Show(....) It's temporary , I;ll make instead of it something that paints the col\row\square that is faulty to another color.
Thank You a lot ,Jerry!
I can't believe It's almost finished(or not)! So i can have my 2 week holidays free (free minus physics=3 days free
)
JooClops
I'm not from the US :< I currently live in Israel.
and im 16.5 years old
Now,
I've made two new buttons:
1."Check me".
2."Clear".
about the Clear:
Instead of thinking a lot and writing a lot, I will just make an event for it clicked and reload the form(if it's possible).
about the "Check me"
I thought this one will be the easiest... but It's not, I thought i should just copy my Console.app functions with minor changes and that's all, but nope...
Here is the code(Don't read anything, just the functions' topic)
I think the "main" function is what making a mess here.
C# Syntax (Toggle Plain Text)
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); } }
?BTW Don't mind the MessageBox.Show(....) It's temporary , I;ll make instead of it something that paints the col\row\square that is faulty to another color.
Thank You a lot ,Jerry!
I can't believe It's almost finished(or not)! So i can have my 2 week holidays free (free minus physics=3 days free
) JooClops
Your declaration of the Main function looks a bit strange.
For a Console application it is normally:
For a Windows application we have normally:
You don't do much in the main function. You just start up to run the Application class with a Form.
For a Console application it is normally:
C# Syntax (Toggle Plain Text)
static void Main(string[] args) { }
C# Syntax (Toggle Plain Text)
[STAThread] static void Main() { }
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
JooClops,
Hope you live in a safe area of Israel.
It appears that you just copy paste'd the console methods into the Check button handler. That won't work.
You have three methods that are used to check faulty entries:
Col, Row, Square
and a single method used by each for the actual validation.
Rewrite them as individual methods (IOW not inside the Check button), and call them accordingly.
So, lets start with the isvalid method: Create it as its own method (not embedded within another method.)
Changed to a private (non static) method using Camel-Case naming convention.
Modified to account for arrays less than 9 in length.
Next we need to add the row check method:
Changed to a private (non static) method using a new Name.
Using the private _mat instead of passing it as a variable.
Return whatever IsValid gives you instead of evaluating it.
The remaining two methods are similar in changes:
Now for the actual Check button. I named the button btnCheck
What we are doing here is populate a string with all of the discovered faults, and finally displaying the result in one dialog box.
You could get fancy here, and populate a List<string> private class variable, and kick off a timer to flash the labels back color with red for those faulty items. If you want to go down this path, let me know and I can help.
Since your program is growing, I suggest you send me the zipped project in a private message if you need additional help.
Have Fun!
Jerry
Hope you live in a safe area of Israel.
It appears that you just copy paste'd the console methods into the Check button handler. That won't work.
You have three methods that are used to check faulty entries:
Col, Row, Square
and a single method used by each for the actual validation.
Rewrite them as individual methods (IOW not inside the Check button), and call them accordingly.
So, lets start with the isvalid method: Create it as its own method (not embedded within another method.)
C# Syntax (Toggle Plain Text)
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; }
Modified to account for arrays less than 9 in length.
Next we need to add the row check method:
C# Syntax (Toggle Plain Text)
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); }
Using the private _mat instead of passing it as a variable.
Return whatever IsValid gives you instead of evaluating it.
The remaining two methods are similar in changes:
C# Syntax (Toggle Plain Text)
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); }
Now for the actual Check button. I named the button btnCheck
C# Syntax (Toggle Plain Text)
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"); }
What we are doing here is populate a string with all of the discovered faults, and finally displaying the result in one dialog box.
You could get fancy here, and populate a List<string> private class variable, and kick off a timer to flash the labels back color with red for those faulty items. If you want to go down this path, let me know and I can help.
Since your program is growing, I suggest you send me the zipped project in a private message if you need additional help.
Have Fun!
Jerry
•
•
Join Date: Apr 2009
Posts: 44
Reputation:
Solved Threads: 0
Wow,
Thank you so much! It's Almost like a real soduko!!!
and more important i actually understood everything,I learned so many new things, it's so great! i love programming XD
I'll zip it to show You how it looks in the minute ill figure out which parts ought I zip
now few more things are left for making it perfect:
I tried making the clrbtn_click function, now after I've done it I realized that it just clears the matrix, but not the labels on the screens...so instead of trying to work it out maybe there is a way to just reload the form?
second thing, the labels are sized perfectly but the TEXT size is small, I've reviews the propertied and didn't find anything...OK NVM FOUND IT XDD i just wrote now it looks awesome 
so just the clear thing is left :>
I'm so happy,Can't tell You how grateful I am, and btw i live in non dangerous zone here,You should come visit
, so I'm safe , thank You for your concern.
JooClops
EDIT:
BTW My level of programming is not so high yet, It was our first year, and in school programming lessons are more logic based and almost zero skills, i mean , I barely know what I can do with "Classes" ,cause we'll only learn it next year, these are the things we will learn next year: Lists,recursion,Binary tree,Classes.
What I know is basic, and some things I've learned by my own, cause I was interested in solving problems from project euler ,and i made a few, but there were questions which u have to use Bignumber class , cause the numbers are HUGE!!! 9.6124123E+194
XD
thanks again.
Thank you so much! It's Almost like a real soduko!!!
and more important i actually understood everything,I learned so many new things, it's so great! i love programming XDI'll zip it to show You how it looks in the minute ill figure out which parts ought I zip

now few more things are left for making it perfect:
I tried making the clrbtn_click function, now after I've done it I realized that it just clears the matrix, but not the labels on the screens...so instead of trying to work it out maybe there is a way to just reload the form?
second thing, the labels are sized perfectly but the TEXT size is small, I've reviews the propertied and didn't find anything...OK NVM FOUND IT XDD i just wrote
C# Syntax (Toggle Plain Text)
lbl.Font = new Font("verdana", 12, FontStyle.Bold)

so just the clear thing is left :>
I'm so happy,Can't tell You how grateful I am, and btw i live in non dangerous zone here,You should come visit
, so I'm safe , thank You for your concern.JooClops
EDIT:
BTW My level of programming is not so high yet, It was our first year, and in school programming lessons are more logic based and almost zero skills, i mean , I barely know what I can do with "Classes" ,cause we'll only learn it next year, these are the things we will learn next year: Lists,recursion,Binary tree,Classes.
What I know is basic, and some things I've learned by my own, cause I was interested in solving problems from project euler ,and i made a few, but there were questions which u have to use Bignumber class , cause the numbers are HUGE!!! 9.6124123E+194
XD
thanks again.
Last edited by JooClops; Apr 5th, 2009 at 2:16 pm.
![]() |
Similar Threads
- QBASIC under Windows XP (Visual Basic 4 / 5 / 6)
- Creating Windows Applications (C++)
- Skinning C# Windows Applications (C#)
- XP not able to run MS-DOS and MS Windows applications (Windows NT / 2000 / XP)
- windows error service spyware message (Viruses, Spyware and other Nasties)
- Problem with config.nt on Windows 2000 Pro (Windows NT / 2000 / XP)
- c:/windows?system32?autoexec.nt (Windows NT / 2000 / XP)
- Windows to Linux Migration (Getting Started and Choosing a Distro)
Other Threads in the C# Forum
- Previous Thread: App takes time to load after restoring from minizied state
- Next Thread: how to programmatically compile C code using C# language
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# chat check checkbox client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ gis httpwebrequest image index input install java label list listbox listener mandelbrot math mouseclick mysql networking object operator path photoshop picturebox pixelinversion post prime programming radians regex remote remoting richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update upload usercontrol validation view visualstudio webbrowser windows winforms wpf xml






