In eine TextBox protokollieren und ganz nach unten scrollen

Manchmal reicht bei einem kleinen Programm eine einfache mehrzeilige TextBox-Instanz aus, wenn Ihr Infos wegprotokollieren wollt.

Nachfolgender Code als Extension-Methode zum direkten Verwenden:

internal static class TextBoxLogger
{
    public static void LogLine(
        this TextBox textBox, string text, params object[] args)
    {
        if (textBox.InvokeRequired)
        {
            // Aus Hintergrund-Thread? Dann über Invoke aufrufen.
            textBox.BeginInvoke(
                new MethodInvoker(() => LogLine(textBox, text, args)));
        }
        else
        {
            textBox.Text += string.Format(text, args);

            if (textBox.Visible)
            {
                // Formular muss aktiv sein, damit scrollen kann.
                var form = textBox.FindForm();
                if (form != null)
                {
                    form.Show();
                    form.BringToFront();
                    form.Activate();
                }

                textBox.Select();
                textBox.SelectionStart = textBox.TextLength;
                textBox.ScrollToCaret();
            }
        }
    }
}

Und natürlich könnt Ihr auch ganz einfach in eine Datei loggen.