Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Pull out a substring from a Combobox.

Posted on 2005-05-06
12
Medium Priority
?
268 Views
Last Modified: 2010-04-23
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
Comment
Question by:Richard Kreidl
  • 4
  • 2
  • 2
  • +3
11 Comments
 
LVL 22

Expert Comment

by:rspahitz
ID: 13946709
So, is it not working?
The code looks fine.
0
 
LVL 34

Expert Comment

by:Sancler
ID: 13946915
... 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
 
LVL 22

Expert Comment

by:rspahitz
ID: 13946979
good point, and neither variable is being used anywhere else...
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.

 
LVL 2

Author Comment

by:Richard Kreidl
ID: 13947125
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
 
LVL 2

Author Comment

by:Richard Kreidl
ID: 13947669
I figured it out by using the following statement:


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

The question can be deleted
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 13949480
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
 
LVL 34

Expert Comment

by:Sancler
ID: 13949766
No problems so far as I'm concerned.
0
 
LVL 10

Expert Comment

by:gangwisch
ID: 13952184
you would just do:
dim a() as string = split(combobox1.items.selecteditem.text,":")
dim whatuneed as string=a(1)
0
 
LVL 8

Expert Comment

by:doobdave
ID: 13958070
much more elegant solution from gangwisch...
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 13959254
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
 

Accepted Solution

by:
OzzMod earned 0 total points
ID: 13966928
Closed, 500 points refunded.
OzzMod
Community Support Moderator (Graveyard shift)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question