C# - Loading a large file into a WPF RichTextBox? - c#

I need to load a ~ 10MB range text file into a WPF RichTextBox, but my current code is freezing up the UI. I tried making a background worker do the loading, but that doesnt seem to work too well either.
Here's my loading code. Is there any way to improve its performance? Thanks.
//works well for small files only
private void LoadTextDocument(string fileName, RichTextBox rtb)
{
System.IO.StreamReader objReader = new StreamReader(fileName);
if (File.Exists(fileName))
{
rtb.AppendText(objReader.ReadToEnd());
}
else rtb.AppendText("ERROR: File not found!");
objReader.Close();
}
//background worker version. doesnt work well
private void LoadBigTextDocument(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
System.IO.StreamReader objReader = new StreamReader( ((string[])e.Argument)[0] );
StringBuilder sB = new StringBuilder("For performance reasons, only the first 1500 lines are displayed. If you need to view the entire output, use an external program.\n", 5000);
int bigcount = 0;
int count = 1;
while (objReader.Peek() > -1)
{
sB.Append(objReader.ReadLine()).Append("\n");
count++;
if (count % 100 == 0 && bigcount < 15)
{
worker.ReportProgress(bigcount, sB.ToString());
bigcount++;
sB.Length = 0;
}
}
objReader.Close();
e.Result = "Done";
}

WPF RichTextBox control use Flow Document to display Rich Text and then attach the Flow Document to RTB control,while Windows Form RichTextBox control display Rich Text directly.
that's what makes WPF RTB super slow.
if you are okay with using a WinForm RTB just host it in your wpf app.
the xaml :
<Window x:Class="WpfHostWfRTB.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<Grid>
<WindowsFormsHost Background="DarkGray" Grid.row="0" Grid.column="0">
<wf:RichTextBox x:Name="rtb"/>
</WindowsFormsHost>
</Grid>
</Grid>
</Window>
C# code
private void LoadTextDocument(string fileName, RichTextBox rtb)
{
System.IO.StreamReader objReader = new StreamReader(fileName);
if (File.Exists(fileName))
{
rtb.AppendText(objReader.ReadToEnd());
}
else rtb.AppendText("ERROR: File not found!");
objReader.Close();
}

Graphical controls just isn't designed to handle that kind of data, simply because it would become unworkable. Even if the control could handle the large string, what's visible in the control is so little compared to the entire text that the scroll bars would become practically useless. To locate a specific line in the text you would have to move the slider to the closest position that it could specify, then scroll a line at a time for minutes...
Instead of submitting your users to something useless like that, you should rethink how you display the data, so that you can do it in a way that would actually be possible to use.

I'm working on a very similar project.
The project entails loading a large text file (max size approx: 120MB but we want to go higher) and then constructing an outline of the text file in a tree. Clicking on a node in the tree will scroll the user to that portion of the text file.
After talking to a lot of people I think the best solution is to create a sort of "sliding window" viewer where you only load as much text as the user can see at a time into the rtb.Text.
So.. say load the entire file into a List but only put 100 of those lines into rtb.Text. If the user scrolls up remove the bottom line and add a line of text to the top. If they scroll down remove the top line and add a line of text to the bottom. I get pretty good performance with this solution. (50s to load a 120MB file)

I have notice using RichTextboxes that as you add more "lines" it starts to slow down. If you can do it without appending the '\n' it will speed up for you. Remember each '\n' is a new paragraph object block for the RichTextbox.
This is my method for loading a 10 MB file. It takes about to 30 seconds to load. I use a progress bar dialog box to let my user know it is going to take time to load.
// Get Stream of the file
fileReader = new StreamReader(File.Open(this.FileName, FileMode.Open));
FileInfo fileInfo = new FileInfo(this.FileName);
long bytesRead = 0;
// Change the 75 for performance. Find a number that suits your application best
int bufferLength = 1024 * 75;
while (!fileReader.EndOfStream)
{
double completePercent = ((double)bytesRead / (double)fileInfo.Length);
// I am using my own Progress Bar Dialog I left in here to show an example
this.ProgressBar.UpdateProgressBar(completePercent);
int readLength = bufferLength;
if ((fileInfo.Length - bytesRead) < readLength)
{
// There is less in the file than the lenght I am going to read so change it to the
// smaller value
readLength = (int)(fileInfo.Length - bytesRead);
}
char[] buffer = new char[readLength];
// GEt the next chunk of the file
bytesRead += (long)(fileReader.Read(buffer, 0, readLength));
// This will help the file load much faster
string currentLine = new string(buffer).Replace("\n", string.Empty);
// Load in background
this.Dispatcher.BeginInvoke(new Action(() =>
{
TextRange range = new TextRange(textBox.Document.ContentEnd, textBox.Document.ContentEnd);
range.Text = currentLine;
}), DispatcherPriority.Normal);
}

