Gettint text from lastline in richtextbox - vb.net

I want to copy the last line of a rich textbox.
I am avoiding Dim lastLine As String = RichTextBox1.Lines(RichTextBox1.Lines.Length - 1)as
it's not working properly, as It works just if there are atleast 2 lines in it.
I'm trying with MsgBox(RichTextBox1.Lines(UBound(richtextbox1.Lines))) but the problem is that even if the richtextbox has just 1 line of text but the cursor is in the second empty line, it will give back "" as I think the software is reading the empty 2nd line.
There is a solution to that?
Thanks

This will get the last non-empty line:
RichTextBox1.Lines.Where(Function(line) line <> String.Empty).Last()
There are some potential issues with that. If there's no text at all or if there are multiple lines but they are all empty, that will throw an exception. To allow for that, you can call LastOrDefault instead, in which case it would return Nothing.
If you only want to exclude the last empty line, e.g. if you have some text followed by a line break and then another line break then you want to get the first of those two empty lines, then you can't really do it in one line:
Dim lines = RichTextBox1.Lines
Dim upperBound = lines.GetUpperBound(0)
Dim lastLine = lines(upperBound)
If lastLine = String.Empty Then
If upperBound > 0 Then
lastLine = lines(upperBound - 1)
Else
lastLine = Nothing
End If
End If
'Use lastLine here.

Related

Finding width of each line in richtextbox where wordwrap is true

I have a richtextbox and i have put a long sentences. Its wordwrap is on. Because of this it shows 4 lines. I want to show the width of each line separately.
I have tried with richtextbox1.lines.length, but it is showing: 1
I have tested this code briefly and I think that it does what you want. It will include carriage return and line break characters in the count I think, so you will have to handle that explicitly if you need different.
Dim previousFirstCharIndex = 0
Dim lineIndex = 1
Dim firstCharIndex = RichTextBox1.GetFirstCharIndexFromLine(lineIndex)
Dim lineLengths As New List(Of Integer)
Do Until firstCharIndex = -1
lineLengths.Add(firstCharIndex - previousFirstCharIndex)
previousFirstCharIndex = firstCharIndex
lineIndex += 1
firstCharIndex = RichTextBox1.GetFirstCharIndexFromLine(lineIndex)
Loop
lineLengths.Add(RichTextBox1.TextLength - previousFirstCharIndex)
MessageBox.Show(String.Join(Environment.NewLine, lineLengths), "Line Lengths")

replace a line in richtextbox vb.net

