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

Time:12-14

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

autocomplete combobox

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

CodePudding user response:

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
  • Related