user2317810
user2317810

Reputation: 11

how to convert Console.WriteLine in to RichTextBox?

how to convert Console.WriteLine to RichTextBox?

I was wondering if anyone knew and I do not know (for something I put the question) ... I searched and found nothing so I want to know if any of you can help me

http://i41.tinypic.com/9hutzo.png

Upvotes: 0

Views: 5716

Answers (1)

Damith
Damith

Reputation: 63105

You can use AppendText as below

//Console.WriteLine("Hello");

richTextBox1.AppendText(String.Format("{0}{1}", "Hello", Environment.NewLine));

EDIT

As per your comments you want to write text form windows form to a console application

Add class called Win32 to your project like below

using System;
using System.Runtime.InteropServices;

namespace SoTest
{
    public class Win32
    {
        /// <summary>
        /// Allocates a new console for current process.
        /// </summary>
        [DllImport("kernel32.dll")]
        public static extern Boolean AllocConsole();

        /// <summary>
        /// Frees the console.
        /// </summary>
        [DllImport("kernel32.dll")]
        public static extern Boolean FreeConsole();
    }
}

You have to do few changes to windows form application like below

   public Form1()
    {
        InitializeComponent();
        Win32.AllocConsole();  //Allocates a new console for current process.
    }

    private void button1_Click(object sender, EventArgs e) // on button click we write text to console 
    {
        Console.WriteLine(richTextBox1.Text); // write RTB text to console 
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)  // add form closing event 
    {
        Win32.FreeConsole(); // Free the console.
    }

Done!

enter image description here

Refere This blog Post for more information

Upvotes: 2

Related Questions