作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
public void overallTextReplace(RichTextBox[] rtb) {
string[] keyword = { "FCI", "CNG", "DCR", "EZR", "VASC", "CND" };
string[] newString = { "Forecourt Controller","Case Number Declined" ,"Case Number Given", "Dispenser Card reader", "Enhanced Zone Router", "Verifone Authorized Service Contractor" };
TextRange[] text = new TextRange[rtb.Length];
for (int I = 0; I < rtb.Length; I++) {
text[I] = new TextRange(rtb[I].Document.ContentStart, rtb[I].Document.ContentEnd);
}
for (int I = 0; I < text.Length; I++) {
for (int K = 0; K < keyword.Length; K++) {
TextPointer current = text[I].Start.GetInsertionPosition(LogicalDirection.Forward);
string textInRun = current.GetTextInRun(LogicalDirection.Forward);
if (!string.IsNullOrEmpty(textInRun)) {
int index = textInRun.IndexOf(keyword[K]);
if (index != -1) {
TextPointer selectionStart = current.GetPositionAtOffset(index, LogicalDirection.Forward);
TextPointer selectionEnd = selectionStart.GetPositionAtOffset(keyword.Length, LogicalDirection.Forward);
TextRange selection = new TextRange(selectionStart, selectionEnd);
selection.Text = newString[K];
selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Regular);
rtb[I].Selection.Select(selection.Start, selection.End);
rtb[I].Focus();
}
}
current = current.GetNextInsertionPosition(LogicalDirection.Forward);
}
}
}
好的,当传递给函数时,这段代码将查看 WPF 表单中的所有 RichTextBox,然后查找列出的关键字并将它们替换为 newString。我遇到的问题是程序从头到尾只看一行文本。如果它检测到一个换行符,它不会看过去,例如:第 1 行:FCI 是一个燃料 Controller 。它替换它就好了,但如果我在第 2 行有更多内容,它就不会进行替换。如果有什么不同的话,它们是 6 个 richTextBoxes 被传递给这个函数。
只是发现了一个错误,但与我的第一个问题无关。所以看起来有 6 个数组索引会阻止代码运行并在 TextRange selection = new Textrange(selectionStart, selectionEnd) 上抛出一个空引用;但如果我用 VASC 作为要替换的词,他们也不异常(exception)。我不确定为什么。
最佳答案
对于 winforms:试试这个(虽然我没有运行这段代码,但从逻辑上讲它应该可以工作):
public void overallTextReplace(RichTextBox[] rtb)
{
string[] keyword = { "FCI", "CNG", "DCR", "EZR", "VASC", "CND" };
string[] newString = { "Forecourt Controller","Case Number Declined" ,"Case Number Given", "Dispenser Card reader", "Enhanced Zone Router", "Verifone Authorized Service Contractor" };
for (int i = 0; i < rtb.Length; i++)
{
for (int j = 0; j < 6; j++)
{
rtb[i].Rtf=rtb[i].Rtf.Replace(keyword[j],newString[j]);
}
}
}
对于 wpf:
for (int i = 0; i < rtb.Length; i++)
{
RichTextBox rtb_wording= rtb[i];
var textRange = new TextRange(rtb_wording.Document.ContentStart, rtb_wording.Document.ContentEnd);
string rtf;
using (var memoryStream = new MemoryStream())
{
textRange.Save(memoryStream, DataFormats.Rtf);
rtf = ASCIIEncoding.Default.GetString(memoryStream.ToArray());
}
for (int j = 0; j < 6; j++)
{
rtf =rtf.Replace(keyword[j],newString[j]);
}
MemoryStream stream = new MemoryStream (ASCIIEncoding.Default.GetBytes(rtf));
rtb_wording.SelectAll();
rtb_wording.Selection.Load(stream, DataFormats.Rtf);
}
关于c# - 当用户在 richtextbox 中输入时,我可以使用 WPF 进行自动单词替换吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32751741/
我是一名优秀的程序员,十分优秀!