I'm currently pooping out, I am having trouble displaying lines from a text document. What I mean is whenever I press enter it displays a new line of the text document.
Thanks
This is I guess some code, this is referenced to a text document and received a definition, from here it receives it and gets it ready to display but I just need it to read from say result one line at a time every time I press enter
First line wont work this is the first line ->>>>
var result = GetLinesWithWord(i1, #"" + Path + "/dict2.txt");
// Display the results.
foreach (var line1 in result)
{
//word maxlength
const int MaxLength = 82;
var name1 = line1;
if (name1.Length > MaxLength)
name1 = name1.Substring(0, MaxLength);
Console.WriteLine(name1 + "\r");
string boc1 = name1;
string foc1 = i1;
System.IO.File.AppendAllText (#"" + Path + "/" + n + ".txt", foc1 + "\n" + boc1 + "\n");
If I understand you correctly (especcialy your comment to #MairajAhmad) you want to dump the text file line by line, waiting for the user to press a key after each line.
Look at the post here. I think, that is what you need. It's basically a call to Console.ReadKey(true). The mentioned post listen's for the escape key, but it should still illustrate what to do.
Related
I have a problem with somwthing about replace char. I tryied a lot of links but get the same problem to replace (\\) to (\)
here is my code:
string mystringA = textBox.text
string mystringB = mystringA.Replace("\\", "\"");
The result of mystringB stay the same as mystringA.
I am saying because I put a debug mode to see the result
My textBox.txt = C:\Users\Braulio Jose\Desktop\impressora\myfoto.png
I have to replace the double quotes because I want to delete this photo in another place but when I follow the path, mystringA put another quote, and I this path don't exist
I am using visual studio 2013 and C# Language.
Some help. thank you
Due to the fact, that your question is about quotes, but your code is about slashes, it's hard to guess what your real problem is.
But here is some sample code for both replacements:
var replaceQuotes = "Some text with \"\"double quotes\"\"";
var replacedQuotes = replaceQuotes.Replace("\"\"", "\"");
Console.WriteLine("Before: " + replaceQuotes);
Console.WriteLine("After: " + replacedQuotes);
Console.WriteLine();
var replaceSlashes = "Some text with \\\\double slashes\\\\";
var replacedSlashes = replaceSlashes.Replace("\\\\", "\\");
Console.WriteLine("Before: " + replaceSlashes);
Console.WriteLine("After: " + replacedSlashes);
And here the output:
Before: Some text with ""double quotes""
After: Some text with "double quotes"
Before: Some text with \\double slashes\\
After: Some text with \double slashes\
Clarification: I want to output line of text to the same "position" in a RichTextBox, replacing the previous line.
In C# Windows Forms Application trying to use RichTextBox for displaying messages. Most of the messages are appended, so that is fine but at one point in the program it has a counter, showing the amount of rows processed. For example like this:
Processed: 001 Records.
etc
well ... I don't need it to fill the RichTextBox with a thousands of lines like this:
Processed: 001 Records.
Processed: 002 Recoeds.
Instead I am trying to move the Caret to a start of the line and write the line again. Probably need to remove the previous line in a RichTextBox. Can't figure out how to always write to the same last line in RichTextBox.
I tried to use SelectionStart and ScrollToCaret() that did not work.
You could try something like this (rtb is your RichTextBox variable)
// Get the index of the last line in the richtextbox
int idx = rtb.Lines.Length - 1;
// Find the first char position of that line inside the text buffer
int first = rtb.GetFirstCharIndexFromLine(idx);
// Get the line length
int len = rtb.Lines[idx].Length;
// Select (Highlight) that text (from first to len chars)
rtb.SelectionStart = first;
rtb.SelectionLength = len;
// Replace that text with your update
rtb.SelectedText = "Processed: " + recordCount + " Records.";
No error handling added, but you could add some checks to be sure to stay inside the text buffer
One solution would be to store the current text before you start processing:
string oldText = richTextBox.Text;
for (int i = 0; i < X; i++)
{
// process stuff
richTextBox.Text = oldText + Environment.NewLine + "Processed: " + i + " Records.";
}
I believe that this method disregards the RTF data though, so you might use RichTextBox.Rtf instead.
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", " ");
I'm reading a field On a table it only has 3 values ("",ESD,R&S)
I don't know exactly why, but when I read the R&S value, the print out label is R ("empty space") S
this is the code I'm using:
char[] area = read1[8].ToString().ToCharArray();
// if array is less than one do nothing
if (area.Length > 1)
{
//trying to use this to check if the second item of array is the "&" symbol (print this format data)
if (area[1].ToString() == "&")
{
Arealbl.Text = area[0].ToString() + "\n" + "&" + "\n" + area[2].ToString();
}
//else print out this format data
else
{
Arealbl.Text = area[0].ToString() + "\n" + area[1].ToString() + "\n" + area[2].ToString();
}
}
I using this code because I haven't found an easy way to put a label on vertical.
The & is a special char in MenuItems, Labels and Buttons, used to indicate that the next char should be underscored. When you manage to focus Arealbl and hit Alt you might see that.
Set
Arealbl.UseMnemonic = false;
somewhere. Like with the designer.
In addition to #Henk Holterman's answer, here are a few code review suggestions. You can access a string as an array, so there is no need to .ToString().ToCharArray(), just to .ToString() everything further down the method. Simplifying the concatenation to a string.Format can help improve readability and assuming you don't have to do this a large number of times (tens of thousands) it shouldn't impact performance.
string area = read1[8].ToString()
if(area.Length < 3) { return; } //exit early on error conditions.
// if array is less than one do nothing
Arealbl.UseMnemonic = false; //only add this if you cannot guarantee it will be set.
Arealbl.Text = string.Format("{0}\n{1}\n{2}", area[0], area[1], area[2]);
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.