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.
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
''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
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.
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="STC_Desc,STC_Code" 'Set it to the fields you want to show
ColumnFieldDLabels= "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 = "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, ""
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, ""
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
''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, ""
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, ",", -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, ""
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, ""
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, ""
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, ""
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, ""
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, ""
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, ""
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, ""
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, ""
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, ""
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, ""
End Try
End Sub
End Class
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.
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 " Windows Form Designer generated code "
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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.