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

Pull out a substring from a Combobox.

Here is my Combobx code:

Private Sub cboTo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboTo.SelectedIndexChanged
        Dim Target As String
        Dim Value As String
        Target = cboTo.SelectedItem
        If Target.IndexOf("BAR17") Or Target.IndexOf("FRE09") Or Target.IndexOf("KAN10") Or Target.IndexOf("RES02") _
        Or Target.IndexOf("TON01") Or Target.IndexOf("ZRI01") Then
            Target = Trim((Target.Substring(Target.Length - 5)))
        Else
            Target = Trim((Target.Substring(Target.Length - 7)))
        End If
        ValueInd = Target
 End Sub

My Form1_load code:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cboTo.Items.Clear()
         cboTo.Items.Insert(0, "Gary B. - Logon ID:BAR17")
        cboTo.Items.Insert(1, "Paul F. - Logon ID:FRE09")
        cboTo.Items.Insert(2, "Colleen H. - Logon ID:HEM4021")
        cboTo.Items.Insert(3, "John K. - Logon ID:KAN10")
        cboTo.Items.Insert(4, "Anne P. - Logon ID:PET0661")
        cboTo.Items.Insert(5, "Johnny P. - Logon ID:POO2223")
        cboTo.Items.Insert(6, "Steve R. - Logon ID:RES02")
        cboTo.Items.Insert(7, "Symitria S. - Logon ID:STE4825")
        cboTo.Items.Insert(8, "Kim T. - Logon ID:TON01")
        cboTo.Items.Insert(9, "Robert Z. - Logon ID:ZRI01")
End Sub

If you look at the ComboBox code above I want to trim certain amount of characters from the string based on a 'IndexOf' check. If any of the six 'IndexOf's are true
then I just want to pull off -5 charcters otherwise I want to pull off -7 chararcters.

Example if the user selects: Gary B. - Logon ID:BAR17

It would pull off: BAR17

If the user selects: Anne P. - Logon ID:PET0661

It would pull off: PET0661

Thanks
0
Richard Kreidl
Asked:
Richard Kreidl
  • 4
  • 2
  • 2
  • +3
1 Solution
 
rspahitzCommented:
So, is it not working?
The code looks fine.
0
 
SanclerCommented:
... except that

        Dim Value As String

        [...]

        ValueInd = Target

That is, you are declaring "Value" as the variable name, but assigning the result to "valueInd".


Roger
0
 
rspahitzCommented:
good point, and neither variable is being used anywhere else...
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Richard KreidlSoftware DeveloperAuthor Commented:
The code is not working. When I select: ' Gary B. - Logon ID:BAR17'

I pull off: 'BAR17'

When I select : 'Anne P. - Logon ID:PET0661'

I pull off: 'T0661' instead of: 'PET0661'

Somehow it's retaining the trim of -5 each time.

Your right the variable: ValueInd should have been Value.

Corrected code below:
Private Sub cboTo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboTo.SelectedIndexChanged
        Dim Target As String
        Dim Value As String
        Target = cboTo.SelectedItem
        If Target.IndexOf("BAR17") Or Target.IndexOf("FRE09") Or Target.IndexOf("KAN10") Or Target.IndexOf("RES02") _
        Or Target.IndexOf("TON01") Or Target.IndexOf("ZRI01") Then
            Target = Trim((Target.Substring(Target.Length - 5)))
        Else
            Target = Trim((Target.Substring(Target.Length - 7)))
        End If
        Value = Target
 End Sub
0
 
Richard KreidlSoftware DeveloperAuthor Commented:
I figured it out by using the following statement:


Value = Target.Substring(Target.IndexOf("ID:") + 3)

The question can be deleted
0
 
rspahitzCommented:
works for me...although I think that rkckjk should throw a token thank-you bone (50 points?) to  Sancler for spotting the variable issue that was fixed.
0
 
SanclerCommented:
No problems so far as I'm concerned.
0
 
gangwischCommented:
you would just do:
dim a() as string = split(combobox1.items.selecteditem.text,":")
dim whatuneed as string=a(1)
0
 
doobdaveCommented:
much more elegant solution from gangwisch...
0
 
rspahitzCommented:
I'm not sure I agree that it's more elegant.

First of all, it could potentially give a wrong answer if the text contains two colons (:) since the split string should be "ID:"

Next, the Q-owner was able to solve the problem in one line whereas the "elegant" solution takes two lines.

Third, the split solution, although often very useful, is veru inefficient compared to indexof and substring.  Here if would have little impact unless you're doing it with thousands of records, but sometimes that's a consideration.

Finally, this question was essentially closed so I stand by my suggestion to refund points.  However, I think the split idea is certainly worth considering for certain problems.
0
 
OzzModCommented:
Closed, 500 points refunded.
OzzMod
Community Support Moderator (Graveyard shift)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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