Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > .NET 3.5 and Visual Studio. 2008 > Visual Studio 2008
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Visual Studio 2008 For discussing Visual Studio 2008. Please post code questions about a specific language (C#, VB, ASP.NET, etc) in the correct language forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Studio 2008 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 20th, 2008, 02:17 AM
Registered User
Join Date: Jun 2008
Location: Kuala Lumpur, Selangor, Malaysia.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Gini
Default Adding data in a combo box from the database

I have a few questions regarding the code below.

1) In the [u]first</u> highlighted section of the code below the employee names are added into the combo box using a large string array.

    ' Declare comboBox1 as a ComboBox.    

Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox

    ' This method initializes the combo box, adding a large string 
    ' array but limiting the drop-down size to six rows so the combo box
    ' doesn't cover other controls when it expands.

    Private Sub InitializeComboBox()
        Me.ComboBox1 = New System.Windows.Forms.ComboBox

        Dim employees() As String = New String() {"Hamilton, David", _
            "Hensien, Kari", "Hammond, Maria", "Harris, Keith", _
            "Henshaw, Jeff D.", "Hanson, Mark", "Harnpadoungsataya, Sariya", _
            "Harrington, Mark", "Harris, Keith", "Hartwig, Doris", _
            "Harui, Roger", "Hassall, Mark", "Hasselberg, Jonas", _
            "Harnpadoungsataya, Sariya", "Henshaw, Jeff D.", "Henshaw, Jeff D.", _
            "Hensien, Kari", "Harris, Keith", "Henshaw, Jeff D.", _
            "Hensien, Kari", "Hasselberg, Jonas", "Harrington, Mark", _
            "Hedlund, Magnus", "Hay, Jeff", "Heidepriem, Brandon D."}


        Me.ComboBox1.Location = New System.Drawing.Point(136, 32)
        Me.ComboBox1.MaxDropDownItems = 5
        Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDown
        Me.ComboBox1.Name = "ComboBox1"
        Me.ComboBox1.Size = New System.Drawing.Size(136, 81)
        Me.ComboBox1.TabIndex = 0
    End Sub

    ' This method is called when the user changes his or her selection.
    ' It searches for all occurrences of the selected employee's
    ' name in the Items array and adds the employee's name and 
    ' the number of occurrences to TextBox1.Text.

    ' CAUTION   This code exposes a known bug: If the index passed to the 
    ' FindStringExact(searchString, index) method is the last index 
    ' of the array, the code throws an exception    .

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        Dim comboBox As comboBox = CType(sender, comboBox)

        ' Save the selected employee's name, because we will remove
        ' the employee's name from the list.

        Dim selectedEmployee = CType(ComboBox1.SelectedItem, String)

        Dim count As Integer = 0
        Dim resultIndex As Integer = -1

        ' Call the FindStringExact method to find the first 
        ' occurrence in the list.

        resultIndex = ComboBox1.FindStringExact (ComboBox1.SelectedItem)

        ' Remove the name as it is found, and increment the found count. 
        ' Then call the FindStringExact method again, passing in the index of the
        ' current found item so the search starts there instead of 
        ' at the beginning of the list.

        While (resultIndex <> -1)
            count += 1
            resultIndex = ComboBox1.FindStringExact _
            (selectedEmployee, resultIndex)
        End While

        ' Update the text in Textbox1.
        TextBox1.Text = TextBox1.Text & Microsoft.VisualBasic.vbCrLf _
            & selectedEmployee & ": " & count

End Sub

End Class
How do I do the same for datas that are in the database(SQL Server 2000) instead? Do I have to use the below line of code? Or if not, what do I have to do to insert the data into the combo box?

Me.Inv_itemcategoryTableAdapter.Fill(Me.dsInventor y.inv_itemcategory)

Inv_itemcategory is the name of the table adapter holding the data. dsInventory is the name of the data source.

2) In the [u]second</u> highlighted section (updating the textbox), can I use the same method to update another combo box and also a text box instead of only updating a text box?

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding value pairs to a combo box tebert2458 C# 2 February 25th, 2009 06:10 AM
Data type mismatch -- Combo box bound to database jamenijamjam Beginning VB 6 2 September 1st, 2006 08:36 PM
adding my own combo box in datagridview drachx Pro Visual Basic 2005 0 January 27th, 2006 03:04 AM
Combo Box Populated off Database Firehawk2006 Visual Basic 2005 Basics 1 November 28th, 2005 01:47 PM
Combo Box - Data In Use MrDannyP Beginning VB 6 1 November 15th, 2003 10:37 AM

All times are GMT -4. The time now is 06:40 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.