使用WPF时,我可以在用户输入richtextbox时进行自动单词替换

时间:2015-09-24 00:55:35

标签: 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替换它们。我遇到的问题是程序只从头到尾查看一行文本。如果它检测到换行符,它将不会超过它,例如:line1:FCI是燃料控制器。它取代它就好了,但如果我在第2行有更多,它将不会取代。如果它有任何区别,他们将6个richTextBox传入此函数。

刚发现错误,但它与我的第一个问题无关。因此看起来有6个数组索引会阻止代码运行并在TextRange选择= new Textrange(selectionStart,selectionEnd)上抛出空引用; 但如果我使用VASC作为要替换的词,他们也不例外。我不知道为什么。

1 个答案:

答案 0 :(得分:2)

对于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);
}