How to prevent line feeds (vbLf) and carriage returns (vbCr) in text boxes in a vb.net text file? - vb.net

How to prevent line feeds (vbLf) and carriage returns (vbCr) in a text box that should only be one line, in a text file? It can happen that these carriage return characters and these new line characters are present (for example during a copy and paste) in the body of the text and thus make the reading of the file impossible. This has happened to me before, and I think I need to set up a check for the presence of these characters and thus warn the user to check his line before any recording. Thank you in advance. Claude.

Replace the CR and LF with nothing?
textBoxWithCrLf.Text.Replace(vbCr, "").Replace(vbLf, "")
Note, this doesn't modify the contents of the textbox, it generates a new string with no CRLF, so you'd use it like:
somefilewriter.SomeWriteStringMethod( textBoxWithCrLf.Text.Replace(vbCr, "").Replace(vbLf, "") )

Related

VB.NET - Appending text to the last line of text file leaves (sometimes) an empty line

The intention of the software I am coding is to append a string to the last line of a text file. The problem I am experiencing is that depending on the situation, sometimes I get an empty line when I try to append text to the last line of a text file.
My text file is very simple. It contains a few lines of text and each line contains a few words. When I open the text file with Windows' Notepad (without editing the content of the text file) and I move the caret to the end of the entire file, sometimes the caret is positioned at the end of the last line with content (text), and sometimes it is positioned at an empty line that doesn't contain even a white space.
When the caret's ending position corresponds to the last char of a line that contains text, I don't experience any problem appending some text programmatically. The programmatically appended line is at a new line and there isn't any empty line between the old last line and the current last line in the text file.
The problem appears when I can move the caret in the text file to the last possible position and that position corresponds to an empty line. In that case, when I try to append a new line programmatically, I can see an empty line before the just added line.
I have tried different codes and techniques, but it always happens the same to me.
The code I am actually using is:
If stringLastLine.Length = 0 Then
Using sw As StreamWriter = New System.IO.StreamWriter(filePath, True)
sw.Write("Inserted text")
sw.Close()
End Using
ElseIf stringLastLine.Length > 0 Then
Using sw As StreamWriter = New System.IO.StreamWriter(filePath, True)
sw.Write(Environment.NewLine & "Inserted text")
sw.Close()
End Using
End If
Basically, I am trying to detect if the last line length is over zero, so it's a line with text content and I will use "environment.newline + string". If it's a line without content (length = 0) I add directly the string without using "environment.newline". In the case that length = 0 I always get an empty line in between.
Any idea to solve this problem?
How about trimming your text to ensure there's no end of line characters at the end (the whole text)? Right now you might have an issue with multiple blank lines:
text = text.TrimEnd({Convert.ToChar(10), Convert.ToChar(13)})
This handles the EOL characters.

Visual Basic pulling a character from a certain line number

I am working on a macro that needs to be able to check and make sure that a character on a certain line is what it should be before it finishes the form.
Where or how do i find the information to do this?
I have tried using the command "Left(#of line ,1)" and am not getting it to return anything at this point.
I Assume that you have this text in a string. Comment to my answer if wrong.
You can use a Regex to compare to string with your character like so :
If Regex.IsMatch(MyString, *.{X}Y) Then
'You have to manually replace X for the number of characters before the character you scan for
'You have to manually replace Y for the-said character it is supposed to be
Look at http://www.codeproject.com/Articles/9099/The-Minute-Regex-Tutorial for more info on Regex.
If you want to only take one line in your string :
TheLine = split(MyString,Environnement.Newline)([Line Number])

write new line to file

I'm trying to write to file newline. I'm using WS_DOWNLOAD but have no idea how to write new line into text.
Anyone knows ?
Set a character type field to cl_abap_char_utilities=>cr_lf and insert that in the appropriate places (wherever you want a new line) to your output.
This contains the carriage return & linefeed characters.

Replace a 'bel' character in a file in vb.NET

I've come across a character in one of my data feeds, which I have never encountered before
The images above are the data feed in Notepad++ and Notepad view. As you can see it appears as 'BEL' in Notepad++ and a sort of 'bullet point' in Notepad.
How would I go about replacing this character in vb.NET?
I've tried a simple replace in a SSIS Script Task by copying and pasting the character into the replace function, e.g.
text = text.Replace("copy and pasted character", "")
and this gives this error
All help is extremely appreciated,
Thanks
I’ve got no idea what SSIS is but since you wanted to know a solution in VB.NET, the code you’ve tried will work here. That is:
text = text.Replace("copy and pasted character", "")
will work just fine in VB. Alternatively, you can use the following:
text = text.Replace(Chr(7).ToString(), "")
Find out what the Ascii value of the character is and then use the Chr function to eliminate it
i.e.
text = text.Replace(Chr(n), "")
[Bell] is probably character 7

How do I remove the box symbol that shows up for a line break when I output a string to a cell?

I have a userform where the user enters a description of their project into a textbox and it is stored in a string. When the user runs the "Generate Report" routine that string is output to a single cell. If the user entered line breaks, they are present in the output, but there is also a "box" character for each line break. I can manually delete the "box" character and the line break is still there in the cell, but how do I prevent the "box" character from showing up in the first place?
Your string is probably carrying a vbCrLf (Carriage Return + Line Feed). All you need to do is remove the CR of the CR+LF.
Try this:
(YourCell.Value Here) = Replace(YourString, vbCrLf, vbLf)
Good luck.