Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combobox

Status
Not open for further replies.

SAWS

Programmer
Nov 24, 2003
6
BE
Hello,

I'am searching for a combobox with different columns. I can not do it with the standard because I really want to see the columns, headers etc. Is there anyone who knows about a component or way to do this.

Thanks. BC.
 
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 = &quot;ctlSpecialComboBox&quot;

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 = &quot;Text&quot;
Me.ValueMember = &quot;Value&quot;
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 & &quot;='&quot; & Trim(strColumnValue) & &quot;'&quot;
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, &quot;,&quot;, -1, CompareMethod.Text)
'Return arrDisplayField
arrDisplayField = Split(ColumnFieldDBName, &quot;,&quot;, -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(&quot;ClientTable&quot;)
'Create datacolumn
Dim objClientValue As DataColumn = New DataColumn(&quot;Value&quot;, System.Type.GetType(&quot;System.String&quot;))
objClientDT.Columns.Add(objClientValue)
Dim objColText As DataColumn = New DataColumn(&quot;Text&quot;, System.Type.GetType(&quot;System.String&quot;))
objClientDT.Columns.Add(objColText)
Dim objClientDR As DataRow
Dim Row As DataRow
For Each Row In objDataTable.Rows
objClientDR = objClientDT.NewRow
objClientDR(&quot;Value&quot;) = Convert.ToString(Row(strDisplayValue))
Dim i As Short
Dim strText As String = &quot;&quot;
For i = 0 To arrDisplayField.GetUpperBound(0)
strText += Trim(Convert.ToString(Row(arrDisplayField(i)))) + &quot;, &quot;
Next

strText = strText.Substring(0, (Len(strText) - 2))

'if comma present at first or last position

''If strText.Substring(0, 1) = &quot;,&quot; Then
'' Mid(strText, 1, 1) = &quot; &quot;
''End If
If strText.Substring(Len(Trim(strText)) - 1, 1) = &quot;,&quot; Then
Mid(strText, Len(Trim(strText)), 1) = &quot; &quot;
End If

'strText = Replace(strText, &quot;,&quot;, &quot;&quot;, Len(Trim(strText)) - 1, , CompareMethod.Text)
objClientDR(&quot;Text&quot;) = 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 = &quot;@Param&quot;
myStruct.paramType = OleDbType.VarChar
myStruct.paramSize = 1000
myStruct.paramValue = &quot;SYSC_System='&quot; & System_Name & &quot;'&quot;
myParam.Add(myStruct)
objTable = objData.ExecuteSelectStoredProc(&quot;usp_SysData_Select&quot;, 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, &quot;,&quot;, -1, CompareMethod.Text)
TempColumnFielLabels = TemparrColumnLables(1) & &quot;,&quot; & TemparrColumnLables(0)
dispColumn = TempColumnFielLabels
getArrayDisplayFields()
ColumnFieldDBName = arrDisplayField(1) & &quot;,&quot; & arrDisplayField(0)
getArrayDisplayFields()
End If
Exit For
End If
Next
End If
End If
End If

arrColumnLabels = Split(ColumnFieldLabels, &quot;,&quot;, -1, CompareMethod.Text)
If UBound(arrColumnLabels) > 0 Then
If arrColumnLabels(1) <> &quot;&quot; Then
MenuItem1 = New MenuItem()
MenuItem1.Text = arrColumnLabels(0) & &quot;,&quot; & arrColumnLabels(1)
Cmenu.MenuItems.Add(MenuItem1)
MenuItem2 = New MenuItem()
MenuItem2.Text = arrColumnLabels(1) & &quot;,&quot; & 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) & &quot;,&quot; & arrDisplayField(0)
ColumnFieldDBName = arrDisplayField(1) & &quot;,&quot; & arrDisplayField(0)
strColumnLabel = arrColumnLabels(1) & &quot;,&quot; & arrColumnLabels(0)

Me.DisplayMember = &quot;Text&quot;
Me.ValueMember = &quot;Value&quot;
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) & &quot;,&quot; & arrDisplayField(1)
ColumnFieldDBName = arrDisplayField(1) & &quot;,&quot; & arrDisplayField(0)
strColumnLabel = arrColumnLabels(0) & &quot;,&quot; & arrColumnLabels(1)

