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

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)

Related

Change selected ComboBox item, from text file string not working

Ok.
So I am working on a project, irrelevant, and I have a bunch (8) of ComboBoxes (they are in DropDownList mode) and there is 8 save files. I have them being imported and converted to strings:
Using class2 As New StreamReader(path & "SaveData/classdata/classdata2.NIC")
Dim fdcount1 As String
fdcount = class2.ReadToEnd()
MessageBox.Show(fdcount1)
hr2choice.SelectedItem = fdcount1
End Using
I already tested this, and it seems to be working.
(Test code I used:)
MessageBox.Show(fdcount1)
and it showed the value ("DiVita")
Despite this, when I tried setting the ComboBox value to this, it did not seem to work.
The ComboBox does have this value in it, and if I try this, it works:
hr2choice.SelectedItem = "DiVita"
For whatever reasons though, it does not work when I try doing it directly from the string.
Thanks for any help with this!
Nic
To answer this, I have to assume that the data in the text file is formatted as one line for each piece of data.
There seems to be a couple of issues with your code. fdcount is just declared as a string where it should be an array to make it easier to access each line that is read from the file. fdcount1 has no relationship to fdcount - it is a completely separate entity, so the data in fdcount1 is coming from somewhere else.
Rather than the above code, It's easier to use this
Dim fdcount() As String
fdcount = File.ReadAllLines("SaveData/classdata/classdata2.NIC")
MessageBox.Show(fdcount(1))
Note that fdcount is declared as an Array of String. The 2nd line does all the opening, reading into the array, and closing of the file.
You can then access each element of the array as shown in the 3rd line.

How do you get a Textbox to Read line by line by label.text

Okay so I fell into a loop and got stumped. I have been trying many ways to try and get a VB.net (Label.text) to = each independent line entered. Is there an easier alternative that will not come up with warnings? Just Curious. I could use a Listbox to Manage the Items and use those lines instead of a Textbox in Multi-lined, Just wondering how to get multi line function that reads text from a textbox to a label.text if many lines are entered.
For Example.
If ProgressBar1.Value = 0% Then Label10.Text = (TextBox1.Text + vbNewLine)
Problem with this is that it Addes it all into the Label. Id like a way to have line by line be shown by the Label.text. Sorry If this is coming off confusing to any of you haha.
Thanks
~Tom
First of all you need to make sure that your Label has property 'AutoSize', which must be set to 'False'
Create a string array by spliting the textbox.text using Environment.NewLine.
Then you can select the proper line to display on the label from that array depending on your business logic.

String Manipulation Inconsistency

This is a more general question. I am reading a document and saving its contents into a string variable. The resulting variable contains approximately 1 million characters (no cleansing). My code would then search the string, and extract key words. However, I am hung-up on an issue:
If I pass the string directly to a message box, it will show me the contents using Mid:
Messagebox.Show(Mid(searchString, startPos, endPos))
However, if I first pass the mid to a string variable, the contents are empty and the messagebox displays nothing:
Dim myString as String
myString = Mid(searchString, startPos, endPos)
Messagebox.Show(myString)
The same effect happens when I use .substring and when I use a stringbuilder.
Does anybody know why this is happening? I assume something is happening during assignment, but I am not sure what is lost?
Here is a snippet of code:
searchPos = textString.IndexOf(searchText, searchPos, StringComparison.OrdinalIgnoreCase)
MessageBox.Show(searchPos)
MessageBox.Show(Mid(textString, searchPos, 100))
So, the inconsistency is as such: the length of textString is around 3,700,000 characters. When I find the indexOf, the value returned in the first Messagebox is 455,225. However, if I try to pull out the characters using Mid, the second messagebox is blank.
Also, although it claims to be 3,700,000 characters, if I do a messagebox on textString, I am only shown around 6 characters of what appears to be XML. The file that is being searched is an old .ppt file, and I know I can just work-around it, but I am confused by how the computer can find the indexof my searchText correctly, but then cannot show me anything.
Thoughts?

Easy way to write a string with control characters to an nvarchar field in a DB?

EDIT: Accepted answer points out what my issue was. I added another answer which shows an even easier way.
I have a multiline textbox from which I create a single string:
Dim wholeThing As String
Dim line as String
For Each line In txtMultiline.Lines
wholeThing = wholeThing & line & Environment.Newline
Next
What I'd like to do is write this to a DB as-is by adding wholeThing as a parameter using to SqlCommand.Parameters.AddWithValue, but all of my painstakingly-inserted newlines go away (or appear to).
I saw this and was hoping I didn't need to concern myself with CHAR(nnn) SQL stuff.
Any suggestions? Thanks!
What do you mean when you say "appears to"? A newline character can be easily stored in an nvarchar field. What tools are you using to verify the results of your actions?
It's even easier than this. As per the discussion in the accepted answer, I wasn't seeing all of the lines in my view. Putting up a messagebox showed everything.
What's more, I don't need to take things line by line:
wholeThing = txtMultiline.Text
works, too, and it keeps all of the line breaks.

Remove paragraph mark when copying from excel cell

Please help me copy a string from a listbox when a user hits ctrl+c. I was using the dataobject but for some reason this worked perfectly some times and gave me an error message other times. If you know why this is, stop reading, as the rest of this question is not necessary.
Now I am putting this in a worksheet cell and using range.copy, however, when the string is pasted into a textbox, it retains the paragraph mark that excel seems to put at the end of every cell! Just to make things fun, the paragraph mark cannot be removed by using Left() - it takes everything but the paragraph mark. (Paragraph mark below is represented by P).
s = "stringP"
s = Left(s,len(s)-1)
print s
returns: strinP
Has to be something simple I'm missing.
I haven't tested this but have you tried chopping two characters?
I'm sure it's \r\n or carriage-return + line feed, not just \n you need to chop.
Have you tried trim() function ?
And why do you have to use Range.copy?
Can't you just assign textbox1.value = Range("A1") ?
It works fine without any bugs.