0

我有 9 个标签,每个标签包含一个来自名为txthidden. 当我点击一个按钮button3时,标签会显示不同的字母数组。我的问题不是让它们出现,而是当文本框中没有 9 个字母时,它会出现错误消息:

Index was outside the bounds of the array.

有没有办法只显示与文本框长度匹配的标签?

提前致谢!

这是我的代码:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Dim hiddenarray() As Char
    hiddenarray = Me.txthidden.Text.ToCharArray
    lbl1.Text = hiddenarray(0)
    lbl2.Text = hiddenarray(1)
    lbl3.Text = hiddenarray(2)
    lbl4.Text = hiddenarray(3)
    lbl5.Text = hiddenarray(4)
    lbl6.Text = hiddenarray(5)
    lbl7.Text = hiddenarray(6)
    lbl8.Text = hiddenarray(7)
    lbl9.Text = hiddenarray(8)
End Sub
4

1 回答 1

0

有很多方法,例如一个简单的解决方案是检查:

lbl1.Text = IIf(hiddenarray.Lenght > 0, hiddenarray(0), "")
lbl2.Text = IIf(hiddenarray.Lenght > 1, hiddenarray(1), "")
'etc

另一种方法是按名称循环选择文本框:

For i As Integer = 0 To hiddenarray.Lenght - 1
    Me.Controls("lbl" & i + 1).Text = hiddenarray(i)
Next
于 2013-04-26T05:39:59.933 回答