Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27206

VS 2012 Using SelectedIndex/ListBox Object with 2D Arrays.

$
0
0
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 KBName:  gtr.PNG
Views: 88
Size:  14.9 KB


Here's what I've got so far:
vb.net Code:
  1. Option Strict On
  2. Imports System.IO
  3. Public Class QuizEditor
  4.     Dim QuizArray(,) As String
  5.     'Sub to read the text file and populte the List Object's Index
  6.     Sub FileReader(filename As String)
  7.         Dim reader As StreamReader = IO.File.OpenText(filename)
  8.         Dim currentLine As String
  9.         Dim currentValues() As String
  10.         Dim currentRowCounter As Integer = 0
  11.         Dim numberOfLinesInFile As Integer = GetNumberOfLinesInFile(filename)
  12.         Dim numberOfColumnsInFile As Integer = GetNumberOfColumnsInFile(filename)
  13.  
  14.         ReDim QuizArray(numberOfLinesInFile - 1, numberOfColumnsInFile - 1)
  15.  
  16.         Do Until reader.EndOfStream
  17.             currentLine = reader.ReadLine
  18.             currentValues = currentLine.Split(",".ToCharArray)
  19.  
  20.             For index = 0 To currentValues.Length - 1
  21.                 QuizArray(currentRowCounter, index) = Convert.ToString(currentValues(index))
  22.             Next
  23.             currentRowCounter += 1
  24.         Loop
  25.  
  26.         reader.Close()
  27.         Call DisplayQuestionList(numberOfColumnsInFile)
  28.     End Sub
  29.     'Function to get the Arrays Y Axis length.
  30.     Function GetNumberOfLinesInFile(fileName As String) As Integer
  31.         Dim numberOfLines As Integer
  32.         Dim reader As StreamReader = IO.File.OpenText(fileName)
  33.  
  34.         Do Until reader.EndOfStream
  35.             reader.ReadLine()
  36.             numberOfLines += 1
  37.         Loop
  38.         reader.Close()
  39.  
  40.         Return numberOfLines
  41.  
  42.     End Function
  43.     'Function to get the Arrays X Axis length.
  44.     Function GetNumberOfColumnsInFile(fileName As String) As Integer
  45.         Dim numberOfColumns As Integer
  46.         Dim firstLine As String
  47.         Dim reader As StreamReader = IO.File.OpenText(fileName)
  48.  
  49.         firstLine = reader.ReadLine
  50.         numberOfColumns = firstLine.Split(",".ToCharArray).Length
  51.  
  52.         reader.Close()
  53.  
  54.         Return numberOfColumns
  55.  
  56.     End Function
  57.     'Sub to reload the Questions in the List Object's Index.
  58.     Sub DisplayQuestionList(numberofcoloumnsinfile As Integer)
  59.         For index = 0 To numberofcoloumnsinfile - 1
  60.             lstQuestions.Items.Add(QuizArray(0, index))
  61.         Next
  62.     End Sub
  63.     'Sub to populate the Question and Answer Text Boxes in the Form according to the List Object Selection.
  64.     Private Sub lstQuestions_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstQuestions.SelectedIndexChanged
  65.  
  66.         txtQuestion.Text = QuizArray(0, lstQuestions.SelectedIndex)
  67.         txtAnswer1.Text = QuizArray(1, lstQuestions.SelectedIndex)
  68.         txtAnswer2.Text = QuizArray(2, lstQuestions.SelectedIndex)
  69.         txtAnswer3.Text = QuizArray(3, lstQuestions.SelectedIndex)
  70.         txtAnswer4.Text = QuizArray(4, lstQuestions.SelectedIndex)
  71.  
  72.     End Sub
  73.  
  74. #Region "File Handlers"
  75.     Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
  76.         opnFile.ShowDialog()
  77.  
  78.         Dim filepath As String = opnFile.FileName
  79.         FileReader(filepath)
  80.     End Sub
  81. #End Region
  82.  
  83.  
  84.     Private Sub btnAddQuestion_Click(sender As Object, e As EventArgs) Handles btnAddQuestion.Click
  85.         AddQuestion(QuizArray.GetUpperBound(0))
  86.     End Sub
  87.     Sub AddQuestion(ByVal numberofcoloumnsinfile As Integer)
  88.         'ReDim QuizArray(QuizArray.GetUpperBound(0) + 1, QuizArray.GetUpperBound(1))
  89.         lstQuestions.Items.Add(txtQuestion.Text)
  90.         lstQuestions.SelectedIndex = QuizArray.GetUpperBound(1)
  91.  
  92.         QuizArray(0, QuizArray.GetUpperBound(1)) = txtQuestion.Text
  93.         QuizArray(1, QuizArray.GetUpperBound(1)) = txtAnswer1.Text
  94.         QuizArray(2, QuizArray.GetUpperBound(1)) = txtAnswer2.Text
  95.         QuizArray(3, QuizArray.GetUpperBound(1)) = txtAnswer3.Text
  96.         QuizArray(4, QuizArray.GetUpperBound(1)) = txtAnswer4.Text
  97.  
  98.         DisplayQuestionList(numberofcoloumnsinfile)
  99.  
  100.     End Sub
  101. End Class
  102.  
  103. 'redim preserve keeps data
  104. 'switch the for loop index to flip the array


EDIT:

This is the code I'm having issues with.

vb.net Code:
  1. Sub AddQuestion(ByVal numberofcoloumnsinfile As Integer)
  2.         'ReDim QuizArray(QuizArray.GetUpperBound(0) + 1, QuizArray.GetUpperBound(1))
  3.         lstQuestions.Items.Add(txtQuestion.Text)
  4.         lstQuestions.SelectedIndex = QuizArray.GetUpperBound(1)
  5.  
  6.         QuizArray(0, QuizArray.GetUpperBound(1)) = txtQuestion.Text
  7.         QuizArray(1, QuizArray.GetUpperBound(1)) = txtAnswer1.Text
  8.         QuizArray(2, QuizArray.GetUpperBound(1)) = txtAnswer2.Text
  9.         QuizArray(3, QuizArray.GetUpperBound(1)) = txtAnswer3.Text
  10.         QuizArray(4, QuizArray.GetUpperBound(1)) = txtAnswer4.Text
  11.  
  12.         DisplayQuestionList(numberofcoloumnsinfile)
  13.  
  14.     End Sub

SelectedIndex was in place of upperbound. I've changed it 5 times or so.
Attached Images
  

Viewing all articles
Browse latest Browse all 27206

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>