为什么它在 VB.NET 中的 KeyPress Enter 事件文本框中不起作用

why doesn't it work in the keypress enter event textbox in VB.NET

提问人:siwa86 提问时间:10/26/2023 最后编辑:JayVsiwa86 更新时间:10/26/2023 访问量:30

问:

我正在尝试在 VB.NET 中执行按键输入事件文本框。

我有下面的代码,但是 但它没有用。

请指导我

谢谢

以下是我使用的代码


Public Class Form1
    Private iService As New ItemService()
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

'if the code is 3 lines below I commentout then the textbox keypress enter works
        TextBoxCodeProduct.AutoCompleteMode = AutoCompleteMode.Suggest
        TextBoxCodeProduct.AutoCompleteSource = AutoCompleteSource.CustomSource
        TextBoxCodeProduct.AutoCompleteCustomSource.AddRange(iService.Getitem.Select(Function(n) n.CodeProduct).ToArray())

    End Sub
    Private Sub GetItemData(ByVal iditem As String)
        Dim item = iService.GetByCodeProductOrBarcode(iditem)
        If item IsNot Nothing Then
            If String.Equals(iditem, item.CodeProduct, StringComparison.CurrentCultureIgnoreCase) Then
                TextBoxCodeProduct.Text = item.CodeProduct
            End If
            TextBoxBarcode.Text = item.Barcode
        Else
            MsgBox("Not Found item product code...")
            TextBoxCodeProduct.Clear()
            TextBoxBarcode.Clear()
            Return
        End If
    End Sub
    Private Sub TextBoxCodeProduct_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBoxCodeProduct.KeyPress
        If e.KeyChar = Convert.ToChar(Keys.Enter) Then
            GetItemData(TextBoxCodeProduct.Text)
        ElseIf e.KeyChar = Convert.ToChar(Keys.Back) Then
            TextBoxCodeProduct.Clear()
            TextBoxBarcode.Clear()
        End If
    End Sub
End Class
Public Class Item
    Public Property Id() As Integer
    Public Property CodeProduct() As String
    Public Property Barcode() As String
End Class

'以下是服务方法

Public Class ItemService
    Public Function Getitem() As IEnumerable(Of Item)
        Dim sql = "SELECT * FROM Items"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            Return _conn.Query(Of Item)(sql).ToList()
        End Using
    End Function

Public Function GetByCodeProductOrBarcode(ByVal code As String) As Item
        Dim sql = $"SELECT * FROM Items WHERE CodeProduct = @code or Barcode = @code"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            Return _conn.Query(Of Item)(sql, New With {Key .code = code}).FirstOrDefault()
        End Using
    End Function
End Class
vb.net WinForms 自动完成 文本框 按键

评论


答: 暂无答案