简体   繁体   中英

how combobox autocomplete when selected and then press enter keyboard then fill datagridview

Dear All Master,

how combobox autocomplete when selected and then press enter keyboard then fill datagridview.

I tried typing in combobox then it appeared autocomplete then I selected it and tried to press Enter on the keyboard but the fill datagridView did not appear whether there was something wrong with my code. Please Recommend.

Thanks

自动完成组合框

Private Sub PopulateComboBox()
 Dim query As String = "SELECT DISTINCT PNM FROM GSDTS UNION SELECT DISTINCT PNM FROM GSGTS ORDER BY PNM"
        Try
            Using con As OleDbConnection = New OleDbConnection(cn)
                Using sda As OleDbDataAdapter = New OleDbDataAdapter(query, con)
                    'Fill the DataTable with records from Table.
                    Dim dt As DataTable = New DataTable()
                    sda.Fill(dt)
  'Insert the Default Item to DataTable.
                    Dim row As DataRow = dt.NewRow()
                    row(0) = ""
                    dt.Rows.InsertAt(row, 0)
                    Dim PNM As IList(Of String) = New List(Of String)
 For Each _row As DataRow In dt.Rows
                        PNM.Add(_row("PNM"))
                    Next
                    Me.ComboBox1.Items.AddRange(PNM.ToArray)
                    Me.ComboBox1.AutoCompleteMode = AutoCompleteMode.Suggest
                    Me.ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems
                    'Assign DataTable as DataSource.
                    ComboBox1.DataSource = dt
                    ComboBox1.DisplayMember = "PNM"
                    ComboBox1.ValueMember = "PNM"
          End Using
            End Using
        Catch myerror As OleDbException
            MessageBox.Show("Error: " & myerror.Message)
        Finally

        End Try
    End Sub
 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.PopulateComboBox()
 fillDataGridView1()
    End Sub
Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted
        fillDataGridView1()
 End Sub

You have to handle an event for the combobox. The KeyDown event is automatically called when an item is selected. So you can do this for both selection and pressing the Enter key.

Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
    If e.KeyCode = Keys.Enter And ComboBox1.Text.Length > 0 Then
        MessageBox.Show($"You selected {ComboBox1.Text}")
    End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM