Hello all;

I have a random data file that I created with code. The file contains strings that represent items I want to display in a combobox control. The strings contain multiple fields with headers that describe the data to follow. I have NO problem displaying any single field of the string. My problem is that I'd like to be able to populate the combobox with an entire string, but only have a select part of the text visible.

The exact issue is that I need to be able to reference the ending 6 characters of the string as that represents the index of the record. Thus, if the combobox becomes populated with more than 1 matching record, I can select that record and then read it's index. This will help me make modifications to my database. Am I going about this the wrong way OR is there any way I can get this accomplished? I would sincerely apprepriate any help I get.

thanx
m_l

You can parse the last six characters of the string to get the last six.

Just parse the selected item, or parse when the item is being inserted into the combobox.

For example:

Private Sub LoadData(ByVal lstString As List(Of String))
    Try
        For Each s As String In lstString
            ComboBox1.Items.Add(s)

            'You can parse here for reference before the selected index change.
            'dicLastSix is a Dictionary(Of String, String)
            'This will give you a reference before the item is selected in the cobobox/
             dicLastSix.Add(s, GetLastSix(s))
        Next
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

Private Function GetLastSix(ByVal str As String) As String
    'Modify this function to return what you want in the case of an exception being thrown.
    Try
        If str.Length - 7 > 0 Then '0 index have to subtract 7 for 6 items
            Return str.IndexOf(str, str.Length - 7, str.Length - 1)
        Else
            Return str.IndexOf(str, 0, str.Length - 1)
        End If
    Catch ex As Exception
        MsgBox(ex.ToString)
        Return String.Empty
    End Try
End Function

Private Sub IndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    Try
        'Geting the last 6 from caling the function
        If Not IsNothing(TryCast(sender, ComboBox)) Then
            Dim s As String = CStr(TryCast(sender, ComboBox).SelectedItem)
            MsgBox("Last Six: " & GetLastSix(s))
        End If
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

Edited 3 Years Ago by Begginnerdev

Rev Jim;
Please excuse the error on my part. I thought I could be clear enough with a description to get someone to understand my issue, I guess not. Sorry to bother all of you, I'll try to figure this out on my own. Thank you for your understanding, patience and support.

m_l

I wasn't saying "go away". I'm just trying to ensure that you get the answer you want. A clear and descriptive title is the first step. Providing as much information as is reasonably possible is a good secoond step. Feel free to come back to the well if you have further questions.

This question has already been answered. Start a new discussion instead.