Difficult Append/Replace question

My attached code cycles through a RadList and appends the data to the existing label(s) at the bottom of my code.

What it DOESN'T do is remove the items from the LABELS that have been removed.

So..If lblFipsStates.Text = 'DC-District of Columbia,KY-Anderson,KY-Ballard,KY-Barren,'
It will ADD a new county but if I uncheck the item (KY-Ballard,) it will NOT remove it from my label lblFipsStates.
Protected Sub btnAppend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAppend.Click
        ' set strings to initially loaded labels 
        Dim strState As String = lblFipsStates.Text
        Dim strFips As String = lblFipsFips.Text
        Dim strStateCounty As String = lblFipsCounties.Text

        Dim _state As String = ""
        Dim _s As String = ""
        Dim _s2 As String = ""

        'Start State Append
        _state = cmbStateBox.Text
        Dim sb As New StringBuilder()
        For Each li As RadListBoxItem In RadListBox1.Items
            If li.Checked = True Then
                sb.Append(li.Value)
                sb.Append(",")
            End If
        Next
        If sb.Length > 0 Then
            _s = sb.ToString().Substring(0, sb.ToString().Length - 1)
        End If
        'End State Append 

        'Start State-County Append
        Dim sb2 As New StringBuilder()
        For Each li As RadListBoxItem In RadListBox1.Items
            If li.Checked = True Then
                sb2.Append(_state)
                sb2.Append("-")
                sb2.Append(li.Text)
                sb2.Append(",")
            End If
        Next
        If sb2.Length > 0 Then
            _s2 = sb2.ToString().Substring(0, sb2.ToString().Length - 1)
        End If
        'End State-County Append

        ' the strings we want to append 
        Dim appendState As String = _state
        Dim appendFips As String = sb.ToString()
        Dim appendStateCountry As String = sb2.ToString()

        ' some declaration to easily search for duplicates 
        Dim statelist As New List(Of String)
        Dim fiplist As New List(Of String)
        Dim StateCountyList As New List(Of String)

        ' adding everything 
        statelist = strState.Split(",").ToList()
        fiplist = strFips.Split(",").ToList()
        StateCountyList = strStateCounty.Split(",").ToList()

        Dim appendlist As List(Of String)

        ' add items to state 
        appendlist = appendState.Split(",").ToList()
        For Each x As String In appendlist
            If statelist.Contains(x) = False Then
                statelist.Add(x)
            End If
        Next
        ' add items to fip 
        appendlist = appendFips.Split(",").ToList()
        For Each x As String In appendlist
            If fiplist.Contains(x) = False Then
                fiplist.Add(x)
            End If
        Next
        ' add items to statecountry 
        appendlist = appendStateCountry.Split(",").ToList()
        For Each x As String In appendlist
            If StateCountyList.Contains(x) = False Then
                StateCountyList.Add(x)
            End If
        Next

        ' create the concatenated string 
        strState = String.Join(",", statelist.ToArray())
        strFips = String.Join(",", fiplist.ToArray())
        strStateCounty = String.Join(",", StateCountyList.ToArray)

        'Set Labels to Appended Strings
        lblFipsStates.Text = strState.ToString()
        lblFipsFips.Text = Replace(strFips.ToString(), ",,", ",")
        lblFipsCounties.Text = Replace(strStateCounty.ToString(), ",,", ",")
    End Sub

Open in new window

Larry Bristersr. DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

