【发布时间】:2016-03-17 07:58:32
【问题描述】:
我正在尝试使表单从文本文件的行中填充 RadioButton 选项。
我有代码为文本文件的每一行动态声明数组中的变量:
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
现在我应该为每一行设置 classSelection()
当我尝试将其传递给 RadioButtons 或 CheckBoxes 时,只有第一行 classSelection(1) 可显示为 RadioButton.Text 例如:
RadioButton1.Text = classSelection(1)
RadioButton2.Text = classSelection(2)
RadioButton3.Text = classSelection(3)
这将在 RadioButton1 上显示文本文件的第一行,在 RadioButton2 和 3 上为空白。我可以将 classSelection(1) 传递给三个 RadioButtons 中的任何一个,它也会显示在它们上,但是 classSelection(2 ) 或 classSelection(3) 将不会显示在任何 RadioButtons 上。 对我来说奇怪的是 RadioButton2.Text 持有 classSelection(2) 的值,只是不显示它。因为我可以将不显示的 RadioButton2.Text 的值传递给另一个对象,classSelection(2) 的值就会很好地显示在它上面。
是什么让 classSelection(1) 与 classSelection(2) 如此不同以使其不显示?
【问题讨论】:
-
不是没有,但如果您使用组合框:
theCBO.Items.AddRange(File.ReadAllLines(someFile))就是您所需要的
标签: vb.net radio-button