WPF RichTextBox Read Text File And Insert Bolded Text

504 views Asked by At

I am trying to figure a way i can read a text file and then add the text file words that have a split so I can say *okay normal text all the way till you see | after that make it Bold till you see another | after that make the text normal again. But i cant figure out how to do it in parts i got it underlining the entire part but that is it.

Example of how text file looks: Here WE go i hope this |*~b^works| and it looks good!

private static TextPointer GetPoint(TextPointer start, int x)
{
    var ret = start;
    var i = 0;

    while (i < x && ret != null)
    {
        if (ret.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.Text ||
            ret.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.None)
        {
            i++;
        }

        if (ret.GetPositionAtOffset(1, LogicalDirection.Forward) == null)
        {
            return ret;
        }

        ret = ret.GetPositionAtOffset(1,LogicalDirection.Forward);
    }
    return ret;
}

public void LetsSave()    
{
    string end = new TextRange(box.Document.ContentStart, box.Document.ContentEnd).Text;
    Clipboard.SetText(end);

    box.Document.Blocks.Clear();
    box.Document.Blocks.Add(new Paragraph(new Run(richText)));
    richText = new TextRange(box.Document.ContentStart, box.Document.ContentEnd).Text;


    File.WriteAllText(@"C:\Users\jacob\Documents\test\test.txt", end);
    string file = File.ReadAllText(@"C:\Users\jacob\Documents\test\test.txt");

    var xx = file.Split('|');
    box.Document.Blocks.Clear();

    int length = 0;
    int wordl = 0;

    foreach (var c in xx)
    {
        wordl = c.Length;
        var start = box.Document.ContentStart;
        var startPos = GetPoint(start, length);
        var endPos = GetPoint(start, length + wordl);
        var textRange = box.Selection;

        textRange.Select(startPos, endPos);
        TextDecorationCollection tdc = textRange.GetPropertyValue(Inline.TextDecorationsProperty) as TextDecorationCollection;

        MessageBox.Show(c);
        box.AppendText(c);

        if (c.Contains("*~"))
        {
            tdc.Add(myUnderline);
            textRange.ApplyPropertyValue(Inline.TextDecorationsProperty, tdc);
        }

        length += c.Length;
    }
}
1

There are 1 answers

0
Jacob Lenertz On BEST ANSWER

This Website helped me with a super easy fix. I was using a .txt file and i should have been using a .xaml. Here is the finished code i ended up using from the site.

private static void LoadFile(string filename, RichTextBox richTextBox)
{
    if (string.IsNullOrEmpty(filename))
    {
        throw new ArgumentNullException();
    }
    if (!File.Exists(filename))
    {
        throw new FileNotFoundException();
    }


    // open the file for reading
    using (FileStream stream = File.OpenRead(filename))
    {
        // create a TextRange around the entire document
        TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);

        // sniff out what data format you've got
        string dataFormat = DataFormats.Text;
        string ext = System.IO.Path.GetExtension(filename);
        if (String.Compare(ext, ".xaml", true) == 0)
        {
            dataFormat = DataFormats.Xaml;
        }
        else if (String.Compare(ext, ".rtf", true) == 0)
        {
            dataFormat = DataFormats.Rtf;
        }
        documentTextRange.Load(stream, dataFormat);
    }
}
private static void SaveFile(string filename, RichTextBox richTextBox)
{
    if (string.IsNullOrEmpty(filename))
    {
        throw new ArgumentNullException();
    }



    // open the file for reading
    using (FileStream stream = File.OpenWrite(filename))
    {
        // create a TextRange around the entire document
        TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);


        // sniff out what data format you've got
        string dataFormat = DataFormats.Text;
        string ext = System.IO.Path.GetExtension(filename);
        if (String.Compare(ext, ".xaml", true) == 0)
        {
            dataFormat = DataFormats.Xaml;
        }
        else if (String.Compare(ext, ".rtf", true) == 0)
        {
            dataFormat = DataFormats.Rtf;
        }
        documentTextRange.Save(stream, dataFormat);
    }
}