Search code examples
.netwinformsformattingrichtextprintdocument

replace and print with printdocument


i need to replace text in a rich-text box with string.format(rtb.rtf,val1,val2,etc) and print the result with printdocument, but having difficulty

Customer:
{0}
Tag:            {1}
Paymethod:      {2}
─────────────────
Status:     {3}
Cash:           {4}
Balance:        {5}
─────────────────
Subtotal:       {6}
─────────────────
VAT:            {7}
Discount:       {8}
═════════════════
Total:      {9}
═════════════════

when i look at the rich-text in notepad, i see the bracketed numbers like this {0}, {1}, etc

I would love your suggestions


Solution

  • Parsing a rich text file is fraught with dangers, so I would avoid trying to do it that way. Simply load the text in a RichTextBox control and replace the text by selecting it.

    for (int i = 0; i < 10; ++i) {
      string findText = "{" + i.ToString() + "}";
      int index = rtb.Text.IndexOf(findText);
      if (index > -1) {
        rtb.Select(index, findText.Length);
        rtb.SelectedText = "new value";
      }
    }