到目前为止,这是我的代码。我的教授说我不能硬编码。你可以帮我点击按钮点击事件下的循环。正确的Flash表单将加载并提取文本文件中的第一个术语和定义,但是当您单击下一个按钮时,它会显示已读入的文件的路径。
Public Class frmFlash
Private _intSizeOfArray As Integer = 3
Private _strLocationOfTerms(_intSizeOfArray) As String
Private _strLocationOfDefinitions(_intSizeOfArray) As String
Private _objReader As IO.StreamReader
Private _strTerms As String
Private Sub frmFlash_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim objReader As IO.StreamReader
Dim strLocationOfTerms As String = "C:\Users\boone\Desktop\Terms.txt"
Dim strLocationOfDefinitions As String = "C:\Users\boone\Desktop\Definitions.txt"
Dim intTermArray(4) As Integer
Dim intCount As Integer
lblDefinition.Visible = False
If IO.File.Exists(strLocationOfTerms) Then
objReader = IO.File.OpenText(strLocationOfTerms)
_strTerms = objReader.ReadLine()
lblTerm.Text = _strTerms
End If
If IO.File.Exists(strLocationOfDefinitions) Then
objReader = IO.File.OpenText(strLocationOfDefinitions)
strLocationOfDefinitions = objReader.ReadLine()
objReader.Close()
lblDefinition.Text = strLocationOfDefinitions
End If
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Dim objReader(_intSizeOfArray) As IO.StreamReader
Dim intCount As Integer = 0
Dim strTermArray As String
Dim strLocationOfTerms As String = "C:\Users\boone\Desktop\Terms.txt"
Dim strLocationOfDefinitions As String = "C:\Users\boone\Desktop\Definitions.txt"
Do While intCount <= _intSizeOfArray
lblTerm.Text = strLocationOfTerms(_strTerms)
lblDefinition.Text = strLocationOfDefinitions
intCount += 1
Loop
End Sub
Private Sub btnDefinition_Click(sender As Object, e As EventArgs) Handles btnDefinition.Click
lblDefinition.Visible = True
End Sub
End Class
答案 0 :(得分:2)
由于您正在使用数组,最简单的方法是完全跳过按钮处理程序中的处理。在Load
事件中加载数组,只需抓取IEnumerator
。
'In Class level declarations
Private _termsEnumerator As IEnumerator(Of String)
Private _defsEnumerator As IEnumerator(Of String)
Private Sub frmFlash_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Your other initialization code...
'...
'Load your _strLocationOfTerms and _strLocationOfTerms here.
_termsEnumerator = _strLocationOfTerms.GetEnumerator()
_defsEnumerator = _strLocationOfDefinitions.GetEnumerator()
End Sub
然后它就像从数组中抓取下一个项目一样简单(它们实现IEnumerable
):
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
If _termsEnumerator.MoveNext() And _defsEnumerator.MoveNext() Then
lblTerm.Text = _termsEnumerator.Current
lblDefinition.Text = _defsEnumerator.Current
End If
End Sub