Results 1 to 13 of 13

Thread: [RESOLVED] looping issue

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    72

    Resolved [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.

  2. #2
    Member
    Join Date
    Apr 2009
    Posts
    57

    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:
    1. Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    2.         Dim count As Integer = 0
    3.         Dim x As Integer = 0
    4. Try
    5.         For x = 1 To DataGridView1.RowCount - 1
    6.             If CBool(DataGridView1.Rows(x).Cells(7).Value) Then
    7.                 count += 1
    8.             End If
    9.         Next
    10.         If Not count = 2 Then
    11.             Me.Close()
    12.         Else
    13.             MsgBox("Error.")
    14.             e.Cancel = True
    15.         End If
    16. Catch ex As Exception
    17.       MsgBox(ex.Message)
    18. End Try
    19. End Sub
    Then give us the error
    Last edited by babyjesus; Sep 1st, 2011 at 09:38 AM.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    72

    Re: looping issue

    Hey, Thanks for the help. Unfortunately that didn't work either.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    72

    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"

  5. #5
    Member
    Join Date
    Apr 2009
    Posts
    57

    Re: looping issue

    You said no more or less than 2... wouldn't you just do If Not count = 2 then close?

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    72

    Re: looping issue

    Quote Originally Posted by babyjesus View Post
    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.

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    72

    Re: looping issue

    Quote Originally Posted by techgnome View Post
    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.

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    72

    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....

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    72

    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
  •  



Click Here to Expand Forum to Full Width