|
-
Sep 1st, 2011, 09:20 AM
#1
Thread Starter
Lively Member
[RESOLVED] looping issue
Hi,
I'm trying to loop through a column of checkboxes to see how many checked values there are in that column. The user has to have 2; no more, no less than 2, otherwise an error should show up.
Code:
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim count As Integer = 0
Dim x As Integer = 0
For x = 1 To DataGridView1.RowCount - 1
If CBool(DataGridView1.Rows(x).Cells(7).Value) Then
count = count + 1
End If
Next
If count = 2 Then
Me.Close()
Else
MsgBox("Error.")
e.Cancel = True
End If
End Sub
The error message still occurs when I have only 2 chosen in that column. I even tried this code:
Code:
If CBool(DataGridView1.Rows(x).Cells(7).Value = Checkstate.checked) Then
Last edited by c3p0; Sep 1st, 2011 at 09:26 AM.
-
Sep 1st, 2011, 09:29 AM
#2
Member
Re: looping issue
I'm on my phone but I will help you best to my knowledge. Utilize the Try.. Catch here...
vb.net Code:
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing Dim count As Integer = 0 Dim x As Integer = 0 Try For x = 1 To DataGridView1.RowCount - 1 If CBool(DataGridView1.Rows(x).Cells(7).Value) Then count += 1 End If Next If Not count = 2 Then Me.Close() Else MsgBox("Error.") e.Cancel = True End If Catch ex As Exception MsgBox(ex.Message) End Try End Sub
Then give us the error
Last edited by babyjesus; Sep 1st, 2011 at 09:38 AM.
-
Sep 1st, 2011, 10:00 AM
#3
Thread Starter
Lively Member
Re: looping issue
Hey, Thanks for the help. Unfortunately that didn't work either.
-
Sep 1st, 2011, 10:13 AM
#4
Thread Starter
Lively Member
Re: looping issue
Now I tried this:
Code:
Dim count As Integer = 0
Dim x As Integer = 0
Try
For x = 1 To DataGridView1.RowCount - 1
If DataGridView1.Rows(x).Cells(7).Value = 1 Then
count += 1
End If
Next
If count = 2 Then
Me.Close()
Else
Dim msgbox1 As Integer
msgbox1 = MessageBox.Show("Error.", "Message", MessageBoxButtons.RetryCancel)
If msgbox1 = vbRetry Then
e.Cancel = True
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Now I get an error on "Me.Close()" saying: " Stack Overflow Exception was unhandled. An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll"
-
Sep 1st, 2011, 10:44 AM
#5
Member
Re: looping issue
You said no more or less than 2... wouldn't you just do If Not count = 2 then close?
-
Sep 1st, 2011, 10:46 AM
#6
Thread Starter
Lively Member
Re: looping issue
 Originally Posted by babyjesus
You said no more or less than 2... wouldn't you just do If Not count = 2 then close?
Basically, the user HAS to check 2 checkboxes. If the user picks less than 2, or more than 2, then it should show an error.
-
Sep 1st, 2011, 10:51 AM
#7
Re: looping issue
you shouldn't be closing the form from inside the FormClosing event... it already is closing... that's why you get the stack overflow error... you created an infinite loop by doing that.... simply set the e.Cancel to false and just let the event finish out....
FYI - with the code you have, if they click Cancel the form will close anyways.... are you sure that's what you want?
-tg
-
Sep 1st, 2011, 11:01 AM
#8
Thread Starter
Lively Member
Re: looping issue
 Originally Posted by techgnome
you shouldn't be closing the form from inside the FormClosing event... it already is closing... that's why you get the stack overflow error... you created an infinite loop by doing that.... simply set the e.Cancel to false and just let the event finish out....
FYI - with the code you have, if they click Cancel the form will close anyways.... are you sure that's what you want?
-tg
Thanks for the tip! Helped get rid of the error. For some reason though, its not counting the checkboxes that are checked correctly.
-
Sep 1st, 2011, 11:15 AM
#9
Re: looping issue
" its not counting the checkboxes that are checked correctly." and what exactly does that mean? too many? too few? arms falling off?
Perhaps adding a breakpoint at the beginning of your loop and stepping through your code will help expose where it's going wrong...
-tg
-
Sep 1st, 2011, 11:21 AM
#10
Thread Starter
Lively Member
Re: looping issue
I've tried stepping through the code, but I have no clue why its doing that.
When I check off 5 checkboxes and try to close the form, it gives me an error, which is good. But then I check 2 checkboxes and try to close the form, it still gives me an error.
Maybe it has to do something with the DataGridView?? The focus on the cells?? I'm out of ideas here....
-
Sep 1st, 2011, 12:43 PM
#11
Re: looping issue
Step through it... look at how many times your counter gets updated... It's possible that there's something in the grid that's hanging it up...
Something you may want to try, check only one... then try to close (you should get the error) then check a second one... see if it works at that point...
-tg
-
Sep 1st, 2011, 12:46 PM
#12
Re: looping issue
I think this is what you need:
EndEdit
http://msdn.microsoft.com/en-us/library/ms158620.aspx
I think what's happening is that the row that you last click on is still in edit mode, and so when you loop through it, since the new value isn't committed, it is still getting counted as being checked. Try this, see if it clears it up:
Code:
Dim count As Integer = 0
Dim x As Integer = 0
DataGridView1.EndEdit
Try
For x = 1 To DataGridView1.RowCount - 1
If DataGridView1.Rows(x).Cells(7).Value = 1 Then
count += 1
End If
Next
If count = 2 Then
Me.Close()
Else
Dim msgbox1 As Integer
msgbox1 = MessageBox.Show("Error.", "Message", MessageBoxButtons.RetryCancel)
If msgbox1 = vbRetry Then
e.Cancel = True
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
-tg
-
Sep 1st, 2011, 01:11 PM
#13
Thread Starter
Lively Member
Re: looping issue
YES! I just figured that out!
Code:
Me.DataGridView1.EndEdit()
spent 3 hours trying to find out why...
Anyways thanks!!
Tags for this Thread
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
|