Small Bug When When Appending Text To Multiline Text Box (C#) - c#

i have multiline text box in my form with existing text and I am trying to append text lines on new line and everything works fine but the first line always get added with the existing last line.
Example
text box holds this value
test1
and I am using below code to enter new line
txtMasterResults.AppendText(String.Join(Environment.NewLine, "line 2"));
txtMasterResults.AppendText(String.Join(Environment.NewLine, "line 3"));
and results looks like this
test1line2
line3
how can I fix the first line of textbox so I get the new text from secondline ?

I would retrieve the old string, remove any newlines at the end, and then append the new content. So like this:
txtMasterResults.Text = txtMasterResults.Text.Trim() + "\n" + newText

String.Join joins strings from an array using a delimiter. This is not what you want here.
Use:
txtMasterResults.Text += Environment.NewLine + "line 2" + Environment.NewLine + "line 3";
Note that
txtMasterResults.Text += "something"; is the same as
txtMasterResults.Text = txtMasterResults.Text + "something";

Related

Word count not working when entering new line

I've been looking at other stack overflow articles regarding similar issues when it comes to word count in C#, but none have helped me when it comes to the pickle I've encountered.
I have a textbox that inputs text from a text file. The text is split into three lines by me pressing the enter button to create a new line in a text file. The text reads:
It's a test to see "if" the
application_for Top Image Systems
actually work. Hopefully it does work.
Now as you can see there should be 17 words, however my word count only says 15. I have realized after a bit of trial and error that the issue must be the fact it's in a new line. Every time it goes to a new line, it thinks the last word of the previous line and the first word of the new line are together as a word (or that's what I think the program is thinking).
My question is with the code I have below, how can I get to recognize that if there is a new line, that it should split the words like a space?
Below is my code:
string nl = System.Environment.NewLine;
//Missing Code to read text file which I don't need to include in this example
do
{
textLine = textLine + txtReader.ReadLine();
}
//Read line until there is no more characters
while (txtReader.Peek() != -1);
//seperate certain characters in order to find words
char[] seperator = (" " + nl).ToCharArray();
//number of words
int numberOfWords = textLine.Split(seperator, StringSplitOptions.RemoveEmptyEntries).Length;
txtReader.ReadLine(); strips your newline away.
From the msdn:
The string that is returned does not contain the terminating carriage return or line feed.
so you have to add it manually (or just add a space)
textLine = textLine + txtReader.ReadLine() + " ";
consider using the StringBuilder class for repeated concatination of strings.
Edit:
To get the character count as if the spaces were never added, do:
int charCount = textLine.Length - lineCount;
where lineCount an integer that you increment every time you add a space in your do-while loop:
int lineCount = 0;
do
{
textLine = textLine + txtReader.ReadLine();
lineCount++;
}
I'm a bit of a beginner myself, sorry if this is not a great answer, but I've just done a bunch of text stuff in c# and I'd probably approach by replacing the line breaks which will show up as "\n" or "\r" in your original string with a space, " " - something like:
nl = nl.Replace("\r", " ");

Replacing newline in multiline textbox with <br/>

I am trying to use a multiline textbox to insert text into a table which is displayed as html. I want to with javascript take the text inside of the textbox and find where the user pressed enter and place "<br/>" in that position so when the text is displayed it will show line break. Any ideas on how I could do this?
I tried something like this but it did not work.
var text = document.getElementById("announcementid").value;
var newtext = text.replace("\n", "<br/>");
text = newtext;
The newtext variable ends up being a copy of the original string from your announcementid element. Thus, you'll need to re-set the value property on the original document element:
var text = document.getElementById("announcementid").value;
var newtext = text.replace(/\n/g, "<br />");
document.getElementById("announcementid").value = newtext;
Also, as Konstantin pointed out, the replace() function in Javascript will just replace the first instance unless you pass in a global regular expression.
Fiddle example
Text is a primitive so it would not replace the value. Use a regular expression to globally replace instead of replacing just the first instance:
var text = document.getElementById("announcementid").value;
var newtext = text.replace(/\n/g, "<br />");
document.getElementById("announcementid").value = newtext;

How to add a line to the Text property of a textbox?

