Textbox not using vbCrLf unless converted to vbNewLine - sql

I have a StoredProcedure that returns a string.
In the string I put CHAR(13) and CHAR(10) to have carriage return and line feed.
I read the string from a winform in VB.NET and the string correctly has a "VBCrLf" in it. When I look at the string during debug (you know, using the little magnifing glass) the lines are correctly there.
THEN: I put the string in the text of a Multiline Textbox and the lines disappear, everything is on a single line.
If I then apply:
myString.Replace(vbCrLf, vbNewLine)
and put it back in the same textBox, everything works fine again.
How can avoid the replace?
Thankx

Related

How to tell if carriage return is in my string?

I have a textbox control that allows for the user of enter button to enter details like ADdresses or other demographic information. Since the default for addresses are as follows:
Address 1
Address 2
City, st
Zip
I am wondering if there is a way to tell if the Enter key was used to make a new line here? I've looked around and currently the only is to have a check in VB for vbCrLf however I'm not seeing it pick this up in the code.
Test data for this would be something similar to below
123 N Street
S Test Street
Test City, XX
91883
The code below is what I'm trying to just replace any return carriage and replace with a space
Text.Replace(vbCrLf, " ")
Will this vbCrLf not pick up a carriage return unless there's an actual space between the above test values?
If you are using a MultiLine textbox (as it seems from your sample) then you don't need to search for the newline characters and replace them with a space.
You could simply use the Lines property where every line is stored separated from the other and then use the string Join method to create a single line string
Dim singleLine = string.Join(" ", myTextBox.Lines)
Of course if you are just interested to know if there is a newline character then just check the Length property of the Lines array
vbCrLf actually refers to two characters, a carriage return (13) and a line feed (10).
I would search and replace each separately. It isn't strictly necessary (as replace will work on the two characters at once), but can catch instances in which the user cut and pasted information, instead of typing directly into the text box.
Text = Text.Replace(vbCr, " ")
Text = Text.Replace(vbLf, " ")
or
Text = Text.Replace(vbCr, " ").Replace(vbLf, " ")
https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.constants.vbcrlf(v=vs.110).aspx
The Replace() function will indeed properly detect and replace all occurrences of the target with the replacement - it does not matter whether there are leading or trailing spaces.
However, please consider that String objects are immutable and cannot be changed after they have been instantiated. Therefore, Replace() does not modify the existing object but rather returns a new string as its result.
To actually see the results of the function call, you need to do something along these lines:
newString = Text.Replace(vbCrLf, " ")
I spent quite some time to resolve exactly this problem, the solution I came across was:
text = text.Replace(" ", ControlChars.CrLf)
Sorry cant remember where I found the solution but if I do remember it I will post the link here.

Read a text file and display result in a window

I have a text file which contains about 60 lines. I would like to parse out all the text from that file and display in a window. The text file contains words that are separated by an underscore. I would like to use regular expression to solve this problem.
Update:
This is my code as of now. I am trying to read "filename" in my code.
Dim filename = "D:\databases.txt"
Dim regexpression As String = "/^[^_]*_([^_]*)\w/"
I know I don't have much done here anyway but I am trying to learn VB on my own and have gotten stuck here.
Please feel free to suggest what I should be doing instead.
Something like this:
TextBox1.Lines = IO.File.ReadAllLines("fileName")
To remove underscores:
TextBox1.Lines = IO.File.ReadAllLines("fileName").Replace("_", String.Empty)
If you also need other special characters removed, you can use Regex.Replace:
Remove special characters from a string
Also on MSDN:
How to: Strip Invalid Characters from a String
Or the old school way - loop through all characters, and filter only those you need:
Most efficient way to remove special characters from string

how to remove white spaces except vbCr/vbCrlf/newline in vb.net

I am currently using this code to remove to much new lines from an explode string..
Me.rtb.Lines = Me.rtb.Text.Split(New Char() {ControlChars.Lf}, _
StringSplitOptions.RemoveEmptyEntries)
I use RichTextBox. I split those string with
incoming = stringOfRtb.Split(ControlChars.CrLf.ToCharArray) ''vcrlf splitter
as a result, i get strings per line.. but sometimes, I think the first code removes not only the white spaces, but also the vbCrlf or the newlines that the module sends back. now the awful thing here is, it appears on random places so the strings that I put into textboxes shuffles and gets other arrays every time I receive the same data.
sometimes, its like this..
While rtb.Text.contains("vbLf")
rtb.txt = rbt.txt.replace("vbLfvbLf","vbLF")
end while
this code should make double chars into one.

How do I retain TextBox line breaks in Winforms after assigning text to a string variable?

I have a WinForms app with a multi-line textbox. This displays and retains (after loading from the DB) line break characters fine.
However if I assign the TextBox.Text value to a string variable and then re-assign the variable back to the TextBox.Text property, the line break characters are lost and replaced with a square character (can't past them here as they just paste as a line break!)
I've tried:
Replace("\n",vbcrlf)
but to no avail.
Can anyone suggest a solution?
UPDATE**
I haven't managed to fix this but have worked around it by avoiding the variable assignment. I now pass a reference to the RichTextBox and directly manipulate the text there. Note that this seems specific to the RichTextBox as I don't see the issue with a normal TextBox.
Instead of Replace("\n"' vbcrlf), you need, to use either Replace(vblf, vbcrlf), or Replace(vbcr, vbcrlf).
Just an idea:
How about fixing ā€œ\r\nā€ instead of ā€œ\nā€?
Are you transforming the string anyway? Since something like this:
Dim s As String = TextBox1.Text
TextBox1.Text = s
works well.
textBox.AppendText("your new text" & Environment.NewLine)

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.