Me.DisplayMember = &quot;Text&quot;
Me.ValueMember = &quot;Value&quot;
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(&quot;Value&quot;, strValue, objClientDT)
If objReturnRow Is Nothing Then
getDisplayFieldNameFromValue = &quot;&quot;
Else
getDisplayFieldNameFromValue = (IIf(IsDBNull(objReturnRow(0)(&quot;Text&quot;)), &quot;&quot;, objReturnRow(0)(&quot;Text&quot;)))
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 = &quot;&quot;
Else
If IsDBNull(objReturnRow(0)(strFieldName)) Then
getDisplayFieldValue = &quot;&quot;
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)), &quot;&quot;, 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) = &quot;_7501SCREEN&quot; 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
 
Hello Mr. Zaheer,

In my project I select 'add new component' and I did paste the code. I only get an error on MMDAC and sprocparam. When this is solved, is it so that I have to drag the component from the solution explorer. Sorry, I'am quiet new to vb.net.

Thank you very much.
 
I am sorry SWS,I had some functionality which you wont require so copy the following code in ur component make sure you just copy that thing with all of the code is seleceted in ur component
Now when you build it then you can add that component in ur
Project
all you have to do is set
1) In properties of combobox you will see two new propeties
called
ColumnFieldDBName=&quot;STC_Desc,STC_Code&quot; 'Set it to the fields you want to show
ColumnFieldDLabels= &quot;State Description, State Code
2) In your code inseted of setting DataSource use the propety
Comobobox.SetDataTable='your table'

Hope it will help you out
Regards
Nouman

Imports System.Data.OleDb
Public Class ctlSpecialComboBox
Inherits System.Windows.Forms.ComboBox
Private Const System_Name As String = &quot;MOAD_USVI&quot;
Protected WithEvents Cmenu As New ContextMenu()
Protected WithEvents MenuItem2 As New MenuItem()
Protected WithEvents MenuItem1 As New MenuItem()


Private blnFirstTime As Boolean
#Region &quot; Windows Form Designer generated code &quot;

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 = &quot;ctlSpecialComboBox&quot;

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 = &quot;Text&quot;
Me.ValueMember = &quot;Value&quot;
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 & &quot;='&quot; & Trim(strColumnValue) & &quot;'&quot;
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, &quot;&quot;)
End Try

End Function
Private Function getArrayDisplayFields() As Array
Try
'arrDisplayField = Split(strDisplayFields, &quot;,&quot;, -1, CompareMethod.Text)
'Return arrDisplayField
arrDisplayField = Split(ColumnFieldDBName, &quot;,&quot;, -1, CompareMethod.Text)
Return arrDisplayField
Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Information, &quot;&quot;)
End Try
End Function
Private Function getClientDataTable() As DataTable
Try
getArrayDisplayFields()
'create clientside datatable
objClientDT = New DataTable(&quot;ClientTable&quot;)
'Create datacolumn
Dim objClientValue As DataColumn = New DataColumn(&quot;Value&quot;, System.Type.GetType(&quot;System.String&quot;))
objClientDT.Columns.Add(objClientValue)
Dim objColText As DataColumn = New DataColumn(&quot;Text&quot;, System.Type.GetType(&quot;System.String&quot;))
objClientDT.Columns.Add(objColText)
Dim objClientDR As DataRow
Dim Row As DataRow
For Each Row In objDataTable.Rows
objClientDR = objClientDT.NewRow
objClientDR(&quot;Value&quot;) = Convert.ToString(Row(strDisplayValue))
Dim i As Short
Dim strText As String = &quot;&quot;
For i = 0 To arrDisplayField.GetUpperBound(0)
strText += Trim(Convert.ToString(Row(arrDisplayField(i)))) + &quot;, &quot;
Next

strText = strText.Substring(0, (Len(strText) - 2))

'if comma present at first or last position

''If strText.Substring(0, 1) = &quot;,&quot; Then
'' Mid(strText, 1, 1) = &quot; &quot;
''End If
If strText.Substring(Len(Trim(strText)) - 1, 1) = &quot;,&quot; Then
Mid(strText, Len(Trim(strText)), 1) = &quot; &quot;
End If

'strText = Replace(strText, &quot;,&quot;, &quot;&quot;, Len(Trim(strText)) - 1, , CompareMethod.Text)
objClientDR(&quot;Text&quot;) = strText
objClientDT.Rows.Add(objClientDR)
Next
Return objClientDT
Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Information, &quot;&quot;)
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()


