Click here to Skip to main content
Licence 
First Posted 28 Jul 2005
Views 33,119
Bookmarked 9 times

Use ComboBox at runtime with your database application

By | 28 Jul 2005 | Article
How to use the combobox at runtime, adding items, selecting default value, and returning selection.
 
Part of The SQL Zone sponsored by
See Also

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 ' 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

About the Author

Execusoft

Web Developer

South Africa South Africa

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanks PinmemberFernando Hitch14:54 22 Jun '07  
GeneralExcellent article Pinmembernavratankorma19:42 14 Oct '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 28 Jul 2005
Article Copyright 2005 by Execusoft
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid