Candice McDonald
Candice McDonald

Reputation: 25

VB .net form gets hung up (can't click on form) after my loop ends

I have a project that I am putting together. After I enter a loop to enter items into a listbox from input boxes (number of boxes determined from prior listbox) - it ends the loop at the correct time, however, I am then unable to click on the form at all. Nothing is sticking out clearly as to why this is happening. I have to stop debugging to close the form. Any ideas?

Private Sub btnEnterBib_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterBib.Click
        ' The btnEnterBib click event accepts and x number of Bib numbers. 
        ' 1 for each time entry.
        ' Declare and initialize variables

        Dim strBibEntry As String = CStr(0)
        Dim strInputMessage As String = "Enter the BIB #"
        Dim strInputHeading As String = "BIB Entry"
        Dim strNegativeError As String = "Error - Enter a positive number for the Bib #"
        Dim strNonNumericError As String = "Error - Enter a positive number for the Bib #"
        Dim intNumberofEntries As Integer = 1
        Dim intBibNum As Integer = 0
        Dim intTotalEntries As Integer = 0
        Dim strCancelClicked As String = " "
        Dim intMaxNumberofEntries As Integer = (lstTimeEntry.Items.Count)



        Do Until intTotalEntries > intMaxNumberofEntries Or strBibEntry = strCancelClicked
            If intNumberofEntries <= intMaxNumberofEntries Then
                strBibEntry = InputBox(strInputMessage & intNumberofEntries, strInputHeading, " ")
            End If
            If IsNumeric(strBibEntry) Then
                intBibNum = Convert.ToInt32(strBibEntry)
                If intBibNum > 0 Then
                    lstBibs.Items.Add(strBibEntry)
                    intNumberofEntries += 1

                Else
                    strInputMessage = strNegativeError
                End If
            Else
                strInputMessage = strNonNumericError
            End If

        Loop

        btnSyncTimesBibs.Enabled = True
        btnEnterBib.Enabled = False

    End Sub

Upvotes: 0

Views: 74

Answers (1)

Dimitri
Dimitri

Reputation: 1208

You have an infinite loop. You set intTotalEntries to 0, and you control the loop by checking if intTotalEntries is greater than intMaxNumberOfEntries, which I assume is never negative.

Within your loop, you never alter intTotalEntries. Instead, you use intNumberOfEntries, also comparing it to intMaxNumberOfEntries. You use this to decide if you should accept more entries, and you do increment intNumberOfEntries. Once that variable exceeds the max, you no longer accept input. This is why you believe the loop is ending. In fact it is now off to the races, because intTotalEntries is always zero, and can never be greater than the max.

As you have seen, an infinite loop will lock everything up solid until the program is forcibly terminated.

I suspect you didn’t mean to have two separate variables that seem to count the entries. Also the first If statement isn’t needed.

Try this:

Do Until intNumberOfEntries > intMaxNumberOfEntries Or strBibEntry = strCancelClicked
    strBibEntry = InputBox(strInputMessage & intNumberOfEntries, strInputHeading, " ")
    'Remainder of your code here
Loop

Upvotes: 1

Related Questions