How to remove vb.net Richtextbox lines that not contains specific text? - vb.net

I use the next code to remove lines from Richtextboxes but that way i can only tell what line to remove. I need to remove all lines that not contains specific text, can this be done with some edits of my code?
1st piece:
Private Property lineToBeRemovedlineToBeRemoved As Integer
2nd piece:
Dim lineToBeRemoved As Integer = 0
lineToBeRemovedlineToBeRemoved = lineToBeRemoved - 0
Dim str As String = RichTextBox1.Lines(lineToBeRemoved)
RichTextBox1.Find(str & vbCr)
RichTextBox1.SelectedText = ""

This code will remove any line from a richtextbox RichTextbox1 that does not contain "Test" on it. Remember to add Imports System.Text.RegularExpressions to the top of your code.
Private Sub RemoveLines()
Dim lines As New List(Of String)
lines = RichTextBox1.Lines.ToList
Dim FilterText = "Test"
For i As Integer = lines.Count - 1 To 0 Step -1
If Not Regex.IsMatch(lines(i), FilterText) Then
lines.RemoveAt(i)
End If
Next
RichTextBox1.Lines = lines.ToArray
End Sub

You code is not close. You should start over. Use a for loop to go through the RichTextBox lines. If the text is not in a line, then delete it. Tip: It may be easier to go from the last line to the first to avoid problems when deleting.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RTB.Select(RTB.GetFirstCharIndexFromLine(2), RTB.Lines(2).Count)
RTB.SelectionLength = RTB.Lines(2).Length + 1
RTB.SelectedText = ""
End Sub

Try this code, I applied to my prog and it work good.
When use, just ... call dels("unwanted") ==> Line which contain unwanted word will disappear.
Private Sub dels(sw As String)
Dim ud As String = "" 'for keep all we need
Dim cn As Integer = 0 'for avoid empty line
For Each line As String In RichTextBox1.Lines 'for every line in reichtextbox
If Len(line) > 5 Then 'if that line got more than 5 character
If InStr(line.ToLower, sw.ToLower) < 1 Then 'transform them to lower case for better resulted
If cn = 1 Then ud = ud + vbCrLf 'not place new-line if it is first
ud = ud + line 'keep this line if not match ne want delete
cn = 1 'turn-off first line signal
End If
End If
Next
RichTextBox1.Clear() 'empty richtextbox
RichTextBox1.AppendText(ud) 'update richtextbox with the unwanted
End Sub

Related

how to edit blank lines inside text file using VB.Net

hi i'm trying to fill blank lines in a text file,but only line 69 is being edited knowing that all the lines are blank,this is my code please help
Public Sub labelstrings()
Dim lines As String() = File.ReadAllLines(del.filename)
Dim v As Integer
For v = 0 To 69
ReDim lines(v)
If String.IsNullOrWhiteSpace(lines(v)) Then
lines(v) = "ali"
File.WriteAllLines(del.filename, lines)
End If
Next
End Sub
i also tried:
Public Sub labelstrings()
Dim content As String
content = File.ReadAllText(del.filename)
content = content.Replace(String.IsNullOrWhiteSpace(content), "ali")
File.WriteAllText(del.filename, content)
End Sub
Process the entire array, then re-write the file only once afterwards. There is no need to re-dim the array, and you should use its upperbound instead of a hard-coded value:
Public Sub labelstrings()
Dim lines As String() = File.ReadAllLines(del.filename)
For v As Integer = 0 To lines.GetUpperBound(0)
If String.IsNullOrWhiteSpace(lines(v)) Then
lines(v) = "ali"
End If
Next
File.WriteAllLines(del.filename, lines)
End Sub

VB "Index was out of range, must be non-negative and less than the size of the collection." When trying to generate a random number more than once

