Home > Net >  change color of row in richtextbox by changing tabindex in c# with wpf
change color of row in richtextbox by changing tabindex in c# with wpf

Time:10-15

I'm converting the project I've done with WinForms before to wpf. But I have some problems.

I paint the lines I want of the text in the RichTextBox.

WinForms code in below:

private void TextBoxGcode_TabIndexChanged(object sender, EventArgs e)
{
    if (0 < GcodeSayi)
    {
        TextBoxGcode.Find(lines[GcodeSayi - 1], RichTextBoxFinds.MatchCase);
        TextBoxGcode.SelectionBackColor = Color.White;
        TextBoxGcode.Find(lines[GcodeSayi], RichTextBoxFinds.MatchCase);
        TextBoxGcode.SelectionBackColor = Color.DodgerBlue;
    }
    else
    {
        TextBoxGcode.SelectAll();
        TextBoxGcode.SelectionBackColor = Color.White;

    }
}

TextBoxGcode.TabIndex = TempGcodeSayi;

For example:

for example

It could be like this. Changing the color of the relevant line in the RichTextBox with the line number I entered from a textbox. I need only line.

How can I do it with WPF? Please help.

Thanks,

CodePudding user response:

The extension class below RichTextBoxExt includes the TextRange SelectLine(this RichTextBox rtb, int line, Color color) method.

This method has two input parameters:

  • int line - a line number that background to be colored.
  • Color color - background color to be use to color the specified line.

The method returns the TextRange of the colored line or null if no line is found with the defined number.

using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace WpfApp
{
    public static class RichTextBoxExt
    {
        public static TextRange SelectLine(this RichTextBox rtb, int line, Color color)
        {
            if (rtb.Document.ContentStart.TryGetLineStartPosition(line) is TextPointer lStart)
            {
                if (rtb.Document.ContentStart.TryGetLineStartPosition(line   1) is TextPointer lEnd)
                {
                    TextRange tr = new(lStart, lEnd);
                    tr.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(color));
                    return tr;
                }
            }
            return null;
        }

        /// <returns>Returns a System.Windows.Documents.TextPointer pointing to the beginning of the specified line or null if the specified line is out of range.</returns>
        private static TextPointer TryGetLineStartPosition(this TextPointer tp, int skip)
        {
            LogicalDirection direction = skip < 0 ? LogicalDirection.Backward : LogicalDirection.Forward;
            while (tp != null && tp.Paragraph == null)
            {
                tp = tp.GetNextContextPosition(direction);
            }
            return tp is TextPointer ? tp.GetLineStartPosition(skip) : null;
        }
    }
}

An example of using the method described above:

using WpfApp;

var rtb = ...; // RichTextBox
int nLine = ...; // Defining number of the line to be colored 

if (rtb.SelectLine(nLine, Colors.Pink) is TextRange tr)
{
    // Set cursor to the start of the colored line
    rtb.CaretPosition = tr.Start;
} 

CodePudding user response:

I have some problems syntax problem

also error describtion, like that

how to define Richtexbox. As you can see it not accept

enter image description here

  • Related