• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

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
0
sasllc
Asked:
sasllc
  • 3
  • 3
  • 3
2 Solutions
 
gplanaCommented:
I thing you should do something like:

Me.txtItem.AddItem(psItemCode1)
Me.txtItem.AddItem(psItemCode2)
Me.txtItem.AddItem(psItemCode3)

Hope it helps
0
 
sasllcAuthor Commented:
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?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Try:

    Me.txtItem.Items.Add(psItemCode1)
    Me.txtItem.Items.Add(psItemCode2)
    Me.txtItem.Items.Add(psItemCode3)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
gplanaCommented:
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.
0
 
sasllcAuthor Commented:
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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
0
 
sasllcAuthor Commented:
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?
0
 
gplanaCommented:
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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
That should be SelectedIndex instead of SelectedItem:

    Me.txtItem.SelectedIndex = 0
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now