|
-
Apr 1st, 2007, 06:38 PM
#1
Thread Starter
New Member
I need some Help (Sudoku Gen)
Hey everyone,
I'm creating a Sudoku code with Visual Basics 2005 Express. However, an error appears saying the code is on a infinite loop
Code:
Private Sub Sudoku_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged, TextBox4.TextChanged, TextBox5.TextChanged, TextBox6.TextChanged, TextBox7.TextChanged, TextBox8.TextChanged, TextBox9.TextChanged, TextBox10.TextChanged, TextBox11.TextChanged, TextBox12.TextChanged, TextBox13.TextChanged, TextBox14.TextChanged, TextBox15.TextChanged, TextBox16.TextChanged, TextBox17.TextChanged, TextBox18.TextChanged, TextBox19.TextChanged, TextBox20.TextChanged, TextBox21.TextChanged, TextBox22.TextChanged, TextBox23.TextChanged, TextBox24.TextChanged, TextBox25.TextChanged, TextBox26.TextChanged, TextBox27.TextChanged, TextBox28.TextChanged, TextBox28.TextChanged, TextBox29.TextChanged, TextBox30.TextChanged, TextBox31.TextChanged, TextBox32.TextChanged, TextBox33.TextChanged, TextBox34.TextChanged, TextBox35.TextChanged, TextBox36.TextChanged, TextBox37.TextChanged, TextBox38.TextChanged, TextBox39.TextChanged, TextBox40.TextChanged, TextBox41.TextChanged, TextBox42.TextChanged, TextBox43.TextChanged, TextBox44.TextChanged, TextBox45.TextChanged, TextBox46.TextChanged, TextBox47.TextChanged, TextBox48.TextChanged, TextBox49.TextChanged, TextBox50.TextChanged, TextBox51.TextChanged, TextBox52.TextChanged, TextBox53.TextChanged, TextBox54.TextChanged, TextBox55.TextChanged, TextBox56.TextChanged, TextBox57.TextChanged, TextBox58.TextChanged, TextBox59.TextChanged, TextBox60.TextChanged, TextBox61.TextChanged, TextBox62.TextChanged, TextBox63.TextChanged, TextBox64.TextChanged, TextBox65.TextChanged, TextBox66.TextChanged, TextBox67.TextChanged, TextBox68.TextChanged, TextBox69.TextChanged, TextBox70.TextChanged, TextBox71.TextChanged, TextBox72.TextChanged, TextBox73.TextChanged, TextBox74.TextChanged, TextBox75.TextChanged, TextBox76.TextChanged, TextBox77.TextChanged, TextBox78.TextChanged, TextBox79.TextChanged, TextBox80.TextChanged, TextBox81.TextChanged
Dim a As Integer
Dim solfound As Boolean = True
Dim textbox As TextBox = CType(sender, TextBox)
If Not SolutionShwn Then
If textbox.Text = "" Then
textbox.Text = "1"
ElseIf textbox.Text = "9" Then
textbox.Text = ""
Else
textbox.Text = Trim(Str(CInt(textbox.Text) + 1))
End If
For a = 1 To 81
If txtbox(a).Text <> Sudokusolution(a) Then
solfound = True
Exit For
End If
Next a
congrats.Visible = solfound
End If
End Sub
The
Code:
textbox.Text = Trim(Str(CInt(textbox.Text) + 1))
Line reports an infinite loop, but I can't seem to fix the problem, any help would be greatly appreciated, thanks.
-
Apr 1st, 2007, 09:52 PM
#2
Frenzied Member
Re: I need some Help (Sudoku Gen)
What do you mean? Like.. That is the only code that is running?.. Or.. cmon guys be more specific ccmmonnnnnnnnnnn
-
Apr 1st, 2007, 10:47 PM
#3
Re: I need some Help (Sudoku Gen)
Well, there's junk in there you don't need, so why not get it out. Try something like this:
textbox.Text = (CInt(textbox.Text) + 1).ToString
After all, Str() isn't such a good idea. ToString is better, in this case, and Trim() would only remove spaces that wouldn't exist anyways, because ToString on an integer won't give you any spaces.
However, that's not such a good idea, either, because if the contents of the textbox is not actually an integer, CInt() will throw an exception. You show signs of having come from VB6 (the use of Str()), so you may be expecting CInt() to be as exception free as Val(), but it isn't. Passing in "123a" would return 123 if you used Val, but will throw an exception under CInt().
A better solution would be to use Integer.TryParse(), but you pass in the variable that you want the integer to be returned in as the second argument:
vb Code:
dim i as integer
If Integer.TryParse(Textbox.Text,i) then
'TryParse will return true if it works. i has the integer.
Textbox.Text = i.ToString
else
'This means that the textbox doesn't hold an integer.
end if
This requires more code, but it is safer because you won't get an exception.
My usual boring signature: Nothing
 