I have this code but it have errors , what should i do ?
Dim lines As New List(Of String)
lines = RichTextBox1.Lines.ToList
'Dim FilterText = "#"
For i As Integer = lines.Count - 1 To 0 Step -1
'If (lines(i).Contains(FilterText)) Then
RichTextBox1.Lines(i) = RichTextBox1.Lines(i).Replace("#", "#sometext")
'End If
Next
RichTextBox1.Lines = lines.ToArray
Update: while the following "works" it does only modify the array which was returned from the Lines-property. If you change that array you don't change the text of the TextBox. So you need to re-assign the whole array to the Lines-property if you want to change the text(as shown below). So i keep the first part of my answer only because it fixes the syntax not the real issue.
It's not
RichTextBox1.Lines(i).Replace = "#sometext"
but
RichTextBox1.Lines(i) = "#sometext"
You can loop the Lines forward, the reverse loop is not needed here.
Maybe you want to replace all "#" with "#sometext" instead:
RichTextBox1.Lines(i) = RichTextBox1.Lines(i).Replace("#","#sometext")
So here the full code necessary (since it still seems to be a problem):
Dim newLines As New List(Of String)
For i As Integer = 0 To RichTextBox1.Lines.Length - 1
newLines.Add(RichTextBox1.Lines(i).Replace("#", "#sometext"))
Next
RichTextBox1.Lines = newLines.ToArray()
But maybe you could even use:
RichTextBox1.Text = RichTextBox1.Text.Replace("#","#sometext")`
because if we have # abcd this code change it to # sometextabcd ! I
Want a code to replace for example line 1 completely to # sometext
Please provide all relevant informations in the first place next time:
Dim newLines As New List(Of String)
For Each line As String In RichTextBox1.Lines
Dim newLine = If(line.Contains("#"), "#sometext", line)
newLines.Add(newLine)
Next
RichTextBox1.Lines = newLines.ToArray()

Removing linebreak after first value in a .csv string using VB

I have a small function that creates a .csv file from strings stored in a Data Grid.
Dim rowcount As Integer = DataGridView1.Rows.Count - 1
For m As Integer = 0 To rowcount
Dim strrowvalue As String = ""
strrowvalue += CStr(DataGridView1.Rows(m).Cells(0).Value)
For n As Integer = 1 To DataGridView1.Columns.Count - 1
If DataGridView1.CurrentCell.Value Is DBNull.Value Then
strrowvalue += "," + "0"
End If
If DataGridView1.Rows(m).Cells(n).Value Is DBNull.Value Then
strrowvalue += "," + "0"
Else
strrowvalue += "," + CStr(DataGridView1.Rows(m).Cells(n).Value)
End If
Next
If m <> rowcount Then
streamwriter.WriteLine(strrowvalue)
End If
Next
However after the first value a linebreak is inserted. This causes Excel and other programs to place the first value on line 1, then the rest of the values of that line are placed on line 2.
Example:
100 ,
555,333,333,666,777
200 ,
444,555,453,345,778
It should be:
100,555,333,333,666,777
200,444,555,453,345,778
Any ideas?
EDIT: Question has been modified to show that it was a linebreak that was being inserted into the string, not a space.
It’s not clear from your code where the space comes from, and the best way for getting rid of the space is probably to prevent it from ever occurring in the first place.
In addition, the space will not cause Excel to add a line break (I’ve just tested it to make sure). In fact, Excel silently swallows the space. If a line break is inserted in your case, that suggests that the character isn’t a whitespace but something else.
Apart from that, your code for removing whitespace works. You probably forgot to assign the result to a variable:
strrowvalue = strrowvalue.Replace(" ", "")
Edit: since it’s apparently a carriage return character rather than a space, use this:
strrowvalue = strrowvalue.Replace(vbCr, "")
Or, if it’s a full Windows-style line break:
strrowvalue = strrowvalue.Replace(vbCrLf, "")
If neither of these works (weird!), try this:
strrowvalue = strrowvalue.Replace(vbLf, "")
In other news, you should use a StringBuilder here instead, it will perform much better than lots of repeated string concatenations.
Depending on what kind of line break is being inserted, I'd update this line to one of the following:
strrowvalue += CStr(DataGridView1.Rows(m).Cells(0).Value).Replace(vbCrLf,"")
-- or --
strrowvalue += CStr(DataGridView1.Rows(m).Cells(0).Value).Replace(vbCr,"")
-- or --
strrowvalue += CStr(DataGridView1.Rows(m).Cells(0).Value).Replace(vbLf,"")

How to delete a string from a RichTextBox?

I am trying to parse junk and narrow down a bunch of text. How do I delete the current line if a does not match? I would like to remove the line entirely:
For i As Integer = 0 To RichTextBox1.Lines.Length - 1
Dim a As String = RichTextBox1.Lines(i).ToString
If Not a = "SaveThisLine" Then
'delete the active line
End If
Next
Also how would I match partially? Such as if not a = "SaveThisLine" & * (to use a wildcard).
I would not touch original text and rather save valid lines into a StringBuilder, so if line is valid, AppendLine to it. In the end dump back into RichTextBox1.Text using StringBuilder.ToString.
For partial match in VB.NET you can use a native Like operator:
"aaa" Like "a*"
Returns True.
Or use regular expressions:
System.Text.RegularExpressions.Regex.Match("aaa", "^a").Success
Also returns True.
You can do it in this way to:
For i As Integer = 0 To RichTextBox1.Lines.Length - 1
If RichTextBox1.Lines(i) = "2" Then
RichTextBox1.Text = Replace(RichTextBox1.Text, RichTextBox1.Lines(i), "", , 1)
End If
Next

VB.NET For Each New Line

I need help making a function that opens a file and for each new line, make a variable like explode("\n", $var) in PHP. I tried
Dim words As String = GetFileContents(OpenFileDialog1.FileName)
For Each word As String In words
Dim doHash As String = MD5(word)
If String.Equals(doHash, hash.Text) Then
Label2.Text = "derp"
Else
Label2.Text = "lol"
End If
Next
but it makes each letter a new variable.
You want to use System.IO.File.ReadLines(OpenFileDialog1.FileName). That will cause the For Each loop to get each line separately.
Specifically:
For Each word As String In File.ReadLines(OpenFileDialog1.FileName)
Dim doHash As String = MD5(word)
If String.Equals(doHash, hash.Text) Then
Label2.Text = "derp"
Else
Label2.Text = "lol"
End If
Next
I notice that your php example is definitely splitting on newlines, but your loop variable is called word. Does your file have one word per line? Doesn't matter much, but I wanted to double check that you're okay with the loop getting each line, not each word (if there's more than one word per line).