I want to display some bold and some simple content in the form so I am using richtextbox.And I have made one file with extension of .rtf.Now I load that file in the richtextbox with use of the Loadfile() function.This works. But I want to display particular content of the file in the richtextbox,like may be first five lines or it may be line no. of six to ten.Then is there any solution ??
It is possible, just not very cleanly. This code uses another RTB to load the file and the clipboard to get the formatted RTF. Beware that it destroys the clipboard content.
using (var helper = new RichTextBox()) {
helper.LoadFile(#"c:\temp\test.rtf");
// Copy line #6
int startRange = helper.GetFirstCharIndexFromLine(5);
int endRange = helper.GetFirstCharIndexFromLine(6);
helper.SelectionStart = startRange;
helper.SelectionLength = endRange - startRange;
helper.Copy();
}
richTextBox1.SelectAll();
richTextBox1.Paste();
This doesn't preserve any formatting, but shows how you could manipulate the Lines array. It looks like the RichTextBox selfishly keeps all RTF codes to itself and only exposes text through Lines:
var fromStart = new string[richTextBox1.Lines.Length - start];
Array.Copy(richTextBox1.Lines, start, fromStart, 0, fromStart.Length);
var lineSet = fromStart.Take(count).ToArray();
richTextBox1.Lines = lineSet;
start and count are passed into this function that selects a set of lines.
Solution using ReadAllLines:
string[] lines = File.ReadAllLines(filename);
int startLine = lines.IndexOf(startMarker);
int endLine = lines.IndexOf(endMarker);
if (startLine == -1 || endLine == -1)
{
// throw some sort of exception - the markers aren't present
}
string[] section = new string[endLine - startLine - 1];
Array.Copy(lines, startLine + 1, section, 0, section.Length);
richTextBox.Rtf = string.Join("\r\n", section);
Solution using ReadAllText:
string text = File.ReadAllText(filename);
int startIndex = text.IndexOf(startMarker);
int endIndex = text.IndexOf(endMarker, startIndex + startMarker.Length);
if (startIndex == -1 || endIndex == -1)
{
// throw some sort of exception - the markers aren't present
}
richTextBox.Rtf = text.Substring(startIndex + startMarker.Length,
endIndex - startIndex - startMarker.Length);
Both of these assume that you really have got a complete RTF document in that section of the file though - you may well find you need additional header text, for example. Also, both assume the file is in UTF-8. I don't know enough about the RTF format to know if that's correct.
Did you tried the Lines property? It allows set / get string array as the RichTextbox content.
Related
I have a search function that searches keywords in a block of text and displays a truncated version of the results. My problem is that it will not show the searched keyword if its near the end.
For example.
Text = "A block of text is text that is grouped together in some way, such as with the use of paragraphs or blockquotes on a Web page. Often times, the text takes on the shape of a square or rectangular block"
I search for "times" with
text = text.Substring(0, 100) + "...";
It will return
"A block of text is text that is grouped together in some way, such as with the use of paragraphs or..."
Is there a way to return 100 characters before and after the searched keyword?
You can do this,
string s = "A block of text is text that is grouped together in some way, such as with the use of paragraphs or";
string toBeSearched = "grouped";
int firstfound = s.IndexOf(toBeSearched);
if (firstfound != -1 )
{
string before = s.Substring(0 , firstfound);
string after = s.Substring(firstfound + toBeSearched.Length);
}
DEMO
string s = "A block of text is text that is grouped together in some way, such as with the use of paragraphs or";
string wordtoSearch = "block";
int firstfound = s.IndexOf(wordtoSearch);
// If the index of the first letter found is greater than 100, get the 100 letters before the found word and 100 letters after the found word
if (firstfound > 100)
{
string before = s.Substring(firstfound , firstfound-100);
string after = s.Substring(firstfound + wordtoSearch.Length, 100);
Console.WriteLine(before);
Console.WriteLine(after);
}
//// If the index of the first letter found is less than 100, get the letters before the found word and 100 letters after the found word
if(firstfound < 100)
{
string before = s.Substring(0, firstfound);
Console.WriteLine(before);
if(s.Length >100)
{
string after = s.Substring(firstfound + wordtoSearch.Length, 100);
Console.WriteLine(after);
}
else
{
string after = s.Substring(firstfound + wordtoSearch.Length);
Console.WriteLine(after);
}
}
You can do something like this as well, making it a bit more reusable and able to match multiple instances of the keyword
string input = "A block of text is text that is grouped together in some way, such as with the use of paragraphs or blockquotes on a Web page. Often times, the text takes on the shape of a square or rectangular block";
int buffer = 30; // how much do you want to show before/after
string match = "times";
int location = input.IndexOf(match);
while (location != -1) {
// take buffer before and after:
int start = location - Math.Min (buffer , location); // don't take before start
int end = location + match.Length
+ Math.Min( buffer, input.Length - location - match.Length); // don't take after end
Console.WriteLine("..." + input.Substring(start, end-start) + "...");
location = input.IndexOf(match,location+1);
}
Giving you an output of
...A block of text is text that is gro...
...with the use of paragraphs or blockquotes on a Web page. Often ...
...pe of a square or rectangular block...
I have a code where I am iterating through every paragraph present in a word document with the use of the Primary Interop Assemblies. What I am essentially doing is extract all the text from each paragraph into a string. Then I searching that string for specific key words/phrases. If it is present it is swapped with something else. Then the paragraph is inserted back into the document.
This works perfect however on some documents what is happening is a new line is being added in between the paragraphs. Upon further investigation it turns out that the paragraph formatting is being altered, that is the line spacing after is increasing from zero to 12 and other things change as well, these include left indents is being removed from paragraphs etc.
I would like to know if there is any way to perform the above task without having the paragraph properties change when inserting the text back. My code is included below in order to show how I am iterating through the document.
Before getting to the main code I do have a word application and document open using the following namespace:
using Word = Microsoft.Office.Interop.Word
and then the following code
Word.Application app = new Word.Application();
Word.Document doc = app.Documents.Open(filePath, ReadOnly: false);
After opening the document I have done the following:
try
{
int totalParagraphs = document.Paragraphs.Count;
string final;
for (int i = 1; i <= totalParagraphs; i++)
{
string temp = document.Paragraphs[i].Range.Text;
if (temp.Length > 1)
{
Regex regex = new Regex(findText);
final = regex.Replace(temp, replaceText);
if (final != temp)
{
document.Paragraphs[i].Range.Text = final;
}
}
}
} catch (Exception) { }
Some things to note is that I have a if statement with "temp.Length > 1". I noticed that is there is nothing but a blank line, it is still counted as a paragraph and the text present inside that paragraph is of length one. When working with blank lines this actually adds in an extra line again when inserting it back in even if no replacements were done. So in order to combat this I simply used this to make sure the paragraph has at least one letter in it and is not just a blank line. This way no additional blank lines are added in between paragraphs.
I have found the answer to my own question. I have included the solution down below in case anyone else is having the same problem or would like it for reference.
What you have to do is get the paragraph format properties of the extracted text before any changes were made. Then once the paragraph is inserted back in, set the same properties we previously extracted to the inserted paragraph to counter any changes that may have been made. The full code is included below:
try
{
int totalParagraphs = document.Paragraphs.Count;
string final;
for (int i = 1; i <= totalParagraphs; i++)
{
string temp = document.Paragraphs[i].Range.Text;
float x1 = document.Paragraphs[i].Format.LeftIndent;
float x2 = document.Paragraphs[i].Format.RightIndent;
float x3 = document.Paragraphs[i].Format.SpaceBefore;
float x4 = document.Paragraphs[i].Format.SpaceAfter;
if (temp.Length > 1)
{
Regex regex = new Regex(findText);
final = regex.Replace(temp, replaceText);
if (final != temp)
{
document.Paragraphs[i].Range.Text = final;
document.Paragraphs[i].Format.LeftIndent = x1;
document.Paragraphs[i].Format.RightIndent = x2;
document.Paragraphs[i].Format.SpaceBefore = x3;
document.Paragraphs[i].Format.SpaceAfter = x4;
}
}
}
} catch (Exception) { }
I'm using C# (unity3d).
I have a string containing lines separated by \r\n, and I need to remove the last line.
z
I managed to make something work but it takes 3 lines, I feel that maybe there could be something shorter in C# to do that.
Here is my current code :
int index = infoTxt.text.LastIndexOf("\r\n");
infoTxt.text = infoTxt.text.Substring(0, index - 2);
infoTxt.text = infoTxt.text.Substring(0, infoTxt.text.LastIndexOf("\r\n")+2);
Is this efficient enough to be used often, with a text string growing as time goes on ?
This is for a console-like system.
You can achieve this by simply doing:
var trimmed = text.Substring(0, text.LastIndexOf("\r\n"));
Tested with:
var text = File.ReadAllText("data.txt"); // "row1\r\nrow2\r\nrow3"
var trimmed = text.Substring(0, text.LastIndexOf("\r\n")); // "row1\r\nrow2"
Your double sub-stringing suggests your data ends with an empty line. If so then you need the second last index of it:
string value = "\r\n";
var text = File.ReadAllText("data.txt"); //"row1\r\nrow2\r\nrow3\r\n"
int lastPosition = text.LastIndexOf(value);
var trimmed = text.Substring(0, text.LastIndexOf(value, lastPosition-1)); // "row1\r\nrow2"
Last - see #serge.karalenka suggestion to use Environment.NewLine instead of "\r\n"
If you have and option of no new lines then you should:
var trimmed = text.Substring(0, lastPosition == -1 ? text.Length :
text.LastIndexOf(Environment.NewLine, lastPosition - 1));
You override the results of the 2nd line with your 3rd line (just adding "\r\n" to it).
Also, you can use Environment.NewLine instead of "\r\n", so your code will be cross-platform.
So just one line will suffice for your task:
var trimmed = text.Substring(0, text.LastIndexOf(Environment.NewLine));
Hi all i am doing an application where i write my data to the text file. What ever data that user enters on the form and click on save i will save that data to the text file that was chosen by the user . Assume my content is as follows
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234
I would like to pad the next 8 lines with the following
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
Lie that if i have 5 lines of text in the file i would like to pad the next 5 lines with the same as mentioned can any one tell how to do this
Each and every line length is '94'
Any number of lines can be there
var text = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234" + Environment.NewLine + "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234";
const String padWith = "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999";
const int lineNum = 10;
var lines = text.Split(new[] { Environment.NewLine }, StringSplitOptions.None).ToList();
while(lines.Count < lineNum) {
lines.Add(padWith);
}
File.WriteAllLines(path, lines);
Here goes the code.
1) Find out the no of lines in your file
2) make count%10, if count%10==0 do not pad else 10-result=required
length , Pad with required length.
Sample code assume you have 8 lines
int cnt = 8;
int result = cnt % 10; // Will get 8
int iresult1 = 10 - result;
Hope it helps
I have a file created from a directory listing. From each of item a user selects from a ListBox, the application reads the directory and writes out a file that matches all the contents. Once that is done it goes through each item in the ListBox and copies out the item that matches the ListBox selection. Example:
Selecting 0001 matches:
0001456.txt
0001548.pdf.
The code i am using isn't handling 0s very well and is giving bad results.
var listItems = listBox1.Items.OfType<string>().ToArray();
var writers = new StreamWriter[listItems.Length];
for (int i = 0; i < listItems.Length; i++)
{
writers[i] = File.CreateText(
Path.Combine(destinationfolder, listItems[i] + "ANN.TXT"));
}
var reader = new StreamReader(File.OpenRead(masterdin + "\\" + "MasterANN.txt"));
string line;
while ((line = reader.ReadLine()) != null)
{
for (int i = 0; i < listItems.Length; i++)
{
if (line.StartsWith(listItems[i].Substring(0, listItems[i].Length - 1)))
writers[i].WriteLine(line);
}
}
Advice for correcting this?
Another Sample:
I have 00001 in my listbox: it returns these values:
00008771~63.txt
00002005~3.txt
00009992~1.txt
00001697~1.txt
00000001~1.txt
00009306~2.txt
00000577~1.txt
00001641~1.txt
00001647~1.txt
00001675~1.txt
00001670~1.txt
It should only return:
00001641~1.txt
00001647~1.txt
00001675~1.txt
00001670~1.txt
00001697~1.txt
Or if someone could just suggest a better method for taking each line in my listbox searching for line + "*" and whatever matches writes out a textfile...
This is all based pretty much on the one example you gave, but I believe the problem is that when you are performing your matching, you are getting the substring if your list item value and chopping off the last character.
In your sample you are attempting to match files starting with "00001", but when you do the match you are getting substring starting at zero and value.length-1 characters, which in this case would be "0000". For example:
string s = "00001";
Console.WriteLine(s.Substring(0,s.Length-1));
results in
0000
So I think if you just changed this line:
if (line.StartsWith(listItems[i].Substring(0, listItems[i].Length - 1)))
writers[i].WriteLine(line);
to this
if (line.StartsWith(listItems[i]))
writers[i].WriteLine(line);
you would be in good shape.
Sorry if I misunderstood your question, but let's start with this:
string line = String.Empty;
string selectedValue = "00001";
List<string> matched = new List<string>();
StreamReader reader = new StreamReader(Path.Combine(masterdin, "MasterANN.txt"));
while((line = reader.ReadLine()) != null)
{
if(line.StartsWith(selectedValue))
{
matched.Add(line);
}
}
This will match all lines from your MasterANN.txt file which begins with "00001" and add them into a collection (later we'll work on writing this into a file, if required).
This clarifies something?