Hello Everyone

So i have this flowlayoutpanel that contains checkboxes, so what It does is if a select a checkbox the text of that checkbox will be passed to the ProblemTextBox

checkboxes

My main problem is that I am able to add new checkboxes to the ProblemTextBox, but when i uncheck a textbox I want it to delete that text from the ProblemTextBox

In Example If i check 'PM, MECHANICAL, ERROR' i will the following in the Problems TextBox

PM, MECHANICAL, ERROR

But if I uncheck 'MECHANICAL', I'm unable to delete that word and have something like this

PM, ERROR

So here is the code that im using for that problem, thanks in advance guys

Public Sub DoCheckBox(ByVal selected As String, ByVal status As Boolean)

        If status = True Then
            If ProblemTextBox.Text <> "" Then
                ProblemTextBox.Text += " " + selected
            Else
                ProblemTextBox.Text = selected
            End If
        Else
            If ProblemTextBox.Text <> "" Then
                If ProblemTextBox.Text.Contains(selected) = True Then
                    '??? what goes here

                End If
                'ProblemTextBox.Text += " " + btn.Text
            Else
                ProblemTextBox.Text = selected
            End If
        End If
End Sub

You can split the contents of the textbox, storing them in list (of string) and remove the element, then rebuild the string.

Example:

    Dim str As New List(Of String)

    'Assuming you are using a , seperator.
    str = TextBox1.Text.Split(",").ToList

    str.Remove("test")

    For i = 0 To str.Count - 1
        If i < str.Count - 1 Then
            TextBox1.Text &= str(i) & ","
        Else
            TextBox1.Text &= str(i)
        End If
    Next

    'When "This,is,a,test,string is passed into - "This,is,a,string" will be returned.

Another alternative is to use a common handler for all the checkboxes.checkchanged event and construct the string on each event.

  Private Sub CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
     Handles CheckBox1.CheckedChanged, _
             CheckBox2.CheckedChanged, _
             CheckBox3.CheckedChanged
     Static Problems As New List(Of CheckBox)
     Dim cb As CheckBox = CType(sender, CheckBox)
     If cb.Checked Then
        Problems.Add(cb)
     Else
        Problems.Remove(cb)
     End If
     Dim sb As New System.Text.StringBuilder(100)
     For i As Int32 = 0 To Problems.Count - 1
        sb.Append(Problems.Item(i).Text)
        If i < Problems.Count - 1 Then sb.Append(", ")
     Next
     TextBox1.Text = sb.ToString
  End Sub

Edited 4 Years Ago by TnTinMN: modified code

This question has already been answered. Start a new discussion instead.