So I'm trying to generate a random number on button click. Now this number needs to be between two numbers that are inside my text file with various other things all separated by the "|" symbol. The number is then put into the text of a textbox which is being created after i run the form. I can get everything to work perfectly once, but as soon as i try to generate a different random number it gives me the error: "Index was out of range, must be non-negative and less than the size of the collection." Here is the main code as well as the block that generates the textbox after loading the form. As well as the contents of my text file.
Private Sub generate()
Dim newrandom As New Random
Try
Using sr As New StreamReader(itemfile) 'Create a stream reader object for the file
'While we have lines to read in
Do Until sr.EndOfStream
Dim line As String
line = sr.ReadLine() 'Read a line out one at a time
Dim tmp()
tmp = Split(line, "|")
rows(lineNum).buybutton.Text = tmp(1)
rows(lineNum).buyprice.Text = newrandom.Next(tmp(2), tmp(3)) 'Generate the random number between two values
rows(lineNum).amount.Text = tmp(4)
rows(lineNum).sellprice.Text = tmp(5)
rows(lineNum).sellbutton.Text = tmp(1)
lineNum += 1
If sr.EndOfStream = True Then
sr.Close()
End If
Loop
End Using
Catch x As Exception ' Report any errors in reading the line of code
Dim errMsg As String = "Problems: " & x.Message
MsgBox(errMsg)
End Try
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
rows = New List(Of duplicate)
For dupnum = 0 To 11
'There are about 5 more of these above this one but they all have set values, this is the only troublesome one
Dim buyprice As System.Windows.Forms.TextBox
buyprice = New System.Windows.Forms.TextBox
buyprice.Width = textbox1.Width
buyprice.Height = textbox1.Height
buyprice.Left = textbox1.Left
buyprice.Top = textbox1.Top + 30 * dupnum
buyprice.Name = "buypricetxt" + Str(dupnum)
Me.Controls.Add(buyprice)
pair = New itemrow
pair.sellbutton = sellbutton
pair.amount = amounttxt
pair.sellprice = sellpricetxt
pair.buybutton = buybutton
pair.buyprice = buypricetxt
rows.Add(pair)
next
end sub
'textfile contents
0|Iron Sword|10|30|0|0
1|Steel Sword|20|40|0|0
2|Iron Shield|15|35|0|0
3|Steel Shield|30|50|0|0
4|Bread|5|10|0|0
5|Cloak|15|30|0|0
6|Tent|40|80|0|0
7|Leather Armour|50|70|0|0
8|Horse|100|200|0|0
9|Saddle|50|75|0|0
10|Opium|200|500|0|0
11|House|1000|5000|0|0
Not sure what else to add, if you know whats wrong please help :/ thanks
Add the following two lines to the start of generate():
Private Sub generate()
Dim lineNum
lineNum = 0
This ensures that you don't point to a value of lineNum outside of the collection.
I usually consider it a good idea to add
Option Explicit
to my code - it forces me to declare my variables, and then I think about their initialization more carefully. It helps me consider their scope, too.
Try this little modification.
I took your original Sub and changed a little bit take a try and let us know if it solve the issue
Private Sub generate()
Dim line As String
Dim lineNum As Integer = 0
Dim rn As New Random(Now.Millisecond)
Try
Using sr As New StreamReader(_path) 'Create a stream reader object for the file
'While we have lines to read in
While sr.Peek > 0
line = sr.ReadLine() 'Read a line out one at a time
If Not String.IsNullOrEmpty(line) And Not String.IsNullOrWhiteSpace(line) Then
Dim tmp()
tmp = Split(line, "|")
rows(lineNum).buybutton.Text = tmp(1)
rows(lineNum).buyprice.Text = rn.Next(CInt(tmp(2)), CInt(tmp(3))) 'Generate the random number between two values
rows(lineNum).amount.Text = tmp(4)
rows(lineNum).sellprice.Text = tmp(5)
rows(lineNum).sellbutton.Text = tmp(1)
lineNum += 1
End If
End While
End Using
Catch x As Exception ' Report any errors in reading the line of code
Dim errMsg As String = "Problems: " & x.Message
MsgBox(errMsg)
End Try
End Sub

VB.NET - How to delete multiple consecutive lines in a text file?

