我有9个标签,每个标签包含一个名为txthidden
的文本框中的1个字母。
当我点击按钮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
答案 0 :(得分: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