Link to home
Start Free TrialLog in
Avatar of jazjef
jazjef

asked on

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

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.
Avatar of Rikin Shah
Rikin Shah
Flag of India image

ListBox1.Items.Item(<<selected index>>)="Change it to this"
Dim newSufix as string
Newsufix="_CF"
Dim name as string
name=""
Dim newname as string
newname = substring(name, 1, len(name) -3) & newsufix
Sorry that is only for 3 chars
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.
Avatar of balatheexpert
balatheexpert

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?
Avatar of jazjef

ASKER

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.
Avatar of jazjef

ASKER

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.
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
ASKER CERTIFIED SOLUTION
Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ignore lines 12 and 13 of my last code, just for demo.
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

Avatar of jazjef

ASKER

Bingo. Works great. Thanks for all the replies.