I am making a customer management system for a school project and I need to be able to delete customers.
Each customer takes up 8 lines in the text file.
I have got it to delete one line using:
Dim lines() As String
Dim outputlines As New List(Of String)
Dim searchstring1 As String = lblName.Text
lines = IO.File.ReadAllLines("Customers.text")
For Each line As String In lines
If line.Contains(searchstring1) = False Then
outputlines.Add(line)
FileClose(1)
System.IO.File.Delete("Customers.text")
IO.File.WriteAllLines("Customers.text", outputlines)
FileClose(1)
End If
Next
But I'm not sure how to repeat this another 7 times, any ideas?
Instead of the for-each-loop, I would use a classical for-next, with a step of 8 - If we can be sure that each dataset consists of 8 lines, and the searchstring is found in the first:
...
For LineNo As Integer=0 to lines.count()-1 step 8
If lines(LineNo).Contains(searchstring1) = False
...
End If
Next lineNo
Additionally, you should close the input file directly after reading before going into the for-loop, and you should probably write the outputlines only after the for-next-loop is done, otherwise you will remove all customers after the searched one.
Next step is to check for errors which may occur during file operations...
If your file has mixed format and cannot use Step 8 because it not contains only N-8-rows of data, you can use a counter.
Set it to 8 when find customer and decrement it each loop.
The creation of new line in output file will be only when counter = 0.
Dim countMatchingLines As Integer = 0
For Each line As String In lines
If line.Contains(searchstring1) Then
countMatchingLines = 8
End If
If countMatchingLines = 0 Then
outputlines.Add(line)
FileClose(1)
System.IO.File.Delete("Customers.text")
IO.File.WriteAllLines("Customers.text", outputlines)
FileClose(1)
Else
countMatchingLines -= 1
End If
Next
Another solution, is just skip next 8 lines by manipulation for-var-counter:
For i as Integer = 0 to lines.Count() - 1
If line.Contains(searchstring1) Then
i = Math.Min(i + 8, lines.Count()-1)
Else
outputlines.Add(line)
FileClose(1)
System.IO.File.Delete("Customers.text")
IO.File.WriteAllLines("Customers.text", outputlines)
FileClose(1)
End If
Next
It will avoid necessary iterates and Ifs..
Try This...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = RemoveParagraphOfText("GARBAGE RECORD", Line)
End Sub
Public Function RemoveParagraphOfText(ByVal textTobeRemoved As String, ByVal Message As String) As String
RemoveParagraphOfText = Message.Replace(textTobeRemoved, "")
End Function

How to color multiple text from richtextbox in vb.net

So I made a chat in vb.net (using FTP server) and I want to color each name from my chat
so the file with messages is something like the following:
#2George: HI
#2George: Hi geoo
and using RichTextBox1 textchanged event I add:
For Each line In RichTextBox1.Lines
If Not line Is Nothing Then
If line.ToString.Trim.Contains("#2") Then
Dim s$ = line.Trim
RichTextBox1.Select(s.IndexOf("#") + 1, s.IndexOf(":", s.IndexOf("#")) - s.IndexOf("#") - 1)
MsgBox(RichTextBox1.SelectedText)
RichTextBox1.SelectionColor = Color.DeepSkyBlue
End If
End If
Next
the first name (George) changed his color but the second one didn't.
Any ideas why this is happening?
The main problem is that your IndexOf calculations are using the index of the current line, but you are not translating that index to where that line is being used in the RichTextBox. That is, your second line of #2George: Hi geoo is finding an index of 0 for the # sign, but index 0 in the RichTextBox is referring to the line #2George: HI, so you keep redrawing the first line every time.
To fix the immediate problem:
For i As Integer = 0 To RichTextBox1.Lines.Count - 1
Dim startIndex As Integer = RichTextBox1.Text.IndexOf("#", _
RichTextBox1.GetFirstCharIndexFromLine(i))
If startIndex > -1 Then
Dim endIndex As Integer = RichTextBox1.Text.IndexOf(":", startIndex)
If endIndex > -1 Then
RichTextBox1.Select(startIndex, endIndex - startIndex)
RichTextBox1.SelectionColor = Color.DeepSkyBlue
End If
End If
Next
The next problem is that doing this in the TextChanged event re-draws all the lines all the time. That won't scale too well. Consider drawing the text before you add it to the control by using a preformatted RTF line. Something like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
AddRTFLine("#2George", "Hi")
AddRTFLine("#2George", "Hi geoo")
End Sub
Private Sub AddRTFLine(userName As String, userMessage As String)
Using box As New RichTextBox
box.SelectionColor = Color.DeepSkyBlue
box.AppendText(userName)
box.SelectionColor = Color.Black
box.AppendText(": " & userMessage)
box.AppendText(Environment.NewLine)
box.SelectAll()
RichTextBox1.Select(RichTextBox1.TextLength, 0)
RichTextBox1.SelectedRtf = box.SelectedRtf
End Using
End Sub

How can I programmatically remove text from a ReadOnly RichTextBox?

