Visual Basic循环中的错误

时间:2018-02-06 10:19:23

标签: vb.net

显然我在循环的第二个条件中弄错了,我留下了一张图片以便更好地理解。

由于某种原因,错误不会显示在第一个if条件中,即使它们几乎相同。

为了把它放在上下文中,我有一个很大的第一个文本框和其他9个小文本框。基本上我想把我在大文本框中写的值逐个发送给小文本。

Big TextBox name属性为t0,而其他属性从t1转到t9

VISUAL BASIC CODE

 Public Class Form1

        Dim array() As TextBox = {t1, t2, t3, t4, t5, t6, t7, t8, t9}

        Private Sub t0_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles t0.KeyDown
            If e.KeyCode = Keys.Enter Then

                If sender.Text.ToString <> String.Empty Then

                    For indice As Integer = 0 To array.Length - 1

                        If array(indice).Text = String.Empty Then '<== The error is in this line

                            array(indice).Text = sender.Text.ToString

                            Exit For

                        End If

                    Next

                End If

            End If

        End Sub

    End Class

enter image description here

更新

使用等于这个时间,但仍然是相同的错误

1 个答案:

答案 0 :(得分:1)

Nothing的一项似乎是if。您可以使用以下AndAlso条件(使用If TypeOf(array(indice)) Is TextBox AndAlso array(indice).Text = String.Empty Then array(indice).Text = sender.Text.ToString Exit For End If )来避免此类错误:

if

注意:您的第一个array条件不使用if,因此不会发生错误。

但问题不在于array条件!您早期定义了array。当您定义Nothing表单的控件未初始化时(因此每个控件都为InitializeComponent,直到调用方法/构造函数New中的array)。

您可以直接在KeyDown事件上初始化TextBox,以确保创建/初始化Private Sub t0_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles t0.KeyDown If e.KeyCode = Keys.Enter Then If sender.Text.ToString <> String.Empty Then Dim array() As TextBox = {t1, t2, t3, t4, t5, t6, t7, t8, t9} For indice As Integer = 0 To array.Length - 1 If TypeOf(array(indice)) Is TextBox AndAlso array(indice).Text = String.Empty Then array(indice).Text = sender.Text.ToString Exit For End If Next End If End If End Sub 元素。

array

另一种全局使用array的解决方案是初始化Load形式的POST /update_location HTTP/1.1 Host: example.com Content-type: application/json Content-length: ... { ID: "foo", location: "-121.45356 46.51119" }