2017-11-20 10 views
0

テキストファイルを読み込んで分かれているテキストファイルを追加する方法を考えようとしています。 |それ以降はもう一度|が見えるまでボールドにしてください。しかし、私は部品の中でそれを行う方法を理解できません。部品全体に下線を引いていますが、それはそれです。WPF RichTextBoxテキストファイルを読み込んで太字のテキストを挿入

テキストファイルがどのように表示されるかの例:ここで私はこの|*~b^works|を希望しています。

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; 
    } 
} 

答えて

0

このWebsiteは、簡単に修正できました。私は.txtファイルを使用していました。私は.xamlを使用していたはずです。ここでは、私はサイトから使用を終了した完成コードです。

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); 
    } 
} 
関連する問題