基于用户输入题量的VB.NET测验程序卡顿问题求助
Hey there! No worries at all—we all start somewhere, and this is a great learning opportunity. Let's break down why your program freezes on the second button click and get it working smoothly.
The Main Culprit: Infinite Loop
Looking at your NxtQues_Click method, here's the root cause:
Do Call Questions() Loop Until n = NoQ
When you click "Next Question", you increment n by 1, then this loop runs non-stop because n doesn't change inside the loop. It keeps calling Questions() over and over without ever reaching NoQ, which completely blocks your UI thread (making the program look frozen).
You don't need this loop at all—each click of the next button should just load one next question, not jump straight to the last one.
Other Key Improvements
Beyond fixing the loop, let's clean up your database code to make it more efficient and secure:
1. Use Parameterized Queries (Avoid SQL Injection)
Directly concatenating n into your SQL string is a risky practice (even if n is an integer here). Using parameters keeps your code safe and follows best practices.
2. Properly Manage Database Resources
Opening/closing a connection every time you load a question is inefficient, and can lead to connection leaks. Use Using statements to automatically dispose of connections, commands, and readers when you're done with them.
3. Handle Edge Cases
Add a check to make sure n doesn't go beyond NoQ—so users can't click "Next" past the last question.
Modified Code
Here's the revised version of your class with all these fixes:
Imports System.Data.OleDb Public Class ArithmeticQuestions Dim NoQ = ArQOP.NoQ Dim n As Integer = 1 ' Reuse the connection string (store it once instead of repeating) Private ReadOnly connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=login.accdb" Private Sub ArithmeticQuestions_Load(sender As Object, e As EventArgs) Handles MyBase.Load Label1.Hide() RadioButton1.Hide() RadioButton2.Hide() RadioButton3.Hide() RadioButton4.Hide() SkipQues.Hide() NxtQues.Hide() End Sub Sub Questions() ' Use Using to auto-dispose resources Using cn As New OleDbConnection(connectionString) cn.Open() ' Parameterized query Using cm As New OleDbCommand("SELECT Questions FROM MCQ WHERE QuestionNumber = @QuestionNum", cn) cm.Parameters.AddWithValue("@QuestionNum", n) Using dr As OleDbDataReader = cm.ExecuteReader() If dr.Read() Then Label1.Text = dr.Item("Questions").ToString() Else ' Handle case where no question is found Label1.Text = "Question not found!" End If End Using End Using End Using End Sub Private Sub NxtQues_Click(sender As Object, e As EventArgs) Handles NxtQues.Click ' Only increment and load next question if we haven't reached the last one If n < NoQ Then n += 1 Questions() Else ' Optional: Disable button or show message when at last question NxtQues.Enabled = False MessageBox.Show("You've reached the last question!") End If End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles StartQuiz.Click Label1.Show() RadioButton1.Show() RadioButton2.Show() RadioButton3.Show() RadioButton4.Show() SkipQues.Show() NxtQues.Show() StartQuiz.Hide() ' Reset n to 1 in case user restarts the quiz n = 1 Questions() ' Make sure next button is enabled NxtQues.Enabled = True End Sub End Class
What Changed?
- Removed the infinite loop in
NxtQues_Click—now each click loads just one next question - Added
Usingstatements to properly clean up database connections/commands/readers - Switched to parameterized SQL to avoid injection risks
- Added checks to prevent going past the last question
- Reset
nwhen starting the quiz (in case the user restarts it)
Give this a try—your program should no longer freeze, and it'll be more robust overall!
内容的提问来源于stack exchange,提问作者H97

