开发者

Console-like text control?

开发者 https://www.devze.com 2023-01-09 00:16 出处:网络
I\'m looking for a winforms control that can output text like in a console window (output is appended to the bottom, color formatting, etc.) Much like the \"outp开发者_如何转开发ut\" window in most ID

I'm looking for a winforms control that can output text like in a console window (output is appended to the bottom, color formatting, etc.) Much like the "outp开发者_如何转开发ut" window in most IDEs.

I tried making one myself but it didn't work the way I wanted, so I'm wondering if there is some existing control out there.

This is for an open-source project.


I have wrote a simple control. But it does not work with colors (its derived from textWriter, which does not support colors).

internal class TextBoxWriter : TextWriter {
    TextBox _output;

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

    public override void WriteLine(string value) {
        Write(value + System.Console.Out.NewLine);
    }

    public override void Write(string value) {
        if(_output.InvokeRequired) {
            _output.BeginInvoke((Action<string>)Write, value);
        } else {
            _output.AppendText(value);
        }
    }

    public override void Write(char value) {
        Write(value.ToString());
    }

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

Usage:

//designer code
private System.Windows.Forms.TextBox outputTextBox;
this.outputTextBox = new System.Windows.Forms.TextBox();

//user code
var _textWriter = new TextBoxWriter();
System.Console.SetOut(_textWriter);
Console.WriteLine("hello");   //this will be show in the outputTextBox


What about RichTextBox Control (built-in) ? Or maybe you could use ListView since it can be useful for irc. Especially ObjectListView gives a lot of possibilities.

0

精彩评论

暂无评论...
验证码 换一张
取 消