Textbox autocomplete string collection Help

abhit_kumar

Member
Joined
Aug 13, 2008
Messages
19
Programming Experience
Beginner
Dear Expert,

I have made taken one textbox in form, in which i have written following logic in Got Focus event.

When i focus on that its automatically showing all the suggested name from database field.

CODE:-

VB.NET:
Private Sub CustomerName_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CustomerName.GotFocus

        da = New SqlDataAdapter("Select CustomerName from CustomerMaster", myConnection)
        da.Fill(ds, "CustomerMaster")
        
        Dim acFirstName As New AutoCompleteStringCollection

        For Each dr As DataRow In Me.ds.Tables("CustomerMaster").Rows
            acFirstName.Add(dr.Item("CustomerName").ToString)
        Next
        CustomerName.AutoCompleteCustomSource = acFirstName

    End Sub

I want that if suppose, i will type some other name which is not exist in autocomplete string collection then its should gives error.

Please guide me.

Regards,
AKM
 
Autocomplete Collection doesnt work with key press event

Hello All Experts,

In Key Press Event i have put the logic of search in textbox, when i press enter key it will take the values from textbox and brings the data in DataGrid.

In another event of GotFocus there is an logic of autocompletestring collection from Table. If i type a in that textbox it will shows all the suggested string in that.

If i comment the logic of GotFocus Event for textbox for autocomplete suggestion then only then Key Press events work else its not working. I dont know why its happening.

So GotFocus loic creates the problem.

Please see the code below.

VB.NET:
Private Sub CustomerName_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles CustomerName.KeyPress

        If e.KeyChar = Chr(13) Then
            Me.FS_ProjectDataSet.Clear()
            MsgBox(CustomerName.Text)

            Dim da As SqlDataAdapter = New SqlDataAdapter("Select * from InwardMaster where CustomerName='" & CustomerName.Text & "'", myConnection)

            da.Fill(Me.FS_ProjectDataSet.InwardMaster)

            DataGridView1.DataSource = Me.FS_ProjectDataSet.InwardMaster

            If (Me.DataGridView1.Rows.Count = 0) Then
                MsgBox("No Record has been Found for Customer " & CustomerName.Text, MsgBoxStyle.Critical, "No Record")
            End If
        End If
    End Sub

Private Sub CustomerName_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles CustomerName.GotFocus
        Dim DataAdap As SqlDataAdapter = New SqlDataAdapter("Select CustomerName from CustomerMaster", myConnection)
        DataAdap.Fill(ds1, "CustomerMaster")

        Dim acFirstName As New AutoCompleteStringCollection

        For Each dr As DataRow In Me.ds1.Tables("CustomerMaster").Rows
            acFirstName.Add(dr.Item("CustomerName").ToString)
        Next
        CustomerName.AutoCompleteCustomSource = acFirstName
    End Sub

Please let me know in which code part im doing wrong stuff.

Regards,
AKM
 
Back
Top