的DataGridView
我创建了一个DKT应用程序。我有一个名为Question的表单,我可以添加,删除,查找,更新,保存以及datagridview控件来完成所有这些工作。问题是,当我删除一个问题时,它没有改变问题号。例如:如果我有6个问题,我删除问题没有4.现在我想再添加1个问题,然后删除问题6,但它不会找到问题no6。有人可以帮我。如果我的问题不清楚请告诉我。感谢
Imports System.IO
Public Class Question
Dim currentlyEditingDeleteingQueNo As Integer
Private Sub Question_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TXTQuestionNo.Enabled = False
TXTQuestionNo.Text = gloTestQuestion.Count + 1
Fill_Grid()
End Sub
'Insert a new question in array list.
Private Sub BTNAdd_Click(sender As Object, e As EventArgs) Handles BTNAdd.Click
Dim question As Trivia_Data
question.intQuestionNo = TXTQuestionNo.Text
question.strQuestion = TXTQuestion.Text
question.strAnswer1 = TXTAnswer1.Text
question.strAnswer2 = TXTAnswer2.Text
question.strAnswer3 = TXTAnswer3.Text
question.strAnswer4 = TXTAnswer4.Text
question.intCorrectAnswer = (TXTAnswer5.Text)
gloTestQuestion.Add(question)
Fill_Grid()
MessageBox.Show("Question inserted sucessfully....")
ClearText()
End Sub
'Clears all text boxes for next question and display possible question for it.
Private Sub ClearText()
TXTAnswer1.Clear()
TXTAnswer2.Clear()
TXTAnswer3.Clear()
TXTAnswer4.Clear()
TXTAnswer5.Clear()
TXTQuestion.Clear()
TXTQuestionNo.Clear()
TXTQuestionNo.Text = gloTestQuestion.Count + 1
End Sub
'Fills data grid view controls with available question in a system.
Private Sub Fill_Grid()
'DataGridView1.Columns.Clear()
' DataGridView1.Rows.Clear()
DataGridView1.Columns.Add("Question Number", "Question Number")
DataGridView1.Columns.Add("Question", "Question")
DataGridView1.Columns.Add("Answer1", "Answer1")
DataGridView1.Columns.Add("Answer2", "Answer2")
DataGridView1.Columns.Add("Answer3", "Answer3")
DataGridView1.Columns.Add("Answer4", "Answer4")
DataGridView1.Columns.Add("CorrectAnswer", "CorrectAnswer")
Dim question As Trivia_Data
For j = 0 To gloTestQuestion.Count - 1
DataGridView1.Rows.Add()
question = gloTestQuestion(j)
DataGridView1.Rows(j).Cells("Question Number").Value = question.intQuestionNo.ToString()
DataGridView1.Rows(j).Cells("Question").Value = question.strQuestion
DataGridView1.Rows(j).Cells("Answer1").Value = question.strAnswer1
DataGridView1.Rows(j).Cells("Answer2").Value = question.strAnswer2
DataGridView1.Rows(j).Cells("Answer3").Value = question.strAnswer3
DataGridView1.Rows(j).Cells("Answer4").Value = question.strAnswer4
DataGridView1.Rows(j).Cells("CorrectAnswer").Value = question.intCorrectAnswer
Next
End Sub
Private Sub BTNFind_Click(sender As Object, e As EventArgs) Handles BTNFind.Click
Dim totalAddedQuestion As Integer = gloTestQuestion.Count
Dim questionNo = InputBox("Enter the question number that you want to search:")
If questionNo > gloTestQuestion.Count And questionNo = 0 Then
MessageBox.Show("Question doesn't exist........")
ClearText()
Exit Sub
Else
'gloTestQuestions. sort(compare)
Fill_Grid()
Dim max As Integer
Dim min As Integer
Dim mid As Integer
min = 0
max = totalAddedQuestion - 1
While (max >= min)
mid = (min + max) / 2
Dim question As Trivia_Data = CType(gloTestQuestion(mid), Trivia_Data)
If question.intQuestionNo < questionNo Then
min = mid + 1
ElseIf question.intQuestionNo > questionNo Then
max = mid - 1
Else
TXTAnswer1.Text = question.strAnswer1
TXTAnswer2.Text = question.strAnswer2
TXTAnswer3.Text = question.strAnswer3
TXTAnswer4.Text = question.strAnswer4
TXTAnswer5.Text = question.intCorrectAnswer
TXTQuestion.Text = question.strQuestion
TXTQuestionNo.Text = question.intQuestionNo
currentlyEditingDeleteingQueNo = mid
MessageBox.Show("Question found.........")
BTNUpdate.Enabled = True
BTNDelete.Enabled = True
Exit Sub
End If
End While
End If
MessageBox.Show("Question doesn't exist......")
ClearText()
End Sub
'Saves questions in binary file and closes the file after writing operations.
Private Sub BTNSave_Click(sender As Object, e As EventArgs) Handles BTNSave.Click
Dim file As New FileStream(Directory.GetCurrentDirectory() & "\Question.dat", FileMode.Create, FileAccess.Write)
Dim fileWrite As BinaryWriter = New BinaryWriter(file)
Dim question As Trivia_Data
For i = 0 To gloTestQuestion.Count - 1
question = CType(gloTestQuestion(i), Trivia_Data)
fileWrite.Write(Convert.ToInt16(question.intQuestionNo))
fileWrite.Write(question.strQuestion)
fileWrite.Write(question.strAnswer1)
fileWrite.Write(question.strAnswer2)
fileWrite.Write(question.strAnswer3)
fileWrite.Write(question.strAnswer4)
fileWrite.Write(Convert.ToInt16(question.intCorrectAnswer))
Next
fileWrite.Close()
file.Close()
MessageBox.Show("Questions saved in file successfully........")
End Sub
'Update existing question as per newly provided values.
Private Sub BTNUpdate_Click(sender As Object, e As EventArgs) Handles BTNUpdate.Click
Dim question As Trivia_Data = CType(gloTestQuestion(currentlyEditingDeleteingQueNo), Trivia_Data)
question.strAnswer1 = TXTAnswer1.Text
question.strAnswer2 = TXTAnswer2.Text
question.strAnswer3 = TXTAnswer3.Text
question.strAnswer4 = TXTAnswer4.Text
question.intCorrectAnswer = TXTAnswer5.Text
question.strQuestion = TXTQuestion.Text
gloTestQuestion(currentlyEditingDeleteingQueNo) = question
MessageBox.Show("Question updated successfully............")
BTNUpdate.Enabled = False
BTNDelete.Enabled = False
ClearText()
Fill_Grid()
End Sub
'Deletes a current question from the arraylist.
Private Sub BTNDelete_Click(sender As Object, e As EventArgs) Handles BTNDelete.Click
gloTestQuestion.RemoveAt(currentlyEditingDeleteingQueNo)
Fill_Grid()
ClearText()
MessageBox.Show("Question removed successfully......")
BTNDelete.Enabled = False
BTNUpdate.Enabled = False
End Sub
'Closes the question form
Private Sub BTNClose_Click(sender As Object, e As EventArgs) Handles BTNClose.Click
Me.Close()
End Sub
End Class
答案 0 :(得分:0)
您的查找功能中此行错误:
If questionNo > gloTestQuestion.Count And questionNo = 0 Then
您使用数组中的问题索引(question.intQuestionNo
)混淆了问题ID(gloTestQuestion
)。