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