[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I change only the last 2 or 3 characters of a selected listbox item in VB6?

Posted on 2011-04-21
12
Medium Priority
?
241 Views
Last Modified: 2012-08-13
I have a VB6 baseball lineup application that uses the following player extensions:  _P, _C, _1B, _2B, _3B, _SS, _LF, _RF, _CF

These extensions are appened to player names. For example: R Jones_SS, T Smith_RF, M Hardy_3B

The 9 player names are in a listbox and I have a textbox and a button.

Question:  How do I change only the extension of a selected player in the listbox by simply putting a different extension in the textbox and clicking the button?

Example: I put _CF into the textbox, select a guy name R Thomas_3B in the listbox, click the button..... and the result changes the guy in the listbox to R Thomas_CF

The key is reading the underscore character and everything to the right of it. Any help is appreciated....thanks.
0
Comment
Question by:jazjef
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 20

Expert Comment

by:Rikin Shah
ID: 35445901
ListBox1.Items.Item(<<selected index>>)="Change it to this"
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35445918
Dim newSufix as string
Newsufix="_CF"
Dim name as string
name=""
Dim newname as string
newname = substring(name, 1, len(name) -3) & newsufix
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35445936
Sorry that is only for 3 chars
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35446003
Hello, sorry, my last coment was bad (from my mobile), here you have a complete example:

Private Sub Form_Load()
    Dim newName As String
    newName = NewPlayerName("R Thomas_3B", "_CF")
End Sub

Function NewPlayerName(playerName As String, newSufix As String) As String
    Dim charIndex As Integer
    charIndex = InStr(playerName, "_") - 1
    
    Dim nameLenght As Integer
    nameLenght = Len(playerName)

    NewPlayerName = Left(playerName, nameLenght - (nameLenght - charIndex)) & newSufix
End Function

Open in new window


I hope this is useful to you.
0
 
LVL 2

Expert Comment

by:balatheexpert
ID: 35446659
please let me know how have you assigned the values? have you set the "List fill Range" property of the listbox control and have provided the values there?
0
 
LVL 4

Author Comment

by:jazjef
ID: 35446933
I want to ignore player name to the left of the '_' character; I only want to be able to change the extension of any player who is the currently selected item in the listbox.
0
 
LVL 4

Author Comment

by:jazjef
ID: 35447087
Here's a clarified example of what I want to do.....

My llistbox looks like this:

G Marks_RF
R Jones_LF
T Millen_CF
J Hock_DH
T Wilson_C
H Meyer_1B
K Barnes_2B
H Gore_3B
M Burns_SS

I want to be able to change the extension of any player I select in the listbox. For example, swapping the extensions of two of the players:  I want to be able to enter _SS in the textbox, highlight/select G Marks_RF in the listbox, click the button, and have it change to G Marks_SS. Next I want to highlight/select M Burns_SS in the listbox, enter _RF in the textbox, click the button, and have it change to M Burns_RF.

By doing this I will have effectively swapped the extensions for two of the player names in the listbox. A solution that address the specific name of only one player will not work---I need the solution to work for any player of any name that might be selected in the listbox. I just want to be able to change the extensions to the right of the '_' for any player selected in the list.
0
 
LVL 2

Expert Comment

by:balatheexpert
ID: 35447132
I have understood the requirement from your first post itself. But i wanted to know, how have you supplied the values to the list box:
1. By setting "List Fill Range" a range and filling in these names in that specific range or
2. what else method did you use to do it?

coz, based on that only, i can suggest you.

Thanks,
Bala
0
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 2000 total points
ID: 35447181
mmm, well, you can do this using my example, try:
Paste this in a form with a List named list1, a text box named Text1 and a command named command1, press the buttom to change the selected player name extension for the textbox value.
Private Sub Command1_Click()
    If List1.ListIndex < 0 Then Exit Sub
    
    List1.List(List1.ListIndex) = NewPlayerName(List1.List(List1.ListIndex), Text1.Text)
End Sub

Private Sub Form_Load()
    List1.AddItem "R Thomas1_3B"
    List1.AddItem "R Thomas4_AB"
    List1.AddItem "R Thomas123_A"
    
    Dim newName As String
    newName = NewPlayerName("R Thomas_3B", "_CF")
End Sub

Function NewPlayerName(playerName As String, newSufix As String) As String
    Dim charIndex As Integer
    charIndex = InStr(playerName, "_")
    
    Dim nameLenght As Integer
    nameLenght = Len(playerName)

    NewPlayerName = Left(playerName, nameLenght - (nameLenght - charIndex)) & newSufix
End Function

Open in new window

0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35447188
Ignore lines 12 and 13 of my last code, just for demo.
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 35448260
Here's some code that can help.  

My 1st suggestion is that rather than using a text box, use a drop-down list box (Combo) to allow the user to select a position from the dropdown rather than keying it in (if you allow it to be keyed, it can be keyed wrong and you suddenly have a EF rather than an RF).

The code assumes that you've allowed the list of players to have multiple players selected.  

Commnand1 will update all  selected player's with the currently selected position.

Command2 will swap the position of the 1st two players selected.
Private Sub Form_Load()
    
    List1.AddItem "G Marks_RF"
    List1.AddItem "R Jones_LF"
    List1.AddItem "T Millen_CF"
    List1.AddItem "J Hock_DH"
    List1.AddItem "T Wilson_C"
    List1.AddItem "H Meyer_1B"
    List1.AddItem "K Barnes_2B"
    List1.AddItem "H Gore_3B"
    List1.AddItem "M Burns_SS"
    
    Combo1.Clear
    Combo1.AddItem "EX"   'No position assigned
    Combo1.AddItem "1B"
    Combo1.AddItem "2B"
    Combo1.AddItem "3B"
    Combo1.AddItem "P"
    Combo1.AddItem "C"
    Combo1.AddItem "DH"
    Combo1.AddItem "LF"
    Combo1.AddItem "CF"
    Combo1.AddItem "RF"
    Combo1.ListIndex = 0
    
End Sub
'Assign the position selected in Combo1 to 1st player selected
Private Sub Command1_Click()
Dim NewPosition As String
Dim PlayerIndex As Integer
Dim Player As String
Dim SelIndex As Integer

    'Get new position for new position combo drop list
    NewPosition = Combo1.List(Combo1.ListIndex)
    If Len(NewPosition) = 0 Then
        MsgBox "Select New Position"
        Exit Sub
    End If
    
    'Get 1st Player Selected
    SelIndex = 1
    PlayerIndex = GetSelectedIndex(SelIndex)
    If PlayerIndex = -1 Then
        MsgBox "Select a Player"
        Exit Sub
    End If
    
    Do While (PlayerIndex > -1)
        'Update Selected Players Position
        Player = GetPlayerName(List1.List(PlayerIndex))
        List1.List(PlayerIndex) = Player & "_" & NewPosition
        'Get Next Selected Player
        SelIndex = SelIndex + 1
        PlayerIndex = GetSelectedIndex(SelIndex)
    Loop

End Sub
'Swap the positions of the 1st 2 players selected
Private Sub Command2_Click()
Dim Player1Index As Integer
Dim Player1 As String
Dim Position1 As String
Dim Player2Index As Integer
Dim Player2 As String
Dim Position2 As String

    'Get the 1st 2 selected players
    Player1Index = GetSelectedIndex(1)
    Player2Index = GetSelectedIndex(2)
    If Player2Index = -1 Then
        MsgBox "Select two players"
        Exit Sub
    End If
    
    'Get the Players Name and Position
    Player1 = GetPlayerName(List1.List(Player1Index))
    Player2 = GetPlayerName(List1.List(Player2Index))
    Position1 = GetPlayerPosition(List1.List(Player1Index))
    Position2 = GetPlayerPosition(List1.List(Player2Index))
    
    'Swap the Positions
    List1.List(Player1Index) = Player1 & "_" & Position2
    List1.List(Player2Index) = Player2 & "_" & Position1
    
End Sub
'Get the Nth selected index from Player list
'Return -1 if SelIndex exceeds number of Players selected
Private Function GetSelectedIndex(SelIndex As Integer) As Integer
Dim SelCount As Integer
Dim Index As Integer

    SelCount = 0
    For Index = 0 To List1.ListCount - 1
        If List1.Selected(Index) Then
            SelCount = SelCount + 1
            If SelCount = SelIndex Then
                GetSelectedIndex = Index
                Exit Function
            End If
        End If
    Next Index
    
    GetSelectedIndex = -1 'Not enough Players selected

End Function
'Return the given player's Name without a position
Private Function GetPlayerName(Player As String)
Dim Pos As Integer

    'Search for Position seperator backwards
    '   incase '_' used in name for some reason
    Pos = InStrRev(Player, "_")
    If Pos > 1 Then
        GetPlayerName = Left(Player, Pos - 1)
    Else
        'Player must not have a position for some reason
        GetPlayerName = Player
    End If
End Function
'Return the given player's Position without a name
Private Function GetPlayerPosition(Player As String)
Dim Pos As Integer

    'Search for Position seperator backwards
    '   incase '_' used in name for some reason
    Pos = InStrRev(Player, "_")
    If Pos > 1 Then
        GetPlayerPosition = Mid(Player, Pos + 1)
    Else
        'Player must not have a position for some reason
        GetPlayerPosition = ""
    End If
End Function

Open in new window

0
 
LVL 4

Author Closing Comment

by:jazjef
ID: 35448693
Bingo. Works great. Thanks for all the replies.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month19 days, 4 hours left to enroll

834 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