locked
Implement WriteLine(string format, params object[] args) for RichTextBox class? RRS feed

  • Question

  • Dear All,

    How to implement WriteLine(string format, params object[] args) for RichTextBox class?

    The situation now is if we want to print formatted string to RichTextBox,

    it is something like

                int i = 0;
                float j = 32.4F;

                richTextBox1.AppendText(String.Format("The output is {0}, {1}", i, j) + Environment.NewLine);

    Is it possible to print the formatted string like we output it to Console

                // if it could be done, the statement is simpler, like below

                richTextBox1.WriteLine("The output is {0}, {1}", i, j);

    Thanks and Best regards,

    E-John


    • Edited by E-John Wednesday, December 11, 2019 5:42 AM
    • Moved by CoolDadTx Wednesday, December 11, 2019 3:43 PM Winforms related
    Wednesday, December 11, 2019 5:31 AM

Answers

  • You can implement that as extension method. For example:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            richTextBox1.WriteLine("This is line {0}", 1);
        }
    }
    
    public static class RichTextBoxExtensions
    {
        public static void WriteLine(this RichTextBox richTextBox, string format, params object[] args)
        {
            richTextBox.AppendText(string.Format(format, args));
        }
    }

    Wednesday, December 11, 2019 5:59 AM

All replies

  • You can implement that as extension method. For example:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            richTextBox1.WriteLine("This is line {0}", 1);
        }
    }
    
    public static class RichTextBoxExtensions
    {
        public static void WriteLine(this RichTextBox richTextBox, string format, params object[] args)
        {
            richTextBox.AppendText(string.Format(format, args));
        }
    }

    Wednesday, December 11, 2019 5:59 AM
  • Dear MasaSam,

    Thanks for your great helps.

    Best regards,

    E-John

    Wednesday, December 11, 2019 12:36 PM