1

I have a combobox whose values (displayvalue) are formatted (from a database query):

John Doe (11111)

where 11111 is the userID. The UserID is the login name for their machine and I want to default the selected value to the login UserID.

Since combobox.findstring(UserID) only matches if the entry begins with that string, I need to iterate through the values to look for the substring of UserID in the entries.

I've searched around here, but solutions seems to land all around this specific example. I can't seem to find a method that returns the display value at a specific index. What am I missing?

EDIT: This is how I am populating my combobox:

Private Sub PopulateDropdown(strSQl As String, objControl As ComboBox, strTextField As String, strDataField As String)

    Dim objdatareader As New DataTable
    objdatareader = DataAccess.GetDataTable(strSQl)
    objControl.DataSource = objdatareader
    objControl.DisplayMember = strTextField
    objControl.ValueMember = strDataField

End Sub
Doug Sholly
  • 123
  • 1
  • 10
  • Check this they show the username and display username + id: http://vb.net-informations.com/dataset/bind-combobox.htm – Mederic Jun 13 '17 at 15:23
  • Does iterating through the [ComboBox.Items](https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.items(v=vs.110).aspx) property provide what you need? – Knowledge Cube Jun 13 '17 at 16:01
  • @Mederic That shows an example of populating the combobox, and I have that part down. After I populate it, I want to select one of the values in the list based on a substring of the displayvalue @Christopher Kyle Horton I can do a `for each item in combobox.items`, but I can't find what method would then give me the displayed text for each item. .text and .value are not members. I need to get the displayvalue back so I can `instr()` it. – Doug Sholly Jun 13 '17 at 16:23
  • You may "have that part down" but how you populate it matters to the answer - we have no idea how the data got there. *Usually* when using `DisplayMember` you are also using a DataSource in which case you should look there for the data. That could be a one line linqy thing – Ňɏssa Pøngjǣrdenlarp Jun 13 '17 at 16:27
  • Edited my post with the method I use to populate the combobox. I don't really have access to the datatable after the fact, unless that is what I am missing here. – Doug Sholly Jun 13 '17 at 16:35
  • If you are trying to decode a user's pick, use the `ValueChanged` event it will give you the ID picked. If it is something else iterate the datatable - you *do* have access to it, but a proper form level datatable might be cleaner – Ňɏssa Pøngjǣrdenlarp Jun 13 '17 at 16:52
  • @DougSholly Based on the documentation for [DisplayMember](https://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.displaymember(v=vs.110).aspx), your `strTextField` should contain the name of the property you are displaying, and hence what you want to access from each item. Unless you know for sure which property this always is, you might be able to [use reflection](https://stackoverflow.com/questions/2942699/access-property-using-its-name-in-vb-net) to get what you want. – Knowledge Cube Jun 13 '17 at 17:22
  • It seems that I am not being clear. I need to perform a function similar to `combobox.FindString("11111")`. The problem with `combobox.FindString("11111")` is that the text does not begin with 11111 in the list, so` FindString` returns -1. So, I need to manually interrogate each value in the list to see if it has the substring of "11111". I can't do it when I populate the dropdown because 1. It is a generic function to populate any combobox and 2. I may need to look for "22222" and select it at a later time. – Doug Sholly Jun 13 '17 at 17:59

1 Answers1

1

This may actually help folks trying to find the ValueMember of a combobox as well.

I am populating my combobox from a datatable, so this solution may only be valid from that. I am not really sure why this works. I just stumbled upon it.

First, I started by doing a for each item in combobox.items. According to intelisense, there is no property of .value, .text, .DisplayMember, or anything related to that. I did notice that the return type on combobox.items is a DataRowView. I am not sure why that is, but I went with it. One of the members of DataRowView is Row. It turns out, each column from the DataTable is added to the Row collection in 'item's' DataRowView. Rows(0) is the first column, Row(1) is the second, etc. I was then able to look in the Row's full text to find my userid, and then select that row by using the FindExactString of the combobox. The below code works (I built the datatable manually in this example):

dim UserID As String="12345"
dim MyTable as New Datatable
MyTable.Columns.Add("Value", Type.GetType("System.String"))
MyTable.Columns.Add("Text", Type.GetType("System.String"))
MyTable.rows.add("1","Bob Smith(11223)"
MyTable.rows.add("2","George Brown(12345)"
cboAssignedID.datasource=MyTable
cboAssignedID.DisplayMember="Text"
cboAssignedID.ValueMember="Value"

For Each item In cboAssignedID.Items
    If InStr(item.Row(1).ToString, UserID) > 0 Then
        cboAssignedID.SelectedIndex = cboAssignedID.FindStringExact(item.Row(1).ToString)
    End If
Next
Community
  • 1
  • 1
Doug Sholly
  • 123
  • 1
  • 10