none
请教有关textbox control 的问题 RRS feed

  • 问题

  • 我正在篇写一有关textbox的代码。

    我有四个textbox1,textbox2,textbox3,textbox4

    textbox1 和 textbox2 是一个pair

    textbox3 和 textbox4 是一个pair

    如果user在textbox1 和 textbox2 填资料就会得到 result pass

    如果user在textbox1 填资料, textbox2 没有填质料就会得到 result fail

    如果user在textbox1 和 textbox2 还有 textbox3 填资料就会得到 result fail

    如果user在textbox1,textbox2, textbox3和textbox4  填资料就会得到 result pass

    如果textbox1,textbox2,textbox3 和textbox4 没有填资料就没有事情发生。

    我尝试写了以下代码请查看

       Private Sub textbox_control1()

            Dim t1IsEmpty As Boolean = sg1.Text = Nothing
            Dim t2IsEmpty As Boolean = sg2.Text = Nothing
            Dim t3IsEmpty As Boolean = sg3.Text = Nothing
            Dim t4IsEmpty As Boolean = sg4.Text = Nothing
            Dim t5IsEmpty As Boolean = sg5.Text = Nothing
            Dim t6IsEmpty As Boolean = sg6.Text = Nothing
            Dim t7IsEmpty As Boolean = sg7.Text = Nothing
            Dim t8IsEmpty As Boolean = sg8.Text = Nothing

            Dim valid As Boolean = True

            If Not t1IsEmpty Then

                If t2IsEmpty Then
                    valid = False
                    MsgBox("FAIL 1 2")
                Else

                    If Not t3IsEmpty Then

                        If t4IsEmpty Then
                            valid = False
                            MsgBox("FAIL 3 4")
                        Else

                            If Not t5IsEmpty Then

                                If t6IsEmpty Then
                                    valid = False
                                    MsgBox("FAIL 5 6")
                                Else

                                    If Not t7IsEmpty Then

                                        If t8IsEmpty Then
                                            valid = False
                                            MsgBox("FAIL 7 8")
                                        End If
                                    End If
                                End If
                            Else
                                valid = False
                            End If

                            Return
                        End If
                    End If
                End If
            End If
        End Sub

    我想将textbox_control1的代码add 进数据库可以怎么加入。

    我想呈现的是当user receive    MsgBox("FAIL 3 4") user就无法成功的将data 存进数据库。请指教

    以下是我save的代码

     Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
            If Not cnn.State = ConnectionState.Open Then
                cnn.Open()
            End If

            cmd.Connection = cnn
            textbox_control1()

            'If CStr(Me.ID.Tag) & "" = "" Then
            If PictureBox1.Image IsNot Nothing Then
                Dim DataPic_Update As Byte() = ImageToBytes(PictureBox1.Image, ImageFormat.Png)
                cmd.CommandText = "insert into Table1(Picture, F1, F2, F3)" &
                                "values (@picture, @F1, @F2, @F3)"

                cmd.Parameters.AddWithValue("@picture", DataPic_Update)
            Else
                cmd.CommandText = "insert into Table1(F1, F2, F3)" &
                                "values (@F1, @F2, @F3)"
            End If


            cmd.Parameters.AddWithValue("@F1", Me.TextBox1.Text)
            cmd.Parameters.AddWithValue("@F2", Me.TextBox2.Text)
            cmd.Parameters.AddWithValue("@F3", Me.TextBox3.Text)


            cmd.ExecuteNonQuery()
            RefreshData()
            MessageBox.Show("Record Is Added")




    2020年6月1日 6:10

答案

  • Hi christing,

    基本的逻辑是:textbox1 和 textbox2 为一组,textbox3 和 textbox4 为一组,首先判断每个组是否都满足条件,并计算出每个组的 bool 值,然后排除掉所有 textbox 都为空的情况,必须每个组都满足条件(bool 为 True),最终的 bool 值才为 True.

    参考以下代码:

        Dim valid12 As Boolean
        Dim valid34 As Boolean
        Dim valid As Boolean
    
        Private Sub textbox_control1()
            valid12 = False
            valid34 = False
            valid = False
            If (TextBox1.Text IsNot "" And TextBox2.Text IsNot "") OrElse (TextBox1.Text Is "" And TextBox2.Text Is "") Then
                valid12 = True
            End If
            If (TextBox3.Text IsNot "" And TextBox4.Text IsNot "") OrElse (TextBox3.Text Is "" And TextBox4.Text Is "") Then
                valid34 = True
            End If
            If TextBox1.Text Is "" And TextBox2.Text Is "" AndAlso TextBox3.Text Is "" And TextBox4.Text Is "" Then
                Return
            ElseIf valid12 And valid34 Then
                valid = True
            End If
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            textbox_control1()
            MessageBox.Show(valid.ToString)
        End Sub

    测试结果:

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 christing 2020年6月2日 8:50
    2020年6月2日 2:08
    版主

