vb.net Visual Basic 在数组中设置标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14418955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:57:02  来源:igfitidea点击:

Visual Basic Setting Labels in an Array

vb.netvisual-studio

提问by Polaric

I'm writing a little program in Visual Basic in Studio '10. I have a series of eight arrays that I need to set text too in a for loop. I need to set the text based off of the label number(eg label 1 gets word1, label 2 gets word2) Is there a way to create a array, then set my existing labels inside this array so I can then say something such as

我正在 Studio '10 中用 Visual Basic 编写一个小程序。我有一系列八个数组,我也需要在 for 循环中设置文本。我需要根据标签编号设置文本(例如标签 1 获得 word1,标签 2 获得 word2)有没有办法创建一个数组,然后在这个数组中设置我现有的标签,这样我就可以说一些诸如

for i = 1 to 8
    subsets(i).Text = words(w + i)
next   
  • 'i' being the variable of a for loop
  • subsets being the array name.
  • 'words' is a array containing strings I'm a bit new at this, so thanks!
  • 'i' 是 for 循环的变量
  • 子集是数组名称。
  • 'words' 是一个包含字符串的数组,我对此有点陌生,谢谢!

回答by Oded

You can create an array (or list) of Labeland add each label control to it.

您可以创建一个数组(或列表)Label并将每个标签控件添加到其中。

This will allow you to loop and assign values as you describe.

这将允许您按照您的描述循环和分配值。

Dim subsets As New List(Of Label)
subsets.Add(label1)
subsets.Add(label2)
...

回答by Mark Hall

Since you want to base the word on the label's name, you should make an array or a list as Oded suggested. You can then use the String.Removemethod to remove the word Label from your labels name, cast it to an int and subtract 1 since arrays in .Net are 0 based.

由于您想根据标签名称创建单词,因此您应该按照 Oded 的建议制作一个数组或列表。然后,您可以使用该String.Remove方法从标签名称中删除单词 Label,将其转换为 int 并减去 1,因为 .Net 中的数组是基于 0 的。

Something like this.

像这样的东西。

Public Class Form1

    Dim subsets(7) As Label
    Dim words() As String = New String() {"this", "is", "a", "test", "of", "text", "replacement", "."}
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        subsets(0) = Label1
        subsets(1) = Label2
        subsets(2) = Label3
        subsets(3) = Label4
        subsets(4) = Label5
        subsets(5) = Label6
        subsets(6) = Label7
        subsets(7) = Label8

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        For Each lbl As Label In subsets
            lbl.Text = words(CInt(lbl.Name.Remove(0, 5)) - 1)
        Next
    End Sub
End Class

回答by SubtleStu

I think this is what you were trying to do, though I may be wrong

我认为这就是你想要做的,虽然我可能是错的

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim words() As String = {"one", "two", "three", "four", "five", "six", "seven", "eight"}
    Dim subsets() As Control = {Label1, Label2, Label3, Label4, Label5, Label6, Label7, Label8}

    For i = 0 To 7
        subsets(i).Text = words(i)
    Next

End Sub
End Class

This assigns the words array to the labels text property

这将单词数组分配给标签文本属性