I'm having trouble understanding the behaviour of the SelectedIndex control I'm using to navigate through a 2D array.
I'm stuck trying to add a column (question), and delete a column of the array without losing the other data.
I'm writing a Text file editing program that edits a quiz. Note the format of the text file. The column headers are the questions, with the answers beneath.
![Name: Capture.PNG
Views: 89
Size: 9.6 KB]()
![Name: gtr.PNG
Views: 88
Size: 14.9 KB]()
Here's what I've got so far:
EDIT:
This is the code I'm having issues with.
SelectedIndex was in place of upperbound. I've changed it 5 times or so.
I'm stuck trying to add a column (question), and delete a column of the array without losing the other data.
I'm writing a Text file editing program that edits a quiz. Note the format of the text file. The column headers are the questions, with the answers beneath.
Here's what I've got so far:
vb.net Code:
Option Strict On Imports System.IO Public Class QuizEditor Dim QuizArray(,) As String 'Sub to read the text file and populte the List Object's Index Sub FileReader(filename As String) Dim currentLine As String Dim currentValues() As String Dim currentRowCounter As Integer = 0 Dim numberOfLinesInFile As Integer = GetNumberOfLinesInFile(filename) Dim numberOfColumnsInFile As Integer = GetNumberOfColumnsInFile(filename) ReDim QuizArray(numberOfLinesInFile - 1, numberOfColumnsInFile - 1) Do Until reader.EndOfStream currentLine = reader.ReadLine currentValues = currentLine.Split(",".ToCharArray) For index = 0 To currentValues.Length - 1 QuizArray(currentRowCounter, index) = Convert.ToString(currentValues(index)) Next currentRowCounter += 1 Loop reader.Close() Call DisplayQuestionList(numberOfColumnsInFile) End Sub 'Function to get the Arrays Y Axis length. Function GetNumberOfLinesInFile(fileName As String) As Integer Dim numberOfLines As Integer Do Until reader.EndOfStream reader.ReadLine() numberOfLines += 1 Loop reader.Close() Return numberOfLines End Function 'Function to get the Arrays X Axis length. Function GetNumberOfColumnsInFile(fileName As String) As Integer Dim numberOfColumns As Integer Dim firstLine As String firstLine = reader.ReadLine numberOfColumns = firstLine.Split(",".ToCharArray).Length reader.Close() Return numberOfColumns End Function 'Sub to reload the Questions in the List Object's Index. Sub DisplayQuestionList(numberofcoloumnsinfile As Integer) For index = 0 To numberofcoloumnsinfile - 1 lstQuestions.Items.Add(QuizArray(0, index)) Next End Sub 'Sub to populate the Question and Answer Text Boxes in the Form according to the List Object Selection. Private Sub lstQuestions_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstQuestions.SelectedIndexChanged txtQuestion.Text = QuizArray(0, lstQuestions.SelectedIndex) txtAnswer1.Text = QuizArray(1, lstQuestions.SelectedIndex) txtAnswer2.Text = QuizArray(2, lstQuestions.SelectedIndex) txtAnswer3.Text = QuizArray(3, lstQuestions.SelectedIndex) txtAnswer4.Text = QuizArray(4, lstQuestions.SelectedIndex) End Sub #Region "File Handlers" Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click opnFile.ShowDialog() Dim filepath As String = opnFile.FileName FileReader(filepath) End Sub #End Region Private Sub btnAddQuestion_Click(sender As Object, e As EventArgs) Handles btnAddQuestion.Click AddQuestion(QuizArray.GetUpperBound(0)) End Sub Sub AddQuestion(ByVal numberofcoloumnsinfile As Integer) 'ReDim QuizArray(QuizArray.GetUpperBound(0) + 1, QuizArray.GetUpperBound(1)) lstQuestions.Items.Add(txtQuestion.Text) lstQuestions.SelectedIndex = QuizArray.GetUpperBound(1) QuizArray(0, QuizArray.GetUpperBound(1)) = txtQuestion.Text QuizArray(1, QuizArray.GetUpperBound(1)) = txtAnswer1.Text QuizArray(2, QuizArray.GetUpperBound(1)) = txtAnswer2.Text QuizArray(3, QuizArray.GetUpperBound(1)) = txtAnswer3.Text QuizArray(4, QuizArray.GetUpperBound(1)) = txtAnswer4.Text DisplayQuestionList(numberofcoloumnsinfile) End Sub End Class 'redim preserve keeps data 'switch the for loop index to flip the array
EDIT:
This is the code I'm having issues with.
vb.net Code:
Sub AddQuestion(ByVal numberofcoloumnsinfile As Integer) 'ReDim QuizArray(QuizArray.GetUpperBound(0) + 1, QuizArray.GetUpperBound(1)) lstQuestions.Items.Add(txtQuestion.Text) lstQuestions.SelectedIndex = QuizArray.GetUpperBound(1) QuizArray(0, QuizArray.GetUpperBound(1)) = txtQuestion.Text QuizArray(1, QuizArray.GetUpperBound(1)) = txtAnswer1.Text QuizArray(2, QuizArray.GetUpperBound(1)) = txtAnswer2.Text QuizArray(3, QuizArray.GetUpperBound(1)) = txtAnswer3.Text QuizArray(4, QuizArray.GetUpperBound(1)) = txtAnswer4.Text DisplayQuestionList(numberofcoloumnsinfile) End Sub
SelectedIndex was in place of upperbound. I've changed it 5 times or so.