C#RichTextBox Env​​ironment.NewLine无法正常工作

时间:2018-03-26 03:58:46

标签: c# newline richtextbox

我有一个自定义类可以在winform应用程序中用作调试控制台。它写的一切都很好,除了它永远不会进入下一行(wordwrap故意设置为false)。这是我的班级:

public static class ConsoleEx
{
    private static RichTextBox outTarget;

    public static void Initialize(RichTextBox target) { outTarget = target; }

    public static void Write( string source, string message ) { Write( source, Color.LightSkyBlue, message );}
    public static void Write( string source, Color sourceColor, string message ) { Write( source, sourceColor, message, Color.Silver );}
    public static void Write( string source, Color sourceColor, string message, Color messageColor )
    {
        if ( outTarget.InvokeRequired )
        {
            outTarget.Invoke( (MethodInvoker)delegate { Write( source, sourceColor, message, messageColor ); } );

            return;
        }

        int wordStart = outTarget.TextLength;

        outTarget.AppendText( source );
        outTarget.Select(wordStart, source.Length);
        outTarget.SelectionColor = sourceColor;

        wordStart = outTarget.TextLength;
        outTarget.DeselectAll();

        outTarget.AppendText( message );
        outTarget.Select(wordStart, source.Length);

        outTarget.SelectionColor = messageColor;
        outTarget.DeselectAll();
    }

    public static void WriteLine( string source, string message ) { Write( source, Color.LightSkyBlue, message);}
    public static void WriteLine( string source, Color sourceColor, string message ) { Write( source, sourceColor, message, Color.Silver );}
    public static void WriteLine( string source, Color sourceColor, string message, Color messageColor )
    {

        if (outTarget.InvokeRequired)
        {
            outTarget.Invoke((MethodInvoker)delegate { WriteLine(source, sourceColor, message, messageColor); });

            return;
        }

        Write( source, sourceColor, message, messageColor );
        outTarget.AppendText( Environment.NewLine );
    }
}

如您所见,我有一个Write和WriteLine部分。当然,写入不会附加新行。但是当我使用WriteLine时,它执行并执行最后一行outTarget.AppendText( Environment.NewLine );,并且,我假设,正在按照人们的预期添加它。但它没有用。在等待大约20行打印到“控制台”RichTextBox之后,每一行都被添加到第0行。任何想法?

1 个答案:

答案 0 :(得分:0)

好的,我做得很傻。我不小心把两个WriteLine链写成了Write而不是第三个WriteLine。大声笑,-facedesk .-