Click here to Skip to main content
16,016,759 members
Articles / Programming Languages / Visual Basic
Article

Use ComboBox at runtime with your database application

Rate me:
Please Sign up or sign in to vote.
1.27/5 (7 votes)
28 Jul 20051 min read 41.2K   10   2
How to use the combobox at runtime, adding items, selecting default value, and returning selection.

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.

VB
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.

VB
#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.

VB
If IsDBNull(rsDataReader("HDH_STATUS")) = False Then
    loadStatusCodes(CInt(rsDataReader("HDH_STATUS")))

To read the selected property, the following code is used:

VB
Dim nSelectedStatus As MACTLS_CB_MCTFHDS
nSelectedCompany = CType(HDH_STATUS.SelectedItem, MACTLS_CB_MCTFHDS)
Dim nHDS_CDE As String = nSelectedStatus.HDS_CDE ' Status Code
Dim nHDS_DESCRIPTION As String = _
   nSelectedStatus.HDS_DESCRIPTION ' Status 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThanks Pin
Fernando Hitch22-Jun-07 14:54
Fernando Hitch22-Jun-07 14:54 
GeneralExcellent article Pin
navratankorma14-Oct-06 19:42
navratankorma14-Oct-06 19:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.