Removing text string on button click

Cracken

Member
Joined
Sep 30, 2008
Messages
15
Location
England UK
Programming Experience
Beginner
Hi

Could someone please advise how to get this to work.
I have a large text box where plain text is pasted into, next to this is a small single line text box where you can enter other text.

On a button click I am looking to remove any text in the large box that is the same as what has been typed in the smaller box.

Here the code I have so far:

Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click

Dim chars As String
chars = txtRemove.Text

If txtText.Text.Contains(chars) Then
txtText.Text.Remove(chars)
End If
End Sub

Thanks in advance.

Mark
 
You could try something like:
VB.NET:
Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
  txtText.Text = txtText.Text.Replace(txtRemove.Text, String.Empty)
End Sub
 
Here's 1 way of doing it.

VB.NET:
Dim myList As New List(Of String)
myList.AddRange(Me.TextBoxMulti.Lines)

VB.NET:
For i As Integer = myList.Count - 1 To 0 Step -1
    If myList.Item(i) = Me.TextBoxSingle.Text Then
        myList.RemoveAt(i)
    End If
Next

VB.NET:
Me.TextBoxMulti.Text = String.Join(Environment.NewLine, myList.ToArray)
 
You could try something like:
VB.NET:
Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
  txtText.Text = txtText.Text.Replace(txtRemove.Text, String.Empty)
End Sub

This will leave a blank line in the multiline textbox. If you change it to remove the duplicate Environment.NewLine it will work.

VB.NET:
txtText.Text = txtText.Text.Replace(txtRemove.Text, String.Empty).Replace(Environment.NewLine & Environment.NewLine, Environment.NewLine)
 
This will leave a blank line in the multiline textbox. If you change it to remove the duplicate Environment.NewLine it will work.

VB.NET:
txtText.Text = txtText.Text.Replace(txtRemove.Text, String.Empty).Replace(Environment.NewLine & Environment.NewLine, Environment.NewLine)
Yes and I would have included code to remove blank lines, except that's something Cracken never said. All Cracken was asking for was a way to remove just the text that matched whatever was typed into the textbox, not remove the text and strip it of blank lines.
 
Thnaks for the input guys.
Blanks lines are fine it was only to remove text such as "/" "?" "#" if need be.

Cheers guys, great forum ! I'll be back soon.

Mark
 
txtText.Text.Remove(chars)

You need to understand:

string.Remove is the complement of Substring.. Where Substring(10) gives you everything after position 9, Remove(10) gives you everything up to position 10. Position starts at 0.


"ABCDE".Substring(3) -OUTPUTS-> "DE"
"ABCDE".Remove(3) -OUTPUTS-> "ABC"

Strings are read only after they are created. Every operation that affects a string content is actually generating a new string with the effect applied. You MUST capture and save the result. The original string is NOT edited.

Dim result as String = original.Replace("a", "b")

Other Notes:

Don't name varaibles with lame names unrelated to their purpose (Dim chars As String).. chars, bad name as it is because it has no purpose implication, also implies an array of Char, not a String
 
Back
Top