Why don't you add to a string variable (or perhaps even use StringBuilder) then assign the value to the .Text property when you're done parsing?

You can try this it worked for me.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Create new StreamReader
StreamReader sr = new StreamReader(openFileDialog1.FileName, Encoding.Default);
// Get all text from the file
string str = sr.ReadToEnd();
// Close the StreamReader
sr.Close();
// Show the text in the rich textbox rtbMain
backgroundWorker1.ReportProgress(1, str);
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// richTextBox1.Text = e.ProgressPercentage.ToString() + " " + e.UserState.ToString();
richTextBox1.Text = e.UserState.ToString();
}

Have you considered trying to make the app multi-threaded?
How much of the text file do you need to see at once? You may want to look into lazy-loading in .NET or in your case C#

I'm not improve the performance of loading, but I use it to load my richtextbox asynchronously. I hope that could help you.
XAML :
<RichTextBox Helpers:RichTextBoxHelper.BindableSource="{Binding PathFileName}" />
Helper :
public class RichTextBoxHelper
{
private static readonly ILog m_Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public static readonly DependencyProperty BindableSourceProperty =
DependencyProperty.RegisterAttached("BindableSource", typeof(string), typeof(RichTextBoxHelper), new UIPropertyMetadata(null, BindableSourcePropertyChanged));
public static string GetBindableSource(DependencyObject obj)
{
return (string)obj.GetValue(BindableSourceProperty);
}
public static void SetBindableSource(DependencyObject obj, string value)
{
obj.SetValue(BindableSourceProperty, value);
}
public static void BindableSourcePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
var thread = new Thread(
() =>
{
try
{
var rtfBox = o as RichTextBox;
var filename = e.NewValue as string;
if (rtfBox != null && !string.IsNullOrEmpty(filename))
{
System.Windows.Application.Current.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Background,
(Action)delegate()
{
rtfBox.Selection.Load(new FileStream(filename, FileMode.Open), DataFormats.Rtf);
});
}
}
catch (Exception exception)
{
m_Logger.Error("RichTextBoxHelper ERROR : " + exception.Message, exception);
}
});
thread.Start();
}
}

Related

How do I use a StreamReader to input text from one WPF window to another in C#?

