Grifkilla51
Grifkilla51

Reputation: 11

using WPF can I do auto word replacement when a user is typing in 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);
            }
        }
    }

okay so this code will look through all RichTextBoxs in the WPF form when passed in to the function, it will then look for the keywords listed and replace them with the newString. the problem im having is that the program only looks on one line of text from start to finish. If it detects a newline it will not look past it for example: line1: the FCI is a fuel controller. it replaces it just fine but if i have more on line 2 it will not make the replace. if it makes any difference their are 6 richTextBoxes being passed in to this function.

just found an error, but it has nothing to do with my first issue. so it seems that having 6 array indexes prevents the code from running and throws a null ref on TextRange selection = new Textrange(selectionStart, selectionEnd); but if i use VASC as the word to be replaced their is no exception. I am not sure why.

Upvotes: 1

Views: 217

Answers (1)

Nikita Shrivastava
Nikita Shrivastava

Reputation: 3018

For winforms: Try this(Though I haven't run this code but logically it should work) :

 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]);
        }
     }
}

For 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);
}

Upvotes: 2

Related Questions