Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with setting a selected Item in a combobox

Status
Not open for further replies.

jpack23

Programmer
Dec 15, 2004
82
0
0
US
I have a combobox with about 100 list items. Since it takes time to find stuff in a list of 100 items I have create a few buttons that I would like to automatically selected the proper item in the list box.

For examble Product A might be #50 in the list of items in the drop down combo box, but since its a popular item, i would like to create a button(btnProductA) that would make product A the selected item in the combobox when clicked

btnProductA_Click
set cboProduct.selectedItem = btnProductA Item

does this make sense?

cboProduct is populated with several fields in a database table so there are several properties associated with product A

when the user clicks that button I want cboProduct to contain every product specific thing it would have had if user selected it from the list

thank you very much for your help

joe
 

That should get you started: one Label, one Combo box, and one button:
Code:
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load

        For i As Integer = 1 To 100
            ComboBox1.Items.Add("Item Number " & i)
        Next

    End Sub
[blue]
    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        ComboBox1.SelectedIndex = 49
    End Sub[/blue]

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

        Label1.Text = "Selected item - " & ComboBox1.Text
    End Sub
End Class

Have fun.

---- Andy
 
Thanks, that works BUT, what if I dont know what the index will be? The combobox is populated from a table that could be update and therefor change the index. The one field I will know is an ID number. How do I try to match the ids with the index?

For example

BtnProductA.tag = 50 (thats the id number for that product)

set combobox.selectedIndex = whatever index number is for ID number of 50


thanks for you time
 
Well, you're going to have to define and store the "popularity" somewhere. So why not create a table in your database? Let's say you have three popular items"
Code:
PopularItems
-------------
ItemID  [Rank]
777     .01
456     .02
212     .03

Your source query could then look like the following:
Code:
SELECT a.*
FROM Items a
LEFT OUTER JOIN PopularItems b
  ON a.ItemID = b.ItemID
ORDER BY ISNULL(b.[Rank], a.ItemID)

That query would return Items in this order: 777, 456, 212, and then everything else ordered by ItemID.
 
Solved my problem thanks for you help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top