-
Apr 2nd, 2007, 07:02 AM
#4
Re: I need some Help (Sudoku Gen)
I think the reason you get an infinite loop is because you alter a textbox text in the sub, which in turn initiates a new textbox changed event, which causes another textbox changed event then another and another etc.
The solution would be to inhibit the event while you update the textbox content (or to use a completely different method!).
-
Apr 2nd, 2007, 09:00 AM
#5
Re: I need some Help (Sudoku Gen)
Oh yeah, that's the reason for the loop. All that other stuff I was talking about was just improvements on the code.
My usual boring signature: Nothing
 
-
Apr 2nd, 2007, 03:33 PM
#6
Re: I need some Help (Sudoku Gen)
As a quick and dirty test (just to check this is the problem) try;
Code:
Dim Inhibit As Boolean = False
Private Sub Sudoku_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged, TextBox4.TextChanged, TextBox5.TextChanged, TextBox6.TextChanged, TextBox7.TextChanged, TextBox8.TextChanged, TextBox9.TextChanged, TextBox10.TextChanged, TextBox11.TextChanged, TextBox12.TextChanged, TextBox13.TextChanged, TextBox14.TextChanged, TextBox15.TextChanged, TextBox16.TextChanged, TextBox17.TextChanged, TextBox18.TextChanged, TextBox19.TextChanged, TextBox20.TextChanged, TextBox21.TextChanged, TextBox22.TextChanged, TextBox23.TextChanged, TextBox24.TextChanged, TextBox25.TextChanged, TextBox26.TextChanged, TextBox27.TextChanged, TextBox28.TextChanged, TextBox28.TextChanged, TextBox29.TextChanged, TextBox30.TextChanged, TextBox31.TextChanged, TextBox32.TextChanged, TextBox33.TextChanged, TextBox34.TextChanged, TextBox35.TextChanged, TextBox36.TextChanged, TextBox37.TextChanged, TextBox38.TextChanged, TextBox39.TextChanged, TextBox40.TextChanged, TextBox41.TextChanged, TextBox42.TextChanged, TextBox43.TextChanged, TextBox44.TextChanged, TextBox45.TextChanged, TextBox46.TextChanged, TextBox47.TextChanged, TextBox48.TextChanged, TextBox49.TextChanged, TextBox50.TextChanged, TextBox51.TextChanged, TextBox52.TextChanged, TextBox53.TextChanged, TextBox54.TextChanged, TextBox55.TextChanged, TextBox56.TextChanged, TextBox57.TextChanged, TextBox58.TextChanged, TextBox59.TextChanged, TextBox60.TextChanged, TextBox61.TextChanged, TextBox62.TextChanged, TextBox63.TextChanged, TextBox64.TextChanged, TextBox65.TextChanged, TextBox66.TextChanged, TextBox67.TextChanged, TextBox68.TextChanged, TextBox69.TextChanged, TextBox70.TextChanged, TextBox71.TextChanged, TextBox72.TextChanged, TextBox73.TextChanged, TextBox74.TextChanged, TextBox75.TextChanged, TextBox76.TextChanged, TextBox77.TextChanged, TextBox78.TextChanged, TextBox79.TextChanged, TextBox80.TextChanged, TextBox81.TextChanged
If Not Inhibit Then
Inhibit = True
Dim a As Integer
Dim solfound As Boolean = True
Dim textbox As TextBox = CType(sender, TextBox)
If Not SolutionShwn Then
If textbox.Text = "" Then
textbox.Text = "1"
ElseIf textbox.Text = "9" Then
textbox.Text = ""
Else
textbox.Text = Trim(Str(CInt(textbox.Text) + 1))
End If
For a = 1 To 81
If txtbox(a).Text <> Sudokusolution(a) Then
solfound = True
Exit For
End If
Next a
congrats.Visible = solfound
End If
Inhibit = False
End If
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|