vb-如何为不同的数组字分配不同的标签属性?

时间:2015-09-13 07:27:11

标签: vb.net

这是我从文本文件到数组插入标签的代码,但是我希望能够在某些单词上分配标签属性,或者使用位于我文本文件下面一行的“答案”? ?

IndexNo = 0
        Dim FileTerm As String = "D:\soccer.txt"
        Dim FileNum As Integer = FreeFile()
        FileOpen(FileNum, FileTerm, OpenMode.Input)
        Do
            Term(IndexNo) = LineInput(FileNum)
            Answer(IndexNo) = LineInput(FileNum)
            IndexNo = IndexNo + 1
        Loop Until EOF(FileNum)
        FileClose(FileNum)
        Dim Obj As Object, Count As Integer = 0
        For Each Obj In Me.Controls
            If TypeOf Obj Is Label Then
                MyLabels(Count) = Obj
                Count = Count + 1
            End If
        Next
        Dim Random1, Random2 As Integer
        Dim TempTerm, TempAnswer As Object
        For Count = 0 To 15
            Randomize()
            Random1 = Val(Int(16 * Rnd()))
            Random2 = Val(Int(16 * Rnd()))
            TempTerm = Term(Random1)
            Term(Random1) = Term(Random2)
            Term(Random2) = TempTerm
            TempAnswer = Answer(Random1)
            Answer(Random1) = Answer(Random2)
            Answer(Random2) = TempAnswer
            Count = Count + 1
        Next
        For Count = 0 To 15
            MyLabels(Count).Text = Term(Count)
        Next

如果有人有任何想法,我们将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

即使你的问题不是很清楚。从我得到的,你想要有一些方法来保持你的标签和相应的术语和答案同步。

有很多方法可以做到这一点,但我更愿意这样做...完美的VB.NET方式,而不是使用任何传统的VB6技术。

首先声明一个将我们的Term和Answers保存在一个对象中的类。这相当于VB6中的Type

Public Class TermAnswer
    Public Term As String
    Public Answer As String
    ' you may add more fields/properties here if you wish to...
End Class

现在很容易编写我们的解决方案。

' declare a Dictionary object with Label as key and the corresponding Term and Answers as values.
Dim TermAnswers As New Dictionary(Of Label, TermAnswer)

' this is a temporary List to hold our Term and Answers read from file until we randomize them.
Dim tempTermAnswers As New List(Of TermAnswer)

' Our Labels array... yes it is this easy :)  
Dim myLabels() As Label = Me.Controls.OfType(Of Label)().ToArray

' read our file into the tempTermAnswers List 
Dim FileTerm As String = "D:\soccer.txt"
Using reader As New IO.StreamReader(FileTerm)
    While Not reader.EndOfStream
Dim ta As New TermAnswer
        ta.Term = reader.ReadLine
        ta.Answer = reader.ReadLine
        tempTermAnswers.Add(ta)
    End While
    reader.Close()
End Using

' pick Term Answers from our tempTermAnswers List randomly and add them to our TermAnswers Dictionary
' we also set our Label text here, though you can loop separately too
Dim randomNumbers As New Random
Dim tempTerm As TermAnswer, randomNumber As Integer
For Each label In myLabels
    randomNumber = randomNumbers.Next(0, tempTermAnswers.Count)
    tempTerm = tempTermAnswers(randomNumber)
    TermAnswers.Add(label, tempTerm)
    tempTermAnswers.Remove(tempTerm)
    label.Text = tempTerm.Term
Next

' now you have your term answers in a Dictionary, indexed by Label.
' you can get any of them by providing a Label on your form as key and get the corresponding Term and Answer as value.
' e.g. let us list the Label Name, Term and Answer in our debug window...
For Each label In myLabels
    Debug.WriteLine(label.Name & " ... " & TermAnswers(label).Term & " ... " & TermAnswers(label).Answer)
Next