I have TextBox in a Windows form application. And I write a text in it.
eg.
texbox.Text = " first line ";
....
textbox.Text = "second line";
When I write second text, the first line is deleted. How to leave the first line and write next texts in next line in the TextBox?
I want the following result:
first line
second line
textbox.text = "first line";
textbox.text += "\nsecond line";
or
textbox.text = "first line";
textbox.text = textbox.text + "\nsecond line";
You want to change the TextMode property to MultiLine
then you can write like
texbox.text = " first line ";
....
textbox.text += "\nsecond line";
Please note the append operator += and \n which is new line character
I usually write a wrapper.
One important difference is to use
Environment.Newline
instead of
"\n\r".
Also, as others have noted, set the textBox multiline property.
public void WriteLine(string msg)
{
if (!string.IsNullOrEmpty(textBox.Text))
{
msg = string.Format("{0}{1}", Environment.NewLine, msg);
}
textBox.AppendText(msg);
}
You can set the Textbox multiline property to true and can use \r\n for multiline text like below:
TextBox1.Text = "First line\r\nSecond line";
You can do following ways also.
textbox.text = "first line";
textbox.text = textbox.text + vbCrlf + "second line";

C# : Printing variables and text in a textbox

I need to know the command that I can print a sentence like "the item Peter at row 233 and column 1222 is not a number " .
I far as now I have made this:
string[] lineItems = (string[])List[]
if (!Regex.IsMatch(lineItems[0], (#"^\d*$")))
textBox2.Text += " The number ,lineItems[0], is bigger than
10 " + Environment.NewLine;
I want to print the array fields that have error. So if it finds something it will print it.
I made a code that correctly prints that there is an error on this line of the array, but I cant print the item of the array.
I need to have an Environment.NewLine because I will print many lines.
Thanks ,
George.
foreach (int lineNumber in lineItems)
{
if (lineNumber > 10)
textBox2.Text += "The number " + lineNumber + " is bigger than 10\n";
}
Something like this should work, (I have not checked the c# code, I am working on a mac at the moment)
TextBox2.Text="This is FirstLine\nThis is Second Line";
The code is not compilable absolutely, but I may be understand what you're asking about.
If you are asking about how to compose the string of text box, by adding new strings to it, based on some desicional condition (regex), you can do folowing, pseudocode:
StringBuilder sb = new StringBuidler();
if (!Regex.IsMatch(lineItems[i], (#"^\d*$")))
sb.Append(string.Format(The number ,{0}, is bigger than 10, lineItems[i]) + Environment.NewLine);
textBox2.Text = sb.ToString();
If this is not what you want, just leave the comment, cause it's not very clear from post.
Regards.

Add spacing to textbox results

Hi there I have the following code-
richTextBox1.Text = richTextBox1.Text + action + "ok: " + ok.ToString();
richTextBox1.Text = richTextBox1.Text + "err: " + err.ToString();
richTextBox1.Text = richTextBox1.Text + "\r\n";
textBox1.Text = textBox1.Text;
The results look like -
ok:7err:0
But I want-
ok:7
err:0
With spacing, to make it look better how can I do this?
You could add another 2 lines:
richTextBox1.Text += Environment.NewLine;
richTextBox1.Text += Environment.NewLine;
between your "ok" and "err" - assuming you want a blank line between the two lines of output. However, you should either be using string.Format or a StringBuilder to create your output as concatenating strings this way in inefficient.
You also don't need the final:
textBox1.Text = textBox1.Text;
as that is just setting the text box contents back to itself and does nothing.
You've already got your answer, you just have it in the wrong place! The key is to use the escape sequence \r\n, which inserts a carriage return and a new line.
Also, there's no reason to split this code up into multiple lines. You end up incurring a performance penalty for doing so. It's better to do all of the string concatenation at one time. (You aren't doing enough concatenations here to justify using the StringBuilder class, but it's worth keeping in mind that strings are immutable in .NET and writing code accordingly.)
Try rewriting the code like this:
textBox1.Text = textBox1.Text + action + "ok: " + ok.ToString(); + "\r\n" +
"err: " + err.ToString(); + "\r\n";
You can also complete eliminate the last line of code, as that simply sets the value of textBox1.Text to itself. It's a no-op, meaning that it does nothing at all.
first that you could do all these in a single statement, second you could use += operator instead, and third what is that last statement doing?! it not needed, fourth add "\n" after each part you need there is no limit where you should put it, no "\r" needed.

Categories