13598Commented:
Try this
Protected Sub btnAppend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAppend.Click
        ' set strings to initially loaded labels  
        Dim strState As String = lblFipsStates.Text
        Dim strFips As String = lblFipsFips.Text
        Dim strStateCounty As String = lblFipsCounties.Text
 
        Dim _state As String = "" 
        Dim _s As String = "" 
        Dim _s2 As String = "" 
 
        'Start State Append
        _state = cmbStateBox.Text
        Dim sb As New StringBuilder()  = ""
        For Each li As RadListBoxItem In RadListBox1.Items
            If li.Checked = True Then
                sb.Append(li.Value)
                sb.Append(",")
            End If
        Next
        If sb.Length > 0 Then
            _s = sb.ToString().Substring(0, sb.ToString().Length - 1)
        End If
        'End State Append  
 
        'Start State-County Append
        Dim sb2 As New StringBuilder()  = ""
        For Each li As RadListBoxItem In RadListBox1.Items
            If li.Checked = True Then
                sb2.Append(_state)
                sb2.Append("-")
                sb2.Append(li.Text)
                sb2.Append(",")
            End If
        Next
        If sb2.Length > 0 Then
            _s2 = sb2.ToString().Substring(0, sb2.ToString().Length - 1)
        End If
        'End State-County Append
 
        ' the strings we want to append  
        Dim appendState As String = _state
        Dim appendFips As String = sb.ToString()
        Dim appendStateCountry As String = sb2.ToString()
 
        ' some declaration to easily search for duplicates  
        Dim statelist As New List(Of String)
        Dim fiplist As New List(Of String)
        Dim StateCountyList As New List(Of String)
 
        ' adding everything  
        statelist = strState.Split(",").ToList()
        fiplist = strFips.Split(",").ToList()
        StateCountyList = strStateCounty.Split(",").ToList()
 
        Dim appendlist As List(Of String)
 
        ' add items to state  
        appendlist = appendState.Split(",").ToList()
        For Each x As String In appendlist
            If statelist.Contains(x) = False Then
                statelist.Add(x)
            End If
        Next
        ' add items to fip  
        appendlist = appendFips.Split(",").ToList()
        For Each x As String In appendlist
            If fiplist.Contains(x) = False Then
                fiplist.Add(x)
            End If
        Next
        ' add items to statecountry  
        appendlist = appendStateCountry.Split(",").ToList()
        For Each x As String In appendlist
            If StateCountyList.Contains(x) = False Then
                StateCountyList.Add(x)
            End If
        Next
 
        ' create the concatenated string  
        strState = String.Join(",", statelist.ToArray())
        strFips = String.Join(",", fiplist.ToArray())
        strStateCounty = String.Join(",", StateCountyList.ToArray)
 
        'Set Labels to Appended Strings
        lblFipsStates.Text = strState.ToString()
        lblFipsFips.Text = Replace(strFips.ToString(), ",,", ",")
        lblFipsCounties.Text = Replace(strStateCounty.ToString(), ",,", ",")
    End Sub
0
Larry Bristersr. DeveloperAuthor Commented:
13598:
That bombed big tome on the stringbuilders = ""...

Looking for a "Close"???
0
13598Commented:
Yeah, i pasted the wrong window.
Try this:
 Dim sb As New StringBuilder()
        Dim collection As IList(Of RadListBoxItem) = listBox.CheckedItems
        For Each item As RadListBoxItem In collection
        if sb.tostring.trim = "" then
  sb.Append(item.Value )
else
  sb.Append("," & item.Value  )
end if
         Next
        If sb.Length > 0 Then
            _s = sb.ToString.trim
        End If


0
13598Commented:
Try this:
'Start State Append
        _state = cmbStateBox.Text

 Dim sb As New StringBuilder()
        Dim collection As IList(Of RadListBoxItem) = listBox.CheckedItems
        For Each item As RadListBoxItem In collection
        if sb.tostring.trim = "" then
  sb.Append(item.Value )
else
  sb.Append("," & item.Value  )
end if
         
        Next
        If sb.Length > 0 Then
            _s = sb.ToString.trim
        End If
  'End State Append  
 
    'Start State-County Append
        Dim sb2 As New StringBuilder()
Dim collection As IList(Of RadListBoxItem) = listBox.CheckedItems
        For Each item As RadListBoxItem In collection  
if sb2.tostring.trim = "" then        
                sb2.Append(_state)
                sb2.Append("-")
                sb2.Append(item.Value)
else
                sb2.Append(",")
sb2.Append(_state)
                sb2.Append("-")
                sb2.Append(item.Value)            
end if
        Next
        If sb2.Length > 0 Then
            _s2 = sb2.ToString.trim
        End If
        'End State-County Append
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Larry Bristersr. DeveloperAuthor Commented:
Thanks..posting a simple follow-up question for a vb function
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.