Here is the code I'm working on. The user should be able to type in a number and then after clicking the button, list (in a listbox) of all the prime numbers leading up to the number (including the number if it's prime)

MY ERROR/PROBLEM: When i input a number value, it get a list of nothing...and it(the form) locks up. I know it works up to the "msgbox" code (i've tested for 1). Please help where you can and thank you for your time.

--------
Public Class frmPrime

Private Sub btnCompute_Click(....) Handles btnCompute.Click
Dim prim As Integer
'im test As Integer 'to test each number up to prim
Dim imPrime As Boolean = False
prim = CInt(txtNum.Text)
test = prim
'loop while prim is greater than 1 and not equal to prim

'As long as the number is greater than 1
If prim = 1 Then
imPrime = False
MessageBox.Show("Enter a number greater than one please")
Else
Do While prim >= 2
For i As Integer = 2 To prim
If prim Mod i = 0 Then
imPrime = False
Exit For
Else
imPrime = True
lstPrime.Items.Add(prim)
End If
Next
Loop
End If
'If imPrime = True Then
'lstPrime.Items.Add(prim)
'End If

End Sub
End Class

Dani AI

Generated

Quick diagnosis and a clean fix.

The hang happens because the code never advances the number being tested (so the outer loop never exits) and it adds items from inside the divisor loop. That makes the inner loop both test and mutate the listbox and leaves the outer loop running forever. 's suggestion to "add a do" may have unblocked the form, but the real fix is to iterate each candidate number from 2 to the input and, for each candidate, test divisibility up to its square root — only add the candidate after the divisor loop finishes and only if it stayed prime.

A straightforward, correct VB.NET approach (validate input, clear the list, test each candidate, stop divisor checks at sqrt) is:

Dim nMax As Integer
If Not Integer.TryParse(txtNum.Text, nMax) OrElse nMax < 2 Then
    MessageBox.Show("Enter an integer >= 2")
    Return
End If

lstPrime.Items.Clear()
For n As Integer = 2 To nMax
    Dim isPrime As Boolean = True
    For i As Integer = 2 To CInt(Math.Sqrt(n))
        If n Mod i = 0 Then
            isPrime = False
            Exit For
        End If
    Next
    If isPrime Then lstPrime.Items.Add(n)
Next

Tips and cautions: clear the ListBox before adding results; use TryParse to avoid exceptions from bad input; for large n the trial-division loop is slow — prefer a Sieve of Eratosthenes for big ranges. If the UI still "locks" on large inputs, run the calculation on a background thread or Task.Run and marshal updates back to the UI to keep the form responsive. Test with small numbers first and step through the loops to confirm the logic.

Recommended Answers

All 2 Replies

you need to add a do event to your code.

you need to add a do event to your code.

oh...wow...thank you. It works...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.