Hello

I am currently stuck on trying to collect some string items from a richtextbox and then count how many items there are. I then want to relocate one of the string items if there are more than three into another richtextbox.

I currently have been attempting to capture the string items into an array but I have had no luck.

The items in the richtextbox are

Laura Philips
Jaime Jarvis
Rachel Dawkins

So in this case I would want to remove the last entry "Rachel Dawkins" and place it into another richtextbox.

If anyone could give me an example that I can work of or point me in the right direction it would be much appreciated.

Thank you

Recommended Answers

All 5 Replies

See if this helps.
2 RichTextBoxes, 1 Button

Public Class Form1
    Private arlRtbLines As New ArrayList '// adds rtb1 lines if string is not found in a line.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sStringToFind As String = "Rachel Dawkins" '// String to locate.
        arlRtbLines.Clear() '// clear for new input.
        With RichTextBox1
            For Each rtbLine As String In .Lines '// loop thru all rtb1 lines.
                If Not rtbLine.Contains(sStringToFind) Then '// check first if it does not contain, since more lines probably will not.
                    arlRtbLines.Add(rtbLine) '// add line to ArrayList.
                Else
                    RichTextBox2.Text = rtbLine '// send line to rtb2.
                End If
            Next
            .Clear() '// clear rtb1 for new input to write the lines back.
            For Each itm As String In arlRtbLines
                If Not .Text = "" Then .Text &= vbNewLine & itm Else .Text = itm '// add lines back to rtb1.
            Next
        End With
    End Sub
End Class

Thank you for this, much appreciated.

Again thank you for your response.

I am however having a few more problems. The code you gave me was great and worked perfectly, the problem is that the name being removed from the list could be of any value and there is the possibility of being more than one name that I want to remove from the list.

I have tried modifying the code to fix the problem by adding another array and removing any more than two variables in the list but I'm not the most proficient programmer so I haven't got far.

If there is any way you could help me again I would be very grateful.

Thank you

This will use 2 ArrayLists.
1 ArrayList is to write lines back to original rtb and the other is to add the Names to be located in the original rtb.

Public Class Form1
    Private arlRtbLines As New ArrayList '// adds rtb1 lines if string is not found in a line.
    Private arlNamesToFind As New ArrayList '// adds the Names that need to be extracted from rtb1.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        arlRtbLines.Clear() : arlNamesToFind.Clear() '// clear both if needed for new input.
        '// add names to find.
        arlNamesToFind.Add("Rachel Dawkins")
        arlNamesToFind.Add("Laura Philips")
        '// check if items to find have been added.
        RichTextBox2.Clear() '// clear output rtb.
        If Not arlNamesToFind.Count = 0 Then '// check if Names have been added to the Names to Find ArrayList.
            With RichTextBox1
                For Each rtbLine As String In .Lines '// loop thru all rtb1 lines.
                    If Not arlNamesToFind.Contains(rtbLine) Then '// if name is not found.
                        arlRtbLines.Add(rtbLine) '// add line to ArrayList for writing lines back.
                    Else
                        With RichTextBox2
                            If Not .Text = "" Then .Text &= vbNewLine & rtbLine Else .Text = rtbLine '// send line to rtb2.
                        End With
                    End If
                Next
                .Clear() '// clear rtb1 for new input to write the lines back.
                For Each itm As String In arlRtbLines
                    If Not .Text = "" Then .Text &= vbNewLine & itm Else .Text = itm '// add lines back to rtb1.
                Next
            End With
        End If
    End Sub
End Class

Thank you again for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.