0
votes

I am trying to make a form populate RadioButton choices from the lines of a text file.

I have code dynamically declaring variables in an array for each line of the text file:

Public Function CountCharacter(ByVal value As String, ByVal ch As Char) As Integer
    Dim cnt As Integer = 0
    For Each c As Char In value
        If c = ch Then cnt += 1
    Next
    Return cnt
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim output As String
    Dim lineCount As Integer = 0
    Dim classText As String = "(text file path)"
    output = IO.File.ReadAllText(classText)
    'declare text file to variable
    lineCount = CountCharacter(output, vbCrLf) + 1
    'get line count (delimited by line feed)
    Dim strLine() As String = output.Split(vbCrLf)
    'split output variable delimited by line feed
    Dim classSelection(lineCount) As String
    'declare string array for amount of lines
    Dim period As Integer = 0
    'declare integer for counting 'For Each' statement
    For Each line As String In strLine
        period += 1
        'count one 'For Each' rotation period
        classSelection(period) = line
        'dynamically declare array data based on line/rotation number
    Next

Now I should have classSelection() for each line

When I try to pass this into RadioButtons or CheckBoxes only the first line classSelection(1) is displayable as RadioButton.Text For example:

RadioButton1.Text = classSelection(1)
RadioButton2.Text = classSelection(2)
RadioButton3.Text = classSelection(3)

this will display the first line from the text file on RadioButton1 and be blank on RadioButton2 and 3. I can pass classSelection(1) to any one of the three RadioButtons and it will display on them as well, but classSelection(2) or classSelection(3) will not display on any of the RadioButtons. What is odd to me is that RadioButton2.Text is holding the value of classSelection(2), just not displaying it. As I can pass the value of the non displaying RadioButton2.Text to another object and the value of classSelection(2) will display on it just fine.

What would make classSelection(1) so different from classSelection(2) to make it not display?

1
Not for nothing, but if you used a combobox: theCBO.Items.AddRange(File.ReadAllLines(someFile)) is all you would needŇɏssa Pøngjǣrdenlarp

1 Answers

0
votes

The issue is the way you're splitting the String you read. This is a perfect example of why you should have Option Strict On. When you have Option Strict Off, code may be executed in unexpected ways. Here:

Dim strLine() As String = output.Split(vbCrLf)

you presumably think that you're splitting on pairs of carriage return and line feed characters. You are not. That overload of String.Split accepts Char delimiters only, not String delimiters. If you had Option Strict On then you'd have been warned of that. As it is, the compiler just assumes that you want to use the first Char of that String. As such, the line feed gets dropped and the splitting is done on the carriage returns only. What that means is that every element of the resulting array after the first will start with a line feed.

If you actually wanted to split on a String delimiter then you'd have to call an overload of Split that accepted String delimiters. That would go like this:

Dim strLine() As String = output.Split({vbCrLf}, StringSplitOptions.None)

Having said all that, why would you bother calling File.ReadAllText and then splitting on line breaks when you can just call File.ReadAllLines?