My problem is this, I am writing some software in WPF C# and I need to makeit so that the MainWindow will parse the txt file I have made
and store the information in a data
structure, the data should be passed to the
second Window when it is opened.
I have the StreamReader code working fine, it can locate the txt file, but it won't show the information in the listbox on the second window (I apologize if I have screwed up the formatting, very new to the website)
namespace ACW2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void inventoryButton_Click(object sender, RoutedEventArgs e)
{
InventoryWindow wnd = new InventoryWindow();
wnd.ShowDialog();
string line;
StreamReader file = new StreamReader(#"G:\P1\txt_files\inventory.txt");
List<int> list = new List<int>();
while ((line = file.ReadLine()) != null) ;
{
ListBox.Items.Add.(Line);
list.Add(int.Parse(line));
}
}
you have few problems here:
semicolon on while ((line = file.ReadLine()) != null) ; line which means that next code block (in curly braces) will not be executed in the loop, but after the loop ends.
What is Line (with capital L) which you are adding to ListBox.Items collection? Where is that Line defined and what is it's value?
what is line (lowercase L this time)?
try to fix those errors and we'll see what's next to be done...
What do you want to reach with
ListBox.Items.Add.(Line);
Maybe you mean this:
ListBox.Items.Add.(line);
You also dont need the semicolon at the end of your while statement.
Edit:
Specify your ListBox on the second window with a x:Name = "myListBox" tag.
After you did that you should be able to add an element to the listbox with wnd.myListBox.Items.Add(line);

How to restore Caret position or Change color of text without making a selection in RichTextBox

This has been an issue with many of my applications and I don't know why Windows doesn't have an elegant solution for this.
I am working with Winforms in .Net 4.5 in VS2013
For example, I would like to change the color of one line of text in a multiline RichTextBox.
For this I am required to set the selection using something like
rtb.Select(rtb.GetFirstCharIndexFromLine(r), str.Length);
Then, I would set the color using
rtb.SelectionColor = Color.Red;
And presumably, cancel the selection with
rtb.DeselectAll();
Now the problem is the cursor/caret has moved back to the beginning of the line,
I try to fix it by saving the previous Caret Position,
rtb.CaretPosition
However, CaretPosition is not a method of RichTextBox, and everywhere online this is the primary method everyone uses.
I tried adding PresentationFramework to my References and to my code I added
using System.Windows.Framework;
As suggested here: http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.caretposition(v=vs.110).aspx
but I still do not see the CaretPosition property, only the ScrollToCaret() method.
My 2 questions are:
How do I get the CaretPosition property in my RichTextBox?
How can I change the text color without using selections and affecting the caret position, having to write complex logic to restore it for the user.
My application checks serial numbers, one per line, and highlights them red if they do not match the format, as shown below.
private void rtb_TextChanged(object sender, EventArgs e)
{
string pattern = #"[A-Z]{2}[A-Z, 0-9]{2}\d{4}";
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
TextReader read = new System.IO.StringReader(rtb.Text);
SerialNumbers.Clear();
int selectStart = 0;
for (int r = 0; r < rtb.Lines.Length; r++)
{
string str = read.ReadLine();
if (str != null)
{
selectStart += str.Length;
MatchCollection matches = rgx.Matches(str);
if (matches.Count == 1)
{
SerialNumbers.Add(str);
}
else
{
rtb.Select(rtb.GetFirstCharIndexFromLine(r), str.Length);
rtb.SelectionColor = Color.Red;
rtb.DeselectAll();
}
}
}
}
You should be using SelectionCaret (as #Mangist mentioned in a comment) because you're using WinForms and not WPF. The MSDN article you referenced only applies to WPF, which is very different from WinForms.
As an example, I use the following to easily log to a rich text box from anywhere in a WinForms app:
public static void Log(string text, ref RichTextBox rtbLogBox) {
//
if (text == null) return;
var timestamp = DateTime.Now.ToLongTimeString();
var logtext = string.Format("{0} - {1}\r\n\r\n", timestamp, text);
if (rtbLogBox.InvokeRequired) {
var logBox = rtbLogBox;
logBox.Invoke(new MethodInvoker(delegate {
logBox.AppendText(logtext);
logBox.Update();
logBox.SelectionStart = logBox.Text.Length;
logBox.ScrollToCaret();
}));
} else {
rtbLogBox.AppendText(logtext);
rtbLogBox.Update();
rtbLogBox.SelectionStart = rtbLogBox.Text.Length;
rtbLogBox.ScrollToCaret();
}
}
Notice how the ScrollToCaret() is called after setting SelectionStart to the length of text in the rich text box. This solves the 'issue' of AppendText not scrolling to the bottom after adding text.
In your case you will simply want to save the SelectionStart value before you format your text with the highlighting, and then restore it once you've finished.
Fixed it by saving SelectionStart position
int selectionStart = SNbox.SelectionStart;
SNbox.Select(SNbox.GetFirstCharIndexFromLine(r), str.Length);
SNbox.SelectionColor = Color.Red;
SNbox.DeselectAll();
SNbox.SelectionStart = selectionStart;
SNbox.SelectionLength = 0;

How to bring Inline from a RichTextBox Child into View

How can i focus a Inline in a RichTextBox?
I Create a FlowDocument from a Text-File and load it in my richTextBox1
and mark one Inline after an other accordingly to a Button_click (be recreating the FlowDocument)
with this code:
richTextBox1.SelectAll();
richTextBox1.Selection.Text = "";
string text = System.IO.File.ReadAllText(file);
int iZeile = 0;
string[] split = text.Split(new string[] {"\r\n"},StringSplitOptions.None);
foreach (string s in split)
{
if (iZeile != 27)
{
paragraph.Inlines.Add(s + "\r\n"); // adds line added without marking
}
else
{
Run run = new Run(split[27]); // adds line with marking
run.Background = Brushes.Yellow;
paragraph.Inlines.Add(run);
paragraph.Inlines.Add("\r\n");
}
iZeile++;
}
FlowDocument document = new FlowDocument(paragraph);
richTextBox1.Document = new FlowDocument();
richTextBox1.Document = document;
Keyboard.Focus(richTextBox1);
}
I know its not.. perfect.
the Issue
It works so far but the problem that occurs is me Market Inline doesn't comes intoView. Is there a easy way to bring this Inline intoView?
The straightforward solution seemed to be FrameworkContentElement.BringIntoView() but after putting it in the code below it initially had no effect. As it turns out this is one of these timing issues (I've seen similar problems in WinForms) that can be solved by processing the outstanding Windows Messages. WPF has no direct equivalent of DoEvents() but there exists a well known substitute.
I placed this in a ButtonClick, changes marked with //**:
Paragraph paragraph = new Paragraph();
Inline selected = null; //**
richTextBox1.SelectAll();
richTextBox1.Selection.Text = "";
string text = System.IO.File.ReadAllText(#"..\..\MainWindow.xaml.cs");
int iZeile = 0;
string[] split = text.Split(new string[] { "\r\n" }, StringSplitOptions.None);
foreach (string s in split)
{
if (iZeile != 27)
{
paragraph.Inlines.Add(s + "\r\n"); // adds line added without marking
}
else
{
Run run = new Run(split[27]); // adds line with marking
run.Background = Brushes.Yellow;
paragraph.Inlines.Add(run);
paragraph.Inlines.Add("\r\n");
selected = run; // ** remember this element
}
iZeile++;
}
FlowDocument document = new FlowDocument(paragraph);
richTextBox1.Document = new FlowDocument();
richTextBox1.Document = document;
Keyboard.Focus(richTextBox1);
DoEvents(); // ** this is required, probably a bug
selected.BringIntoView(); // **
And the helper method, from here:
public static void DoEvents()
{
Application.Current.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Background,
new Action(delegate { }));
}
you should try one of this methods
richTextBox.SelectionStart = richTextBox.Text.Length;
richTextBox.ScrollToCaret();
.
richTextBox.AppendText(text);
richTextBox.ScrollToEnd();
futher informations are here and here
Edit
ok after a bit of digging in the WPF RichTextBox i thing you cloud try richTextBox.ScrollToVerticalOffset(Offset)
to get the Offset maybe you could use this answer
EDIT 2
ok after some more research i found following Link where you can download this working example

Windows Phone Emulator Functions Properly, Debugging or Deploying to Device Does Not

I am developing a very simple application that parses an XML feed, does some formatting and then displays it in a TextBlock. I've added a hyperLink (called "More..) to the bottom of the page (ideally this would be added to the end of the TextBlock after the XML has been parsed) to add more content by changing the URL of the XML feed to the next page.
The issue I'm experiencing is an odd one as the program works perfectly when in the Windows Phone 7 Emulator, but when I deploy it to the device or debug on the device, it works for the first click of the "More..." button, but the ones after the first click just seem to add empty space into the application when deployed or debugged from the device.
I'm using a Samsung Focus (NoDo) and originally thought this may have had to do with the fact that I may not have had the latest developer tools. I've made sure that I am running the latest version of Visual Studio and am still running into the issue.
Here are some snippets of my code to help out.
I've declared the clickCount variable here:
public partial class MainPage : PhoneApplicationPage
//set clickCount to 2 for second page
int clickCount = 2;
Here is the snippet of code I use to parse the XML file:
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
ListBoxItem areaItem = null;
StringReader stream = new StringReader(e.Result);
XmlReader reader = XmlReader.Create(stream);
string areaName = String.Empty;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "example")
{
areaName = reader.ReadElementContentAsString();
areaItem = new ListBoxItem();
areaItem.Content = areaName;
textBlock1.Inlines.Add(areaName);
textBlock1.Inlines.Add(new LineBreak());
}
}
}
}
}
and the code for when the hyperLink button is clicked:
private void hyperlinkButton1_Click(object sender, RoutedEventArgs e)
{
int stringNum = clickCount;
//URL is being incremented each time hyperlink is clicked
string baseURL = "http://startofURL" + stringNum + ".xml";
Uri url = new Uri(baseURL, UriKind.Absolute);
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(url);
//increment page number
clickCount = clickCount + 1;
}
It feels like there's a little more debugging to do here.
Can you test where exactly this is going wrong?
is it the click that is not working on subsequent attempts?
is it the HTTP load which is failing?
is it the adding of inline text which is failing?
Looking at it, I suspect it's the last thing. Can you check that your TextBlock is expecting Multiline text? Also, given what you've written (where you don't really seem to be making use of the Inline's from the code snippet I've seen), it might be easier to append add the new content to a ListBox or a StackPanel rather than to the inside of the TextBlock - ListBox's especially have some benefit in terms of Virtualizing the display of their content.

Path Display in Label

Are there any automatic methods for trimming a path string in .NET?
For example:
C:\Documents and Settings\nick\My Documents\Tests\demo data\demo data.emx
becomes
C:\Documents...\demo data.emx
It would be particularly cool if this were built into the Label class, and I seem to recall it is--can't find it though!
Use TextRenderer.DrawText with TextFormatFlags.PathEllipsis flag
void label_Paint(object sender, PaintEventArgs e)
{
Label label = (Label)sender;
TextRenderer.DrawText(e.Graphics, label.Text, label.Font, label.ClientRectangle, label.ForeColor, TextFormatFlags.PathEllipsis);
}
Your code is 95% there. The only
problem is that the trimmed text is
drawn on top of the text which is
already on the label.
Yes thanks, I was aware of that. My intention was only to demonstrate use of DrawText method. I didn't know whether you want to manually create event for each label or just override OnPaint() method in inherited label. Thanks for sharing your final solution though.
# lubos hasko Your code is 95% there. The only problem is that the trimmed text is drawn on top of the text which is already on the label. This is easily solved:
Label label = (Label)sender;
using (SolidBrush b = new SolidBrush(label.BackColor))
e.Graphics.FillRectangle(b, label.ClientRectangle);
TextRenderer.DrawText(
e.Graphics,
label.Text,
label.Font,
label.ClientRectangle,
label.ForeColor,
TextFormatFlags.PathEllipsis);
Not hard to write yourself though:
public static string TrimPath(string path)
{
int someArbitaryNumber = 10;
string directory = Path.GetDirectoryName(path);
string fileName = Path.GetFileName(path);
if (directory.Length > someArbitaryNumber)
{
return String.Format(#"{0}...\{1}",
directory.Substring(0, someArbitaryNumber), fileName);
}
else
{
return path;
}
}
I guess you could even add it as an extension method.
What you are thinking on the label is that it will put ... if it is longer than the width (not set to auto size), but that would be
c:\Documents and Settings\nick\My Doc...
If there is support, it would probably be on the Path class in System.IO
You could use the System.IO.Path.GetFileName method and append that string to a shortened System.IO.Path.GetDirectoryName string.
Next code works for folders. I'm using it to display a download path!
public static string TrimPath(string path) {
string shortenedPath = "";
string[] pathParts = path.Split('\\');
for (int i = 0; i < pathParts.Length-1; i++) {
string part = pathParts[i];
if (pathParts.Length-2 != i) {
if (part.Length > 5) { //If folder name length is bigger than 5 chars
shortenedPath += "..\\";
}
else {
shortenedPath += part+"\\";
}
}
else {
shortenedPath += part+"\\";
}
}
return shortenedPath;
}
Example:
Input:
C:\Users\Sandra\Desktop\Proyectos de programaciĆ³n\Prototype\ServerClient\test
output:
C:\Users\..\..\..\..\..\test\

Categories

Resources