The following code aims to maintain a text buffer in a ReadOnly RichTextBox, with a maximum number of characters stored, and always kept scrolled to the bottom. It streams a realtime log.
But in my attempt to maintain the maximum character count, rtMessages.TextLength() isn't changing after rtMessages.SelectedText = String.Empty and consequently, without the defensive If block, I'd end up with an infinite loop attempting to repeatedly delete the first line of the buffer.
When I remove the ReadOnly-ness of the RichTextBox, this functionality succeeds. Seems a little strange, since AppendText succeeds, but I understand that selection is a different beast.
Can I make it so that a ReadOnly RichTextBox is programmatically modifiable?
Private Sub onNewData(ByRef data As String) Handles _server.clientSentData
' Add new text
rtMessages.SelectionStart = rtMessages.TextLength()
rtMessages.AppendText(data)
' Delete oldest text line-by-line until the whole buffer is shorter than MAX_TEXT_LENGTH characters
Const MAX_TEXT_LENGTH = 200
Dim textLength = rtMessages.TextLength()
While textLength > MAX_TEXT_LENGTH
Dim i As Int16 = 0
Do While rtMessages.GetLineFromCharIndex(i) < 1
i += 1
Loop
rtMessages.Select()
rtMessages.SelectionStart = 0
rtMessages.SelectionLength = i
rtMessages.SelectedText = String.Empty
rtMessages.SelectionLength = 0
If rtMessages.TextLength() = textLength Then
rtMessages.Clear()
rtMessages.AppendText("[buffer trimming algorithm failed]")
Exit While
End If
textLength = rtMessages.TextLength()
End While
' Scroll down
rtMessages.SelectionStart = rtMessages.TextLength()
rtMessages.ScrollToCaret()
End Sub
While trying to replace SelectedText in a ReadOnly RichTextBox doesn't work, using the SelectedRtf does work:
'rtMessages.Select()
'rtMessages.SelectionStart = 0
'rtMessages.SelectionLength = i
'rtMessages.SelectedText = String.Empty
'rtMessages.SelectionLength = 0
rtMessages.Select(0, i)
rtMessages.SelectedRtf = "{\rtf1\ansi}"
rtMessages.SelectionLength = i - 1
Should be replaced by
rtMessages.SelectionLength = i
EDIT #1
By adding the -1 to the SelectionLength, you're missing the last character of the first line. On the second run, only 1 character will be on the first line (the one you missed on the first run). Then you will try to delete a SelectionLength of 0 and you'll get the same TextLength for every other runs and there you go with the infinite loop!
You can remove the read-only parameter, write your appendtext code and then make the richtextbox readonly again.
I know this is an old thread, but you can get around the ReadOnly issue the replacing the following code:
rtMessages.Select()
rtMessages.SelectionStart = 0
rtMessages.SelectionLength = i
rtMessages.SelectedText = String.Empty
rtMessages.SelectionLength = 0
with this:
rtMessages.Text = rtMessages.Text.Substring(i)
I'm not sure if this is better or worse performance, but it gets around the RichTextBox being set as ReadOnly
Edit:
Here is the complete code used to test this (Note: I added the code to a Button.Click for testing)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Static X As Integer = 0
X += 1
Dim Data As String = "Line " & X.ToString & ControlChars.NewLine
Me.onNewData(Data)
End Sub
Private Sub onNewData(ByRef data As String)
' Add new text
rtMessages.SelectionStart = rtMessages.TextLength()
rtMessages.AppendText(data)
' Delete oldest text line-by-line until the whole buffer is shorter than MAX_TEXT_LENGTH characters
Const MAX_TEXT_LENGTH As Integer = 200
Dim textLength As Integer = rtMessages.TextLength()
While textLength > MAX_TEXT_LENGTH
Dim i As Integer = 0
Do While rtMessages.GetLineFromCharIndex(i) < 1
i += 1
Loop
'rtMessages.Select()
'rtMessages.SelectionStart = 0
'rtMessages.SelectionLength = i
'rtMessages.SelectedText = String.Empty
'rtMessages.SelectionLength = 0
rtMessages.Text = rtMessages.Text.Substring(i)
If rtMessages.TextLength() = textLength Then
rtMessages.Clear()
rtMessages.AppendText("[buffer trimming algorithm failed]")
Exit While
End If
textLength = rtMessages.TextLength()
End While
' Scroll down
rtMessages.SelectionStart = rtMessages.TextLength()
rtMessages.ScrollToCaret()
End Sub