C# 自动替换wpf richtextbox中的文本
我有一个WPF.NET 4 C#C# 自动替换wpf richtextbox中的文本,c#,wpf,.net-4.0,richtextbox,C#,Wpf,.net 4.0,Richtextbox,我有一个WPF.NET 4 C#RichTextBox,我想用其他字符替换该文本框中的某些字符,这将发生在KeyUp事件中 我试图做到的是用完整的单词替换首字母缩略词,例如: 个人电脑 星际争霸 等等 我查看了一些类似的线程,但在我的场景中发现的任何东西都没有成功 最终,我希望能够通过一系列首字母缩略词来做到这一点。但是,我甚至在替换单个首字母缩写词时遇到问题,有人能帮忙吗?因为System.Windows.Controls.RichTextBox没有Text的属性要检测其值,可以使用以下方法检
RichTextBox
,我想用其他字符替换该文本框中的某些字符,这将发生在KeyUp
事件中
我试图做到的是用完整的单词替换首字母缩略词,例如:
个人电脑
星际争霸
等等 我查看了一些类似的线程,但在我的场景中发现的任何东西都没有成功
最终,我希望能够通过一系列首字母缩略词来做到这一点。但是,我甚至在替换单个首字母缩写词时遇到问题,有人能帮忙吗?因为
System.Windows.Controls.RichTextBox
没有Text
的属性要检测其值,可以使用以下方法检测其值
string _Text = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;
_Text = _Text.Replace("pc", "Personal Computer");
if (_Text != new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text)
{
new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text = _Text;
}
然后,您可以更改\u Text
,并使用以下命令发布新字符串
string _Text = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;
_Text = _Text.Replace("pc", "Personal Computer");
if (_Text != new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text)
{
new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text = _Text;
}
看起来是这样的
string _Text = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;
_Text = _Text.Replace("pc", "Personal Computer"); // Replace pc with Personal Computer
if (_Text != new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text)
{
new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text = _Text; // Change the current text to _Text
}
备注:不使用文本。替换(“pc”、“个人电脑”)
您可以声明一个列表
,在其中保存字符及其替换项
示例:
List<string> _List = new List<string>();
private void richTextBox1_TextChanged(object sender, TextChangedEventArgs e)
{
string _Text = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;
for (int count = 0; count < _List.Count; count++)
{
string[] _Split = _List[count].Split(','); //Separate each string in _List[count] based on its index
_Text = _Text.Replace(_Split[0], _Split[1]); //Replace the first index with the second index
}
if (_Text != new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text)
{
new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text = _Text;
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// The comma will be used to separate multiple items
_List.Add("pc,Personal Computer");
_List.Add("sc,Star Craft");
}
List _List=newlist();
私有void richTextBox1_TextChanged(对象发送者,textchangedventargs e)
{
string _Text=新文本范围(richTextBox1.Document.ContentStart,richTextBox1.Document.ContentEnd).Text;
对于(int count=0;count<\u List.count;count++)
{
string[]_Split=_List[count].Split(',);//根据索引分隔_List[count]中的每个字符串
_Text=_Text.Replace(_Split[0],_Split[1]);//用第二个索引替换第一个索引
}
if(_Text!=新文本范围(richTextBox1.Document.ContentStart,richTextBox1.Document.ContentEnd).Text)
{
新的文本范围(richTextBox1.Document.ContentStart,richTextBox1.Document.ContentEnd);
}
}
已加载私有无效窗口(对象发送器、路由目标)
{
//逗号将用于分隔多个项目
_列表。添加(“pc、个人电脑”);
_列表。添加(“sc,星际争霸”);
}
谢谢,
我希望你觉得这很有用:)@Mike一点问题都没有,很高兴我能帮上忙:)我发现这行
新的文本范围(richTextBox1.Document.ContentStart,richTextBox1.Document.ContentEnd)。Text=\u Text
真的很奇怪。这真的改变了RichTextBox文本吗?