Link to home
Start Free TrialLog in
Avatar of sasllc
sasllcFlag for United States of America

asked on

How to load a ComboBox with string values?

I got some code from a co-worker some time back that allowed me to populate a ComboBox from an SQL table, but now I need to populate it with a set of string variables instead--not using a table at all.  The problem is that I don't understand how to modify this code to do what I need.

I have a ComboBox named txtItem, and I'd like to populate it with three strings named psItemCode1, psItemCode2, and psItemCode3, so I need to understand how to modify the code he gave me.  Right now this is what I have to populate it from an SQL table:

        'Populating the combobox ----------
        Dim ItemTable As Data.DataTable
        ItemTable = GetItems()
       
        If ItemTable.Rows.Count > 0 Then
            Me.txtItem.DataSource = ItemTable
            Me.txtItem.DisplayMember = ItemTable.Columns("Item").ToString
            Me.txtItem.ValueMember = ItemTable.Columns("Item").ToString
            Me.txtItem.Enabled = True
        End If

And the code for GetItems() is:

    Public Function GetItems(Optional ByVal DataRead As String = Nothing) As DataTable
        Dim Sqlstr As String
        Sqlstr = " SELECT Item From Sysitem Order By Item "

        Return RunSQLDataTable(Sqlstr, CommandType.Text)

    End Function

So what do I need to add or remove from this existing code in order to simply populate the ComboBox wtih my three strings?  TIA
SOLUTION
Avatar of gplana
gplana
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sasllc

ASKER

For reasons I don't yet understand, it does not allow me to create statements like this.  As I key in "Me.txtItem.", I see that "AddItem" is not showing as an option.  If I key it in anyway, the error says "AddItem" is not a member of 'System.Windows.Forms.ComboBox'.

Maybe it's because this app is for a mobile device, so I'm working within the constraints of the compact framework.  Whatever the reason...how else might I add elements?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, I think it's Add instead of AddItem, sorry for my mistake.

This method an item to combobox and the value added is what you put on its paramether.

Regards.
Avatar of sasllc

ASKER

OK, that syntax went in fine.  

So now I assume I would delete that 'GetItems' function and the statement "ItemTable = GetItems()" that runs it, since I'm no longer bringing in my row data from a table.

But what about all this:

        If ItemTable.Rows.Count > 0 Then
            Me.txtItem.DataSource = ItemTable
            Me.txtItem.DisplayMember = ItemTable.Columns("Item").ToString
            Me.txtItem.ValueMember = ItemTable.Columns("Item").ToString
            Me.txtItem.Enabled = True
        End If

Do I still need that code?  I just don't understand what it's doing, or what it's there for, so I'm hoping someone can clarify.
Right...you don't need that stuff anymore, except maybe for the line that sets Enabled to True.  Basically what that code did was set the columns from the DataTable that ComboBox would display and return when an item is selected.
Avatar of sasllc

ASKER

So now for fine tuning...and tell me if I need to start a new question:

1) I the code, I load three rows into the ComboBox, and then I focus on the ComboBox, but nothing shows in that field on the form until you scroll down.  When I scroll down, I do see the rows I loaded.  But is there a way to make the value of the first row show in the ComboBox when I first run the form, rather than a blank, empty field?

2) For the ComboBox I have set the background color to Yellow, and I can indeed see a bit of yellow on the left edge of the field when I run the app--something I've never seen before.  But the ComboBox itself is plain white on the form.  Any idea what I might be doing wrong here?
Yes, you can use this code:
me.txtItem.SelectedItem = 0
to select first  item for the combobox after filling the items.

Hope it helps. Regards.
That should be SelectedIndex instead of SelectedItem:

    Me.txtItem.SelectedIndex = 0