VB.NET中文本框之间的选择

时间:2012-02-22 20:49:26

标签: vb.net

我有3个文本框TextBox1,TextBox2,TextBox3和一个按钮,我想做以下

如果我在TexBox1中输入一个值并按下按钮,则会在textbox2和textbox3中设置一些文本;如果我在TextBox2中输入一个值并按下按钮,一些数据将显示在TextBox1和TextBox3中。

我不知道如何检查代码是否textbox1值不为null且textbox2值为null,以执行某个语句并将值设置为Textbox2和textbox3;如果Textbox2值的值为null且textbox2值不为null,则执行某个语句并将值设置为Textbox1和textbox3

更新

我的问题在于此代码。如果2个文本框的值为空,我收到正确的消息,当第一个文本框插入数据而第二个文本框为空时,返回的值正确,但是当我尝试在第二个文本框中输入值时,我得到了捕获异常

Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click


    Try


        If SQLConnection.State = ConnectionState.Open Then
            If (Me.TextBox5.Text.Length = 0 And Me.TextBox6.Text.Length = 0) Then
                MsgBox("Check your input values.")

            ElseIf (Me.TextBox5.Text.Length > 0 And Me.TextBox6.Text.Length = 0) Then
                Dim myAdapter As New MySqlDataAdapter("select * from university_members, members_car where(university_members.member_id = members_car.member_id) AND car_id = " & TextBox5.Text, SQLConnection)
                Dim mydatatable As New DataTable()
                Dim dataset As New DataSet()
                myAdapter.Fill(mydatatable)


                If (mydatatable.Rows.Count > 0) Then
                    TextBox6.Text = mydatatable.Rows(0).Item("plate_number")
                    TextBox7.Text = mydatatable.Rows(0).Item("first_name")
                    TextBox8.Text = mydatatable.Rows(0).Item("last_name")
                    TextBox9.Text = mydatatable.Rows(0).Item("type")
                    TextBox10.Text = mydatatable.Rows(0).Item("chassis_id")
                    TextBox11.Text = mydatatable.Rows(0).Item("model")
                    TextBox12.Text = mydatatable.Rows(0).Item("color")
                    ComboBox2.SelectedItem = mydatatable.Rows(0).Item("code")


                Else
                    MsgBox("Check Error: ID Not Found! Enter a Valid ID")
                    ComboBox2.ResetText()
                    TextBox5.Text = " "
                    TextBox6.Text = " "
                    TextBox7.Text = " "
                    TextBox8.Text = " "
                    TextBox9.Text = " "
                    TextBox7.ReadOnly = True
                    TextBox8.ReadOnly = True
                    TextBox9.ReadOnly = True
                    TextBox10.Text = " "
                    TextBox11.Text = " "
                    TextBox12.Text = " "
                End If

            ElseIf (Me.TextBox5.Text.Length = 0 And Me.TextBox6.Text.Length > 0) Then
                Dim myAdapter As New MySqlDataAdapter("select * from university_members, members_car where(university_members.member_id = members_car.member_id) AND first_name = " & TextBox6.Text & "AND code = '" & ComboBox2.SelectedItem & "'", SQLConnection)
                Dim mydatatable As New DataTable()
                Dim dataset As New DataSet()
                myAdapter.Fill(mydatatable)

                If (mydatatable.Rows.Count > 0) Then
                    TextBox5.Text = mydatatable.Rows(0).Item("car_id")
                    TextBox7.Text = mydatatable.Rows(0).Item("first_name")
                    TextBox8.Text = mydatatable.Rows(0).Item("last_name")
                    TextBox9.Text = mydatatable.Rows(0).Item("type")
                    TextBox10.Text = mydatatable.Rows(0).Item("chassis_id")
                    TextBox11.Text = mydatatable.Rows(0).Item("model")
                    TextBox12.Text = mydatatable.Rows(0).Item("color")



                Else
                    MsgBox("Check Error: ID Not Found! Enter a Valid ID")
                    ComboBox2.ResetText()
                    TextBox5.Text = " "
                    TextBox6.Text = " "
                    TextBox7.Text = " "
                    TextBox8.Text = " "
                    TextBox9.Text = " "
                    TextBox7.ReadOnly = True
                    TextBox8.ReadOnly = True
                    TextBox9.ReadOnly = True
                    TextBox10.Text = " "
                    TextBox11.Text = " "
                    TextBox12.Text = " "
                End If


            End If
        Else
            MsgBox("No Record Found.")
        End If



    Catch ex As Exception
        MsgBox("Fatal Error.")
        MsgBox("Application will now exit.")
        Application.Exit()

    End Try


End Sub

1 个答案:

答案 0 :(得分:1)

检查length属性

If Me.txtBox1.Text.Length > 0 then
'value exists in textbox 1 continue...
end if