c# - 当用户在 richtextbox 中输入时,我可以使用 WPF 进行自动单词替换吗

标签 c# wpf windows forms richtextbox

    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/

相关文章:

windows - 图标资源路径

css - 为不同的操作系统分配不同的字体

windows - 使用我的 HDMI 端口作为输入

c# - 在列网格内定义行网格

wpf - DataGrid内的Combobox,隐藏Combobox上的选定项

c# - 将 Dictionary<T> 绑定(bind)到 WPF 列表框

c# - c#中的基本接口(interface)

c# - 对于链式异步/事件调用是否有有用的设计模式?

c# - c#中的批处理

c# - 如何在 Lucene.NET 中使用运算符组合术语查询和数字查询?