Saturday, November 5, 2011

Auto complete in Combo Box on Visual Basic .Net


The Windows Forms ComboBox control is used to display data in a drop-down combo box. By default, the ComboBox control appears in two partsthe top part is a text box that allows the user to type a list item. The second part is a list box that displays a list of items from which the user can select one.
‘Sample code on how to create a auto complete box on visual basic .net 
Private Sub cboName_Leave(ByVal sender As Object, ByVal e As System.EventArgs)
Handles cboName.Leave

Dim recRowView As DataRowView

Dim recName As DB.tblNameRow
AutoCompleteCombo_Leave(cboName)

‘OPTIONAL: Now you can do some extra handling if you want

‘Get the Selected Record from my Data Bound Combo (Return Type is DataRowView)
recRowView = cboName.SelectedItem

If recRowView Is Nothing Then Exit Sub

‘Display the Name Info (Row Type comes from my bound Dataset)
recName = recRowView.Row
lblAccountNum.Text = recName.AccountNum
lblCompanyName.Text = recName.CompanyName
End Sub
Private Sub cboName_KeyUp(ByVal sender As Object,
ByVal e As System.Windows.Forms.KeyEventArgs) Handles cboName.KeyUp
AutoCompleteCombo_KeyUp(cboName, e)
End Sub
‘Here are the Generic Functions for handling the events:
Public Sub AutoCompleteCombo_KeyUp(ByVal cbo As ComboBox, ByVal e As KeyEventArgs)

Dim sTypedText As String

Dim iFoundIndex As Integer

Dim oFoundItem As Object

Dim sFoundText As String

Dim sAppendText As String

‘Allow select keys without Autocompleting
Select Case e.KeyCode

Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, Keys.Delete, Keys.Down

Return

End Select

‘Get the Typed Text and Find it in the list
sTypedText = cbo.Text
iFoundIndex = cbo.FindString(sTypedText)

‘If we found the Typed Text in the list then Autocomplete

If iFoundIndex >= 0 Then

‘Get the Item from the list (Return Type depends if Datasource was bound

‘ or List Created)
oFoundItem = cbo.Items(iFoundIndex)

‘Use the ListControl.GetItemText to resolve the Name in case the Combo

‘ was Data bound
sFoundText = cbo.GetItemText(oFoundItem)

‘Append then found text to the typed text to preserve case
sAppendText = sFoundText.Substring(sTypedText.Length)
cbo.Text = sTypedText & sAppendText

‘Select the Appended Text
cbo.SelectionStart = sTypedText.Length
cbo.SelectionLength = sAppendText.Length

End If
End Sub
Public Sub AutoCompleteCombo_Leave(ByVal cbo As ComboBox)

Dim iFoundIndex As Integer
iFoundIndex = cbo.FindStringExact(cbo.Text)
cbo.SelectedIndex = iFoundIndex
End Sub

Remote Connection Setup of SQL Server 2005 Express or SQL Server 2005 Database


Microsoft SQL Server 2005 or Microsoft SQL Server 2005 Express does not allow remote access by default. If you want to remote connect or use another computer in your LAN or network, you must configure on your SQL Server 2005 or the SQL Server 2005 Express.
If you don’t change the settings of the SQL server for remote connection, you may receive an error message.
Sqlcmd: Error: Microsoft SQL Native Client: An error has occurred while establishing a connection to the server. When connecting to SQL Server2005, this failure may be caused by the fact that under the defaultsettings SQL Server does not allow
Enable remote connections for SQL Server 2005 Express or SQL Server 2005 Developer Edition
To enable remote connections on the instance of SQL Server 2005 Express, please follow the steps below.
1. Locate your Microsoft SQL Server 2005 program on the Start menu on your computer, point toConfiguration Tools, and then you will see the menu item SQL Server Surface Area Configuration(please check image below)
2. Click SQL Server Surface Area Configuration, and then click Surface Area Configuration for Services and Connections.
3. In Database Engine menu, please select the Remote connections and by default the SQL Server 2005 connection type is Local Connections only, please select the Local and Remote connections and selectUsing both TCP/IP and Name Pipes. Based on my experience, It’s better to use both TCP/IP and Name Pipes as you can see the image below because during the configuration of your ODBC you can use these type TCP/IP or Name Pipes to connect the SQL Server 2005 database.
4. Please Click to confirm the database setting changes.
5. Return to the Surface Area Configuration for Services and Connections page, expand Database Engine, select Service, On the right side, click Stop button, then wait until the MSSQLSERVER (MSSQL$SQLEXPRESS2005) service stops, and then click Start button to restart the MSSQLSERVER (MSSQL$SQLEXPRESS2005) service.