Hi
SAWS
i made a Combobox component which i called as "ctlSpecialComboBox" which can do following things
1) Can have two columns
2) can switch the column orders
3) Can do extended searching
so below is the code for this
all you have to do is to add a NEW COMPONENENT From "ADD COMPONENT" and then paste the following code
named the control to "CtlSpecialCombobox" and you are done you can then paste this component on any form or container
Regards
Nouman
Imports System.Data.OleDb
Public Class ctlSpecialComboBox
Inherits System.Windows.Forms.ComboBox
Private Const System_Name As String = "MOAD_USVI"
Protected WithEvents Cmenu As New ContextMenu()
Protected WithEvents MenuItem2 As New MenuItem()
Protected WithEvents MenuItem1 As New MenuItem()
Private blnFirstTime As Boolean
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'ctlSpecialComboBox
'
Me.Name = "ctlSpecialComboBox"
End Sub
#End Region
Private objDataTable As DataTable
Private objClientDT As DataTable
Private strDisplayValue As String
Private strSelectedIndex As String
Private strColumnFieldLables As String
Dim arrColumnLabels As Array
Private strColumnFieldDBName As String
Private strDefualtFieldName As String
Dim arrDisplayField As Array
Public Property SetDataTable() As DataTable
Get
Return objDataTable
End Get
Set(ByVal Value As DataTable)
objDataTable = Value
If Not Value Is Nothing Then
Me.DisplayMember = "Text"
Me.ValueMember = "Value"
GenerateContextMenu(strColumnFieldLables)
Me.DataSource = getClientDataTable()
End If
End Set
End Property
Public Property DisplayValue() As String
Get
Return (strDisplayValue)
End Get
Set(ByVal Value As String)
strDisplayValue = Value
End Set
End Property
Public Property ColumnFieldLabels() As String
Get
Return strColumnFieldLables
End Get
Set(ByVal Value As String)
strColumnFieldLables = Value
End Set
End Property
Public Property ColumnFieldDBName() As String
Get
Return strColumnFieldDBName
End Get
Set(ByVal Value As String)
strColumnFieldDBName = Value
End Set
End Property
Public Property DefualtFieldName() As String
Get
Return strDefualtFieldName
End Get
Set(ByVal Value As String)
strDefualtFieldName = Value
End Set
End Property
Private Function getRow(ByVal strColumnName As String, ByVal strColumnValue As String, ByVal dtBindedDataSet As DataTable) As Object
Try
Dim findRow() As DataRow
Dim strSql As String
strSql = strColumnName & "='" & Trim(strColumnValue) & "'"
findRow = dtBindedDataSet.Select(strSql)
If findRow.Length = 0 Then
getRow = Nothing
Else
getRow = findRow
End If
Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Information, strSystemTitle)
End Try
End Function
Private Function getArrayDisplayFields() As Array
Try
'arrDisplayField = Split(strDisplayFields, ",", -1, CompareMethod.Text)
'Return arrDisplayField
arrDisplayField = Split(ColumnFieldDBName, ",", -1, CompareMethod.Text)
Return arrDisplayField
Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Information, strSystemTitle)
End Try
End Function
Private Function getClientDataTable() As DataTable
Try
getArrayDisplayFields()
'create clientside datatable
objClientDT = New DataTable("ClientTable"

'Create datacolumn
Dim objClientValue As DataColumn = New DataColumn("Value", System.Type.GetType("System.String"

)
objClientDT.Columns.Add(objClientValue)
Dim objColText As DataColumn = New DataColumn("Text", System.Type.GetType("System.String"

)
objClientDT.Columns.Add(objColText)
Dim objClientDR As DataRow
Dim Row As DataRow
For Each Row In objDataTable.Rows
objClientDR = objClientDT.NewRow
objClientDR("Value"

= Convert.ToString(Row(strDisplayValue))
Dim i As Short
Dim strText As String = ""
For i = 0 To arrDisplayField.GetUpperBound(0)
strText += Trim(Convert.ToString(Row(arrDisplayField(i)))) + ", "
Next
strText = strText.Substring(0, (Len(strText) - 2))
'if comma present at first or last position
''If strText.Substring(0, 1) = "," Then
'' Mid(strText, 1, 1) = " "
''End If
If strText.Substring(Len(Trim(strText)) - 1, 1) = "," Then
Mid(strText, Len(Trim(strText)), 1) = " "
End If
'strText = Replace(strText, ",", "", Len(Trim(strText)) - 1, , CompareMethod.Text)
objClientDR("Text"

= strText
objClientDT.Rows.Add(objClientDR)
Next
Return objClientDT
Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Information, strSystemTitle)
End Try
End Function
Private Sub GenerateContextMenu(ByVal dispColumn As String, Optional ByVal check As Boolean = False)
Try
'Generate Context menu Dynamically
'Cmenu = Nothing
Cmenu.MenuItems.Clear()
'checking the SysData Table for the specific DefualtFieldName
If Not DefualtFieldName Is Nothing Then
Dim objData As New MMDAC()
Dim objTable As DataTable
Dim myParam As New ArrayList()
Dim myStruct As sprocParam
Dim intCol As Integer
myParam.Clear()
myStruct.paramName = "@Param"
myStruct.paramType = OleDbType.VarChar
myStruct.paramSize = 1000
myStruct.paramValue = "SYSC_System='" & System_Name & "'"
myParam.Add(myStruct)
objTable = objData.ExecuteSelectStoredProc("usp_SysData_Select", myParam)
If Not objTable Is Nothing Then
If objTable.Rows.Count > 0 Then
For intCol = 0 To objTable.Columns.Count - 1
If DefualtFieldName.ToString().Equals(objTable.Columns(intCol).ColumnName) Then
'1 is for desc
'2 is for code
If objTable.Rows(0)(intCol) = 2 Then
Dim TempColumnFielLabels As String
Dim TemparrColumnLables As Array
TemparrColumnLables = Split(ColumnFieldLabels, ",", -1, CompareMethod.Text)
TempColumnFielLabels = TemparrColumnLables(1) & "," & TemparrColumnLables(0)
dispColumn = TempColumnFielLabels
getArrayDisplayFields()
ColumnFieldDBName = arrDisplayField(1) & "," & arrDisplayField(0)
getArrayDisplayFields()
End If
Exit For
End If
Next
End If
End If
End If
arrColumnLabels = Split(ColumnFieldLabels, ",", -1, CompareMethod.Text)
If UBound(arrColumnLabels) > 0 Then
If arrColumnLabels(1) <> "" Then
MenuItem1 = New MenuItem()
MenuItem1.Text = arrColumnLabels(0) & "," & arrColumnLabels(1)
Cmenu.MenuItems.Add(MenuItem1)
MenuItem2 = New MenuItem()
MenuItem2.Text = arrColumnLabels(1) & "," & arrColumnLabels(0)
Cmenu.MenuItems.Add(MenuItem2)
DisableMenuItem(dispColumn)
End If
Me.ContextMenu = Cmenu
End If
Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Information, strSystemTitle)
End Try
End Sub
Private Sub DisableMenuItem(ByVal dispColumn As String)
Try
If MenuItem1.Text = dispColumn Then
MenuItem1.Enabled = False
MenuItem2.Enabled = True
ElseIf MenuItem2.Text = dispColumn Then
MenuItem1.Enabled = True
MenuItem2.Enabled = False
End If
Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Information, strSystemTitle)
End Try
End Sub
Private Sub ctlSpecialComboBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
''If e.Button = MouseButtons.Right Then
'' GenerateContextMenu(DisplayFields)
''End If
''If e.Button = MouseButtons.Left Then
'' Me.SelectAll()
''End If
End Sub
Private Sub MenuItem2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
Try
'code , desc
Dim strColumnLabel As String
'DisplayFields = arrDisplayField(1) & "," & arrDisplayField(0)
ColumnFieldDBName = arrDisplayField(1) & "," & arrDisplayField(0)
strColumnLabel = arrColumnLabels(1) & "," & arrColumnLabels(0)
Me.DisplayMember = "Text"
Me.ValueMember = "Value"
If Not Me.SelectedValue Is Nothing Then
strSelectedIndex = Me.SelectedValue
Me.DataSource = getClientDataTable()
Me.SelectedValue = strSelectedIndex
DisableMenuItem(strColumnLabel)
End If
Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Information, strSystemTitle)
End Try
End Sub
Private Sub MenuItem1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
Try
'desc , code
Dim strColumnLabel As String
'DisplayFields = arrDisplayField(0) & "," & arrDisplayField(1)
ColumnFieldDBName = arrDisplayField(1) & "," & arrDisplayField(0)
strColumnLabel = arrColumnLabels(0) & "," & arrColumnLabels(1)
Me.DisplayMember = "Text"
Me.ValueMember = "Value"
If Not Me.SelectedValue Is Nothing Then
strSelectedIndex = Me.SelectedValue
Me.DataSource = getClientDataTable()
Me.SelectedValue = strSelectedIndex
DisableMenuItem(strColumnLabel)
End If
Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Information, strSystemTitle)
End Try
End Sub
Public Function getDisplayFieldNameFromValue(ByVal strValue As String) As String
Try
Dim objReturnRow As Object
objReturnRow = getRow("Value", strValue, objClientDT)
If objReturnRow Is Nothing Then
getDisplayFieldNameFromValue = ""
Else
getDisplayFieldNameFromValue = (IIf(IsDBNull(objReturnRow(0)("Text"

), "", objReturnRow(0)("Text"

))
End If
Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Information, strSystemTitle)
End Try
End Function
Public Function getDisplayFieldValue(ByVal strFieldName As String) As String
Try
Dim objReturnRow As Object
objReturnRow = getRow(strDisplayValue, Me.SelectedValue, objDataTable)
If objReturnRow Is Nothing Then
getDisplayFieldValue = ""
Else
If IsDBNull(objReturnRow(0)(strFieldName)) Then
getDisplayFieldValue = ""
Else
getDisplayFieldValue = Trim(objReturnRow(0)(strFieldName))
End If
End If
Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Information, strSystemTitle)
End Try
End Function
Public Sub setSelectedIndexByFieldText(ByVal strFieldName As String, ByVal strFieldValue As String)
Try
Dim objReturnRow As Object
objReturnRow = getRow(strFieldName, strFieldValue, objDataTable)
If Not objReturnRow Is Nothing Then
Me.SelectedValue = IIf(IsDBNull(objReturnRow(0)(DisplayValue)), "", objReturnRow(0)(DisplayValue))
End If
Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Information, strSystemTitle)
End Try
End Sub
Public Sub AutoCompleteCombo_KeyUp(ByVal cbo As ComboBox, ByVal e As KeyEventArgs)
Try
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, Keys.Home, Keys.End
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
Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Information, strSystemTitle)
End Try
End Sub
Public Sub AutoCompleteCombo_Leave(ByVal cbo As ComboBox)
Try
Dim iFoundIndex As Integer
iFoundIndex = cbo.FindStringExact(cbo.Text)
cbo.SelectedIndex = iFoundIndex
Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Information, strSystemTitle)
End Try
End Sub
Private Sub ctlSpecialComboBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Leave
Try
If Me.DropDownStyle = ComboBoxStyle.DropDown Then
AutoCompleteCombo_Leave(Me)
End If
Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Information, strSystemTitle)
End Try
End Sub
Private Sub ctlSpecialComboBox_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
Try
Dim objContainer As Object
If e.Control Or e.Alt Then
If UCase(Me.FindForm.Name) = "_7501SCREEN" Then
objContainer = Me.FindForm
objContainer.FormKeyDown(e)
e.Handled = True
End If
Else
If Me.DropDownStyle = ComboBoxStyle.DropDown Then
AutoCompleteCombo_KeyUp(Me, e)
e.Handled = True
End If
End If
Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Information, strSystemTitle)
End Try
End Sub
End Class
Nouman Zaheer
Software Engineer
MSR