C
C#6mo ago
!Rushaan

Preventing TextBlock from getting deleted

I'm using a RichTextBox and setting some inline as TextBlock but the user is able to delete that textblock and i dont want that to happen, for some reason the inline returns Run type even if its a textblock, how do i see if its a textblock and prevent deletion
private void richTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Back || e.Key == Key.Delete)
{
TextPointer caretPosition = rich.CaretPosition;
if (caretPosition != null)
{
TextPointer prevCaretPosition = caretPosition.GetNextInsertionPosition(LogicalDirection.Backward);
if (prevCaretPosition != null)
{
Paragraph paragraph = prevCaretPosition.Paragraph;
if (paragraph != null)
{
Inline tb = paragraph.Inlines.LastInline;
if (tb != null)
{
Trace.WriteLine(tb.GetType().ToString());
}
}
}
}
}
}}
private void richTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Back || e.Key == Key.Delete)
{
TextPointer caretPosition = rich.CaretPosition;
if (caretPosition != null)
{
TextPointer prevCaretPosition = caretPosition.GetNextInsertionPosition(LogicalDirection.Backward);
if (prevCaretPosition != null)
{
Paragraph paragraph = prevCaretPosition.Paragraph;
if (paragraph != null)
{
Inline tb = paragraph.Inlines.LastInline;
if (tb != null)
{
Trace.WriteLine(tb.GetType().ToString());
}
}
}
}
}
}}
0 Replies
No replies yetBe the first to reply to this messageJoin