新输入的富文本框颜色(WPF,C#) [英] Rich textbox color for new input (WPF, C#)

查看:247
本文介绍了新输入的富文本框颜色(WPF,C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为新输入更改我的富文本框颜色。文本框在启动时加载文本。我想控制新用户输入仍然突出显示,例如红色或黄色背景。新文本可以添加到任何地方,而不仅仅是最后。如果它只是在最后我可以轻松地在那里添加一个红色字母。



背后的问题是用户应该编辑文本并且应该突出显示更改。



我的代码:

C#program

  string  RTF =  @  {\rtf1\ansi\deff0 + 
@ {\ colortbl; \\\\\green0 \ blue0; \ red255 \ green0 \ blue0;} +
@ line1 \ line +
@ \ cf2 +
@ < span class =code-string>第2行\ line +
@ \ cf1 +
@ 第3行 +
@ };

MemoryStream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(RTF));
TextRange range = new TextRange(demo.Document.ContentStart,demo.Document.ContentEnd);
range.Load(stream,DataFormats.Rtf);



XAML代码

< RichTextBox Horizo​​ntalAlignment =   Left Height =   116 Margin =   37,266,0,0  Grid.Row =   1 x:Name =   demo VerticalAlignment =   Top宽度=   381  >  

< / RichTextBox >





我的尝试:



我已经添加了Rich来自Extended WPF Toolkit的TextBoxFormatBar。它允许用户格式化内容,但我没有找到设置新输入的默认颜色的解决方案。

解决方案

您可以比较给定的文本和新的输入文字。



如果发现任何不匹配,请格式化。





string oldText =whatever;

string newText = rtbGivenText.Text;



for(int i = 0; i< ; oldText.Length; i ++)

{

if(oldText [i] == newText [i])

{

rtbGivenText.Select(i,i + 1);

rtbGivenText.SelectionColor = Color.Red;

}

}

I want to change my rich textbox color for new input. The textbox loads a text at startup. I want to control that new user input is still highlighted for example red color or yellow background. The new text can be added anywhere and not only at the end. If it would be only at the end I could easily add a red letter there.

The problem behind is that the user should edit a text and the changes should be highlighted.

My code :
C# program

string RTF = @"{\rtf1\ansi\deff0 " +
@"{\colortbl;\red0\green0\blue0;\red255\green0\blue0;} "+
@"line1 \line "+
@"\cf2 "+
@"line 2 \line "+
@"\cf1 "+
@"line 3 "+
@"}";

MemoryStream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(RTF));
TextRange range = new TextRange(demo.Document.ContentStart, demo.Document.ContentEnd);
range.Load(stream, DataFormats.Rtf);


XAML Code

<RichTextBox HorizontalAlignment="Left" Height="116" Margin="37,266,0,0" Grid.Row="1" x:Name="demo" VerticalAlignment="Top" Width="381">
         
        </RichTextBox>



What I have tried:

I have already added the RichTextBoxFormatBar from Extended WPF Toolkit. It allows the user to format the content but I did not found a solution for setting the default color for new input.

解决方案

You can compare both the given text and the new input text.

If any mismatch found, then format it.


string oldText = "whatever";
string newText = rtbGivenText.Text;

for (int i = 0; i < oldText.Length; i++)
{
if (oldText[i]==newText[i])
{
rtbGivenText.Select(i, i+1);
rtbGivenText.SelectionColor = Color.Red;
}
}


这篇关于新输入的富文本框颜色(WPF,C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