arrColumnLabels = Split(ColumnFieldLabels, &quot;,&quot;, -1, CompareMethod.Text)
If UBound(arrColumnLabels) > 0 Then
If arrColumnLabels(1) <> &quot;&quot; Then
MenuItem1 = New MenuItem()
MenuItem1.Text = arrColumnLabels(0) & &quot;,&quot; & arrColumnLabels(1)
Cmenu.MenuItems.Add(MenuItem1)
MenuItem2 = New MenuItem()
MenuItem2.Text = arrColumnLabels(1) & &quot;,&quot; & arrColumnLabels(0)
Cmenu.MenuItems.Add(MenuItem2)
DisableMenuItem(dispColumn)

End If

Me.ContextMenu = Cmenu
End If

Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Information, &quot;&quot;)
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, &quot;&quot;)
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) & &quot;,&quot; & arrDisplayField(0)
ColumnFieldDBName = arrDisplayField(1) & &quot;,&quot; & arrDisplayField(0)
strColumnLabel = arrColumnLabels(1) & &quot;,&quot; & arrColumnLabels(0)

Me.DisplayMember = &quot;Text&quot;
Me.ValueMember = &quot;Value&quot;
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, &quot;&quot;)
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) & &quot;,&quot; & arrDisplayField(1)
ColumnFieldDBName = arrDisplayField(1) & &quot;,&quot; & arrDisplayField(0)
strColumnLabel = arrColumnLabels(0) & &quot;,&quot; & arrColumnLabels(1)

Me.DisplayMember = &quot;Text&quot;
Me.ValueMember = &quot;Value&quot;
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, &quot;&quot;)
End Try
End Sub
Public Function getDisplayFieldNameFromValue(ByVal strValue As String) As String
Try
Dim objReturnRow As Object
objReturnRow = getRow(&quot;Value&quot;, strValue, objClientDT)
If objReturnRow Is Nothing Then
getDisplayFieldNameFromValue = &quot;&quot;
Else
getDisplayFieldNameFromValue = (IIf(IsDBNull(objReturnRow(0)(&quot;Text&quot;)), &quot;&quot;, objReturnRow(0)(&quot;Text&quot;)))
End If
Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Information, &quot;&quot;)
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 = &quot;&quot;
Else
If IsDBNull(objReturnRow(0)(strFieldName)) Then
getDisplayFieldValue = &quot;&quot;
Else
getDisplayFieldValue = Trim(objReturnRow(0)(strFieldName))
End If

End If
Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Information, &quot;&quot;)
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)), &quot;&quot;, objReturnRow(0)(DisplayValue))
End If
Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Information, &quot;&quot;)
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, &quot;&quot;)
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, &quot;&quot;)
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, &quot;&quot;)
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) = &quot;_7501SCREEN&quot; 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, &quot;&quot;)
End Try
End Sub
End Class





Nouman Zaheer
Software Engineer
MSR
 
Nomi2000,

In my project I'am currently working in, I selected Add Component and then component class. In the source view I copied your code (the last one) and I saved everything. On my project name I said rebuild, and I did not get any errors, but I don't know what I should do now. Is it on my default combobox from .Net or am I messing up everything, and didn't I use the right add component.

Sorry for this stupid questions, but I can't get out of it.

Thanks.
 
Hi SAWS
thats the one thing which i don't like in VB .net in the version i have which is .net framework 1.0 is when you made your component then it will not come automatically in your toolbar as it should be as its now a component..although you can make another component libraary for holding all the component and then add refreence to that library to any project when its needed but let me give you the solution which you are looking
1) after you compile the project with you component then
paste an ordinay combobox to you form and then go the source of that form and Open that code region which .net write by itself means #Region &quot; Windows Form Designer generated code &quot;
replace
System.Windows.Forms.ComboBox with WindowsApplication8.ctlSpecialComboBox 'where windowsApplication8 is the name of your project if its differnt then change this to your name
And when you did it and go back to design view your combobx will be a SPECIAL combobox with all the MISC properties in the PROPERTIES window of that combobbox

ALTHOUGH THIS STEP Should not be done as it should be in toolbox but my .net not do this for me so first time you have to do like this once you have that combobox on form you can copy and paste from that form to any other form in your project

Regards

Nouman Zaheer
Software Engineer
MSR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top