文本框数据输出

时间:2019-07-15 04:45:06

标签: vb.net

早上好,我的代码遇到问题

Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
    If e.KeyCode = Keys.Tab Then

        checkoperator()
        TextBox1.Text = ""
        SendKeys.Send("{BACKSPACE}")
        If lbloperatorb.Text = "" Then
            TextBox1.Text = ""
            MsgBox("User ID does not exist.")
            lbloperatorb.Text = ""
            TextBox1.Focus()
        Else
            TextBox1.Text = lbloperatorb.Text
            TextBox1.Enabled = False
        End If

        cmbmodel1.Focus()

    End If

End Sub

  Public Sub checkoperator()

    Dim cmd As New MySqlCommand
    cmd.Connection = conn
    cmd.CommandText = "SELECT empname FROM tbluser where userid= '" + TextBox1.Text + "'"
    lbloperatorb.Text = cmd.ExecuteScalar

End Sub

当我使用制表符作为我的“ ENTER”并在文本框中粘贴查询时,这就是我所得到的

sample photo. with tab before my text

我已经尝试在发送数据之前删除文本框。但什么也没发生

1 个答案:

答案 0 :(得分:0)

使用KeyDown事件代替Leave。下面的代码将起作用。

Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave

    checkoperator()
    'TextBox1.Text = ""
    'SendKeys.Send("{BACKSPACE}")
    If lbloperatorb.Text = "" Then
        TextBox1.Text = ""
        MsgBox("User ID does not exist.")
        lbloperatorb.Text = ""
        TextBox1.Focus()
    Else
        TextBox1.Text = lbloperatorb.Text
        TextBox1.Enabled = False
        lbloperatorb.Text = ""
    End If

    'lbloperatorb.Focus()

End Sub