全部回复

  • 我尝试了以下save代码(user 需要填完sg1.Text 至 sg8.Text)才能save

     If Not cnn.State = ConnectionState.Open Then
                cnn.Open()
            End If

            textbox_control()

            If Control = False Then
                MessageBox.Show("PLS MAKE SURE YOUR STRAIN_GAUGE NO")

                cmd.Parameters.Clear()
            Else

                Try

                    cmd.Connection = cnn
                    textbox_control1()

                    'If CStr(Me.ID.Tag) & "" = "" Then
                    If PictureBox1.Image IsNot Nothing Then
                        Dim DataPic_Update As Byte() = ImageToBytes(PictureBox1.Image, ImageFormat.Png)
                        cmd.CommandText = "insert into Table1(Picture, F1, F2, F3)" &
                                        "values (@picture, @F1, @F2, @F3)"

                        cmd.Parameters.AddWithValue("@picture", DataPic_Update)
                    Else
                        cmd.CommandText = "insert into Table1(F1, F2, F3)" &
                                        "values (@F1, @F2, @F3)"
                    End If


                    cmd.Parameters.AddWithValue("@F1", Me.TextBox1.Text)
                    cmd.Parameters.AddWithValue("@F2", Me.TextBox2.Text)
                    cmd.Parameters.AddWithValue("@F3", Me.TextBox3.Text)


                    cmd.ExecuteNonQuery()
                    RefreshData()
                    MessageBox.Show("Record Is Added")
                    cnn.Close()
                Catch a As Exception

                End Try

            End If

    2020年6月2日 1:46
  • Hi christing,

    基本的逻辑是:textbox1 和 textbox2 为一组,textbox3 和 textbox4 为一组,首先判断每个组是否都满足条件,并计算出每个组的 bool 值,然后排除掉所有 textbox 都为空的情况,必须每个组都满足条件(bool 为 True),最终的 bool 值才为 True.

    参考以下代码:

        Dim valid12 As Boolean
        Dim valid34 As Boolean
        Dim valid As Boolean
    
        Private Sub textbox_control1()
            valid12 = False
            valid34 = False
            valid = False
            If (TextBox1.Text IsNot "" And TextBox2.Text IsNot "") OrElse (TextBox1.Text Is "" And TextBox2.Text Is "") Then
                valid12 = True
            End If
            If (TextBox3.Text IsNot "" And TextBox4.Text IsNot "") OrElse (TextBox3.Text Is "" And TextBox4.Text Is "") Then
                valid34 = True
            End If
            If TextBox1.Text Is "" And TextBox2.Text Is "" AndAlso TextBox3.Text Is "" And TextBox4.Text Is "" Then
                Return
            ElseIf valid12 And valid34 Then
                valid = True
            End If
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            textbox_control1()
            MessageBox.Show(valid.ToString)
        End Sub

    测试结果:

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 christing 2020年6月2日 8:50
    2020年6月2日 2:08
    版主
  • @XING YU ZHAO 

    谢谢你的答复。我想问如果user要save data 进database 的话。

    textbox1 有 textbox2 没有(无法save进database)

    textbox1 有 textbox2 有 (save成功进database)

    textbox1 有 textbox2 有 

    textbox3 有 textbox4 没有(无法save进database)

    textbox1 有 textbox2 有 

    textbox3 有 textbox4 有(save成功进database)

    以下是我尝试的代码(想请帮忙看看我的代码是否missing了什么因为我save第一pair数据成功第二pair数据失败)

    textbox4 没有填也save成功

            If Not cnn.State = ConnectionState.Open Then
                cnn.Open()
            End If
            cmd.Parameters.Clear()

            textbox_control1()

            If valid12 = False Then
            ElseIf valid34 = False Then
            ElseIf valid54 = False Then
            ElseIf valid8 = False Then

            Else
                Try
                        cmd.Connection = cnn
                        textbox_control1()

                        'If CStr(Me.ID.Tag) & "" = "" Then
                        If PictureBox1.Image IsNot Nothing Then
                            Dim DataPic_Update As Byte() = ImageToBytes(PictureBox1.Image, ImageFormat.Png)
                            cmd.CommandText = "insert into Table1(Picture, F1, F2, F3)" &
                                            "values (@picture, @F1, @F2, @F3)"

                            cmd.Parameters.AddWithValue("@picture", DataPic_Update)
                        Else
                            cmd.CommandText = "insert into Table1(F1, F2, F3)" &
                                            "values (@F1, @F2, @F3)"
                        End If


                        cmd.Parameters.AddWithValue("@F1", Me.TextBox1.Text)
                        cmd.Parameters.AddWithValue("@F2", Me.TextBox2.Text)
                        cmd.Parameters.AddWithValue("@F3", Me.TextBox3.Text)


                        cmd.ExecuteNonQuery()
                        RefreshData()
                        MessageBox.Show("Record Is Added")
                        cnn.Close()
                    Catch a As Exception

                    End Try

                End If



    2020年6月2日 3:11
  • Hi christing,

    尝试以下结构:

       Private Sub Save_Click(sender As Object, e As EventArgs) Handles Save.Click
            textbox_control1()
    
            If valid Then
                If Not cnn.State = ConnectionState.Open Then
                    cnn.Open()
                End If
                ‘...
            Else
                MessageBox.Show("Please fill in the correct data")
    
            End If
        End Sub

    另外,我注意到你似乎不止4个 textbox , 并且 insert 时有三个 textbox, 如果以上不能解决你的问题,请更清晰的描述一下你的所有需要的 textbox, 这样有助于我们更好的分析你的问题。

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2020年6月2日 6:08
    版主