0

Here's an axample

enter image description here

Just curious, I'm new with C#, but I decided to use it to develop my server for my Java game simply because I wanted a nice server-gui, but I'm starting to realize this would be just as easy to do with Swing...

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195

2 Answers2

6

I did a similar project over the weekend, I used this link to forward all Console data to a textbox. You could use any similar control with minor tweaks.

public class TextBoxStreamWriter : TextWriter
{
    TextBox _output = null;

    public TextBoxStreamWriter(TextBox output)
    {
        _output = output;
    }

    public override void Write(char value)
    {
        base.Write(value);
        _output.AppendText(value.ToString()); // When character data is written, append it to the text box.
    }

    public override Encoding Encoding
    {
        get { return System.Text.Encoding.UTF8; }
    }
}

All of the Write(...) and WriteLine(...) trickle down to Write(char) so it's the only method that you need to override.

My example required the form to also have a public TextBox property which exposed the private TextBox inside the form.

TextWriter consoleRedirect = new Tools.TextBoxStreamWriter(consoleForm.TxtOuputDisplay);
Console.SetOut(consoleRedirect);
  • Hi Infinite Possiblities, it's customary in StackOverflow (and Superuser) answers to include a summary of the contents of a link or the highlights that specifically answer the question. The goal of SE sites is to become a resource of knowledge, of answers, for years to come. With a link-only answer, the op must dig through another resource to locate an answer he/she might not be sure about. Most importantly, if your link were to ever break, your answer is useless for anyone who visits this page in the future. Consider making an edit to your answer to add more details. Good luck! – Jeremy Thompson Feb 26 '13 at 03:15
  • @JeremyThompson I tried to provide my example and a short explanation, feel free to edit more if you can/need. – Infinite Possibilities Feb 26 '13 at 03:22
2

Well I already wrote this up before I saw the previous comment. But since I took the time I will post it anyway.

public void WriteLog(TextBox tb ,string log)
    {
        tb.AppendText(log + "\n");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        WriteLog(textBox1, "[App]: This is a log string");
        WriteLog(textBox1, "[App]: Another log string");
        WriteLog(textBox1, "[App]: Yet another etc etc.");
    }

Where textBox1 is a Multi-line textbox with a black backcolor and blue foreground text. The above comment is a more elegant solution but this will get you by if you want something quick and easy. I dont have enough rep to post an image inline but here is what it looks like. http://imageshack.us/photo/my-images/198/logwindow.jpg/