Introduction
Another way of manipulating your combobox at runtime. I designed this as an alternate way of manipulating the combobox control for adding data, selecting the default value, and reading the value the user selected at runtime. This solution also provides a lot of functionality as you can store a couple of columns of data in your combobox item object at runtime.
Background
I did this programming for the combobox as I did not want to bind controls to datasets at runtime and came up with this solution, and it always worked well in my environment.
Using the code
The first step is to create a class to hold your combobox items. By adding more properties, the class can easily be modified to hold more information for your combobox.
Public Class MACTLS_CB_MCTFHDS
Private _HDS_CDE As Integer
Private _HDS_DESCRIPTION As String
Public Sub New(ByVal HDS_CDE As Integer, ByVal HDS_DESCRIPTION As String)
_HDS_CDE = HDS_CDE
_HDS_DESCRIPTION = HDS_DESCRIPTION
End Sub
Public ReadOnly Property HDS_CDE() As String
Get
Return _HDS_CDE
End Get
End Property
Public ReadOnly Property HDS_DESCRIPTION() As String
Get
Return _HDS_DESCRIPTION
End Get
End Property
Public Overrides Function ToString() As String
Return _HDS_DESCRIPTION
End Function
End Class
The second step is to create a subroutine in your form to load the combobox items and select the default value in your combobox object. The combobox is filled with data from the database and looks for the value passed. If a match is found it sets the SelectedIndex
property of the ComboBox
, otherwise the first item is selected by default.
#Region "Load the Status Codes."
Private Sub loadStatusCodes(ByVal nStatusCode As Integer)
Try
Dim nSQL As String = ""
nSQL = "Select HDS_CDE, HDS_DESCRIPTION from MCTFHDS"
Dim connstr As String = nSQLHelpConnectionString
Dim cnn As New SqlConnection(connstr)
Dim da As SqlCommand = New SqlCommand(nSQL, cnn)
da.CommandType = CommandType.Text
Dim rsDataReader As SqlDataReader
cnn.Open()
rsDataReader = da.ExecuteReader
Dim nItemCount As Integer = 0
Dim nItemMatch As Integer = 0
If rsDataReader.HasRows Then
Do While rsDataReader.Read
Dim nHDS_CDE As Integer = 0
Dim nHDS_DESCRIPTION As String = ""
If IsDBNull(rsDataReader("HDS_CDE")) = False Then
nHDS_CDE = Int(rsDataReader("HDS_CDE"))
If IsDBNull(rsDataReader("HDS_DESCRIPTION")) = False Then
nHDS_DESCRIPTION = Trim(CStr(rsDataReader("HDS_DESCRIPTION")))
HDH_STATUS.Items.Add(New MACTLS_CB_MCTFHDS(nHDS_CDE, nHDS_DESCRIPTION))
If nStatusCode = nHDS_CDE Then nItemMatch = nItemCount
nItemCount += 1
Loop
HDH_STATUS.SelectedIndex = nItemMatch
End If
Catch ex As SqlException
MsgBox(ex.ToString)
End Try
End Sub
#End Region
The previous subroutine can be called with something similar to the following code. This will fill the combobox and select the default value passed from your data.
If IsDBNull(rsDataReader("HDH_STATUS")) = False Then
loadStatusCodes(CInt(rsDataReader("HDH_STATUS")))
To read the selected property, the following code is used:
Dim nSelectedStatus As MACTLS_CB_MCTFHDS
nSelectedCompany = CType(HDH_STATUS.SelectedItem, MACTLS_CB_MCTFHDS)
Dim nHDS_CDE As String = nSelectedStatus.HDS_CDE
Dim nHDS_DESCRIPTION As String = _
nSelectedStatus.HDS_DESCRIPTION
Well, this is my solution to the problem of managing the combobox at runtime and hope someone will benefit from this code.
History
- Created on 2005/07/27 - Version 1.