943,696 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 2737
  • C# RSS
You are currently viewing page 3 of this multi-page discussion thread; Jump to the first page
Apr 4th, 2009
0

Re: Hello,windows applications,help please

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?
Last edited by JooClops; Apr 4th, 2009 at 11:36 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
JooClops is offline Offline
44 posts
since Apr 2009
Apr 4th, 2009
0

Re: Hello,windows applications,help please

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)
  1. int i;
  2. string someValue = "123";
  3. if (Int32.TryParse(someValue, out i))
  4. {
  5. // i now = 123
  6. }
  7. else
  8. MessageBox.Show("Invalid value, must be numeric");

To control the size of your labels, you should set the AutoSize property to false.

// Jerry
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Apr 4th, 2009
0

Re: Hello,windows applications,help please

Click to Expand / Collapse  Quote originally posted by JerryShaw ...
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)
  1. int i;
  2. string someValue = "123";
  3. if (Int32.TryParse(someValue, out i))
  4. {
  5. // i now = 123
  6. }
  7. else
  8. MessageBox.Show("Invalid value, must be numeric");

To control the size of your labels, you should set the AutoSize property to false.

// Jerry
Hi again,
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)
  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3. int[,] mat = new int[9, 9];
  4. for (int i = 0; i < 9; i++)
  5. {
  6. for (int j = 0; j < 9; j++)
  7. {
  8. Label lbl = new Label();
  9. lbl.Text = (i + 1).ToString();
  10. mat[i,j] = Convert.ToInt32(lbl.Text);
  11. lbl.Location = new Point(120 + (j * 100), 70 + (i * 40));
  12. lbl.Size = new Size(label1.Width, label1.Height);
  13. lbl.DragDrop += label1_DragDrop;
  14. lbl.DragOver += label1_DragOver;
  15. lbl.AllowDrop = true;
  16. panel.Controls.Add(lbl);
  17. }
  18. }
and the matrix gets the numbers as it should (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.
Reputation Points: 10
Solved Threads: 0
Light Poster
JooClops is offline Offline
44 posts
since Apr 2009
Apr 4th, 2009
1

Re: Hello,windows applications,help please

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:
C# Syntax (Toggle Plain Text)
  1. lbl.Tag = string.Format("{0}|{1}",i,j);
  2. // 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)
  1. private void label1_DragDrop(object sender, DragEventArgs e)
  2. {
  3. Label target = sender as Label;
  4. Button btn = e.Data.GetData(typeof(Button)) as Button;
  5. target.Text = btn.Text;
  6. string address = (string)target.Tag;
  7. int i = Convert.Int32(address.Split('|')[0]);
  8. int j = Convert.Int32(address.Split('|')[1]);
  9. mat[i,j] = Convert.Int32(target.Text);
  10. }

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
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Apr 4th, 2009
0

Re: Hello,windows applications,help please

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];"
C# Syntax (Toggle Plain Text)
  1. private void label1_DragDrop(object sender, DragEventArgs e)
  2. {
  3. Label target = sender as Label;
  4. Button btn = e.Data.GetData(typeof(Button)) as Button;
  5. target.Text = btn.Text;
  6. string address = (string)target.Tag;
  7. int i = Convert.ToInt32(address.Split('|')[0]);
  8. int j = Convert.ToInt32(address.Split('|')[1]);
  9. _mat[i, j] = Convert.ToInt32(target.Text);
  10.  
  11. }
  12.  
  13. private void Form1_Load(object sender, EventArgs e)
  14. {
  15. int[,] _mat = new int[9, 9];
  16. for (int i = 0; i < 9; i++)
  17. {
  18. for (int j = 0; j < 9; j++)
  19. {
  20. Label lbl = new Label();
  21. lbl.Text = (i + 1).ToString();
  22. _mat[i,j] = i+1;
  23. lbl.Location = new Point(120 + (j * 100), 70 + (i * 40));
  24. lbl.Size = new Size(label1.Width, label1.Height);
  25. lbl.DragDrop += label1_DragDrop;
  26. lbl.DragOver += label1_DragOver;
  27. lbl.AllowDrop = true;
  28. lbl.Tag = string.Format("{0}|{1}", i, j);
  29. panel.Controls.Add(lbl);
  30. }
  31. }
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
Last edited by JooClops; Apr 4th, 2009 at 6:00 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
JooClops is offline Offline
44 posts
since Apr 2009
Apr 4th, 2009
0

Re: Hello,windows applications,help please

You need to move the declaration to the top of the class.

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.
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Apr 5th, 2009
0

Re: Hello,windows applications,help please

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.
C# Syntax (Toggle Plain Text)
  1. private void button10_Click(object sender, EventArgs e)
  2. {
  3. public bool isvalid(int []ar)
  4. {
  5. for (int i=0;i<9;i++)
  6. {for (int j=0;j<9;j++)
  7. if ((j!=i)&&(ar[i]==ar[j]))
  8. return false;
  9. }return true;
  10. }
  11.  
  12. public bool row(int [,]_mat,int rownum)
  13. {
  14. int [] ar=new int [9];
  15. for (int j=0;j<mat.GetLength(1);j++)
  16. ar[j]=_mat[rownum,j];
  17. if (isvalid(ar))
  18. return true;
  19. return false;
  20.  
  21. }
  22.  
  23. public bool col(int [,]_mat,int colnum)
  24.  
  25. { int [] ar=new int [9];
  26. for (int i=0;i<mat.GetLength(1);i++)
  27. ar[i]=_mat[i,colnum];
  28. if (isvalid(ar))
  29. return true;
  30. return false;
  31.  
  32. }
  33. public bool square(int [,]_mat,int rownum,int colnum)
  34. {
  35. int [] ar=new int [9];
  36. int k=0;
  37. for (int i=0;i<3;i++)
  38. { for (int j=0;j<3;j++)
  39. { ar[k]=_mat[rownum+i,colnum+j];
  40. k++;
  41. }
  42. }
  43. if (isvalid(ar))
  44. return true;
  45. return false;
  46.  
  47. }
  48.  
  49. public void printrows(int [,]_mat)
  50. {
  51. for (int i = 0; i < 9; i++)
  52. if (row(_mat, i)==false)
  53. MessageBox.Show("|Row {0} is Faulty!|", i+1);
  54. }
  55.  
  56. public void printcols(int[,]_mat)
  57. {
  58.  
  59. for (int j = 0; j < 9; j++)
  60. if (col(_mat, j)==false)
  61. MessageBox.Show("|Column {0} is Faulty!|", j+1);
  62.  
  63.  
  64. }
  65.  
  66. public void printsquares(int [,]_mat)
  67. {
  68. int k = 0;
  69.  
  70. for (int i = 0; i < 9; i=i+3)
  71. { for (int j = 0; j < 9; j=j+3)
  72. {
  73. k++;
  74. if (square(_mat, i,j)==false)
  75. MessageBox.Show("|Square {0} is Faulty!|", k);
  76. }
  77. }
  78. }
  79. static void main(System[]args)
  80. {
  81.  
  82. printrows(_mat);
  83. printcols(_mat);
  84. printsquares(_mat);
  85. }
  86.  
  87.  
  88. }
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
Reputation Points: 10
Solved Threads: 0
Light Poster
JooClops is offline Offline
44 posts
since Apr 2009
Apr 5th, 2009
0

Re: Hello,windows applications,help please

Your declaration of the Main function looks a bit strange.
For a Console application it is normally:
C# Syntax (Toggle Plain Text)
  1. static void Main(string[] args)
  2. {
  3. }
For a Windows application we have normally:
C# Syntax (Toggle Plain Text)
  1. [STAThread]
  2. static void Main()
  3. {
  4. }
You don't do much in the main function. You just start up to run the Application class with a Form.
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Apr 5th, 2009
0

Re: Hello,windows applications,help please

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.)
C# Syntax (Toggle Plain Text)
  1. private bool IsValid(int[] ar)
  2. {
  3. for (int i = 0; i < ar.Length; i++)
  4. {
  5. for (int j = 0; j < ar.Length; j++)
  6. if ((j != i) && (ar[i] == ar[j]))
  7. return false;
  8. }
  9. return true;
  10. }
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:
C# Syntax (Toggle Plain Text)
  1. private bool CheckRow(int rownum)
  2. {
  3. int[] ar = new int[9];
  4. for (int j = 0; j < _mat.GetLength(1); j++)
  5. ar[j] = _mat[rownum, j];
  6. return IsValid(ar);
  7. }
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:
C# Syntax (Toggle Plain Text)
  1. private bool CheckCol(int colnum)
  2. {
  3. int[] ar = new int[9];
  4. for (int i = 0; i < _mat.GetLength(1); i++)
  5. ar[i] = _mat[i, colnum];
  6. return IsValid(ar);
  7. }
  8.  
  9. private bool CheckSquare(int rownum, int colnum)
  10. {
  11. int[] ar = new int[9];
  12. int k = 0;
  13. for (int i = 0; i < 3; i++)
  14. {
  15. for (int j = 0; j < 3; j++)
  16. {
  17. ar[k] = _mat[rownum + i, colnum + j];
  18. k++;
  19. }
  20. }
  21. return IsValid(ar);
  22. }

Now for the actual Check button. I named the button btnCheck
C# Syntax (Toggle Plain Text)
  1. private void btnCheck_Click(object sender, EventArgs e)
  2. {
  3. int k = 0;
  4. string faults = string.Empty;
  5. for (int i = 0; i < 9; i++)
  6. {
  7. if (!CheckRow(i))
  8. faults += string.Format("Row: {0} is Faulty!\r\n", i);
  9.  
  10. for (int j = 0; j < 9; j++)
  11. {
  12. if (!CheckCol(j))
  13. {
  14. faults += string.Format("Col: {0} is Faulty!\r\n", i);
  15. break;
  16. }
  17. }
  18. }
  19. for (int i = 0; i < 9; i = i + 3)
  20. {
  21. for (int j = 0; j < 9; j = j + 3)
  22. {
  23. k++;
  24. if (!CheckSquare(i, j))
  25. faults += string.Format("Square: {0} is Faulty!\r\n", k);
  26. }
  27. }
  28. if (!string.IsNullOrEmpty(faults))
  29. MessageBox.Show(faults, "Faults");
  30. else
  31. MessageBox.Show("Success");
  32. }

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
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Apr 5th, 2009
0

Re: Hello,windows applications,help please

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
C# Syntax (Toggle Plain Text)
  1. lbl.Font = new Font("verdana", 12, FontStyle.Bold)
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.
Last edited by JooClops; Apr 5th, 2009 at 2:16 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
JooClops is offline Offline
44 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC