【问题标题】:Assign Array Position to a Label Tag Property将数组位置分配给标签标记属性
【发布时间】:2021-03-11 17:00:32
【问题描述】:

我想为 16 个标签的 Tag 属性分配一个数组。这是我想出的:

Label1.Tag = FotoArray(0)
Label2.Tag = FotoArray(1)
Label3.Tag = FotoArray(2)
Label4.Tag = FotoArray(3)
Label5.Tag = FotoArray(4)
Label6.Tag = FotoArray(5)
Label7.Tag = FotoArray(6)
Label8.Tag = FotoArray(7)
Label9.Tag = FotoArray(8)
Label10.Tag = FotoArray(9)
Label11.Tag = FotoArray(10)
Label12.Tag = FotoArray(11)
Label16.Tag = FotoArray(12)
Label13.Tag = FotoArray(13)
Label15.Tag = FotoArray(14)
Label14.Tag = FotoArray(15)

有没有办法以更短的方式做到这一点?

【问题讨论】:

标签: arrays vb.net tags label


【解决方案1】:

假设Label16.Tag = FotoArray(12) 是一个错字,这应该可以工作:

For counter As Integer = 1 To 16
    Dim label As Label = Me.Controls.Find("Label" & counter, True).FirstOrDefault()
    If Not label Is Nothing Then
        label.Tag = FotoArray(counter - 1)
    End If
Next

您也可以简单地改用Me.Controls(name)。更新后的代码如下所示:

For counter As Integer = 1 To 16
    Dim label As Label = Me.Controls("Label" & counter)
    If Not label Is Nothing Then
        label.Tag = FotoArray(counter - 1)
    End If
Next

【讨论】:

  • 这两种方法都不会使用 Option Strict On 进行编译。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-27
  • 1970-01-01
  • 2018-03-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多