VB .NET
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Retrieve all the Company Names in the table
Dim AppPath As String = Mid(Application.ExecutablePath, 1, Len(Application.ExecutablePath) - 14)
Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = " + AppPath + "Samples.mdb"
Dim dbConn As System.Data.OleDb.OleDbConnection = _
New System.Data.OleDb.OleDbConnection(strConn)
dbConn.Open()
Dim DSet As New DataSet, SQLStr As String
Dim cmd As System.Data.OleDb.OleDbCommand
Dim dbAdaptr As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
Dim tRow As DataRow, tTbl As DataTable
With dbAdaptr
.TableMappings.Add("Table", "Companies")
SQLStr = "Select [Company Name] from Companies"
cmd = New System.Data.OleDb.OleDbCommand(SQLStr, dbConn)
cmd.CommandType = CommandType.Text
.SelectCommand = cmd
.Fill(DSet)
.Dispose()
End With
DSet.AcceptChanges()
tTbl = DSet.Tables.Item(0)
DSet.Dispose()
dbConn.Close()
' fill out array by Company Names for cboRightCombo combobox
cboRightCombo.Text = ""
cboRightCombo.Items.Clear()
cboRightCombo.BeginUpdate()
' Load the Company Names into the ComboBox Control
For Each tRow In tTbl.Rows
cboRightCombo.Items.Add(tRow("Company Name").ToString)
Next
cboRightCombo.EndUpdate()
End Sub
' Close application
Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles cmdClose.Click
Me.Close()
End Sub
' Use event "Change" for alphabetical search an appropriate
' value in combo box list and put it into combobox Text.
Private Sub cboRightCombo_TextChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles cboRightCombo.TextChanged
Dim boxIndex As Integer, lExst As Boolean
Dim box As ComboBox = sender
Dim txt As String = box.Text
Dim posCursor As Integer = box.SelectionStart
' If Cursor does not stay on the beginning of text box.
If posCursor <> 0 Then
lExst = False
' Go in cycle through the combo box list to
' find the appropriate entry in the list
For boxIndex = 0 To box.Items.Count - 1
If UCase(Mid(box.Items(boxIndex), 1, posCursor)) = UCase(Mid(txt, 1, posCursor)) Then
box.Text = box.Items(boxIndex)
box.SelectionStart = posCursor
lExst = True
Exit For
End If
Next
' We didn't find appropriate entry and return previous value to text box
If Not lExst Then
box.Text = Mid(txt, 1, posCursor - 1) + Mid(txt, posCursor + 1)
box.SelectionStart = posCursor - 1
End If
End If
End Sub
End Class
|