C# 尝试使用扩展方法向RichTextBox追加文本时出现无效操作异常

C# 尝试使用扩展方法向RichTextBox追加文本时出现无效操作异常,c#,forms,richtextbox,C#,Forms,Richtextbox,我有一个RichTextBox的扩展方法,这个方法是由关于StackOverflow的另一个问题提供的 public static class RichTextBoxExtensions { public static void AppendText(this RichTextBox box, string text, Color color) { box.SelectionStart = box.TextLength;

我有一个RichTextBox的扩展方法,这个方法是由关于StackOverflow的另一个问题提供的

public static class RichTextBoxExtensions
    {
        public static void AppendText(this RichTextBox box, string text, Color color)
        {
            box.SelectionStart = box.TextLength;
            box.SelectionLength = 0;

            box.SelectionColor = color;
            box.AppendText(text);
            box.SelectionColor = box.ForeColor;
        }
    }
我正试图这样使用它:

public void Write(string Text)
        {
            Color Green = Color.Green;
            TxtBox.AppendText(Text, Green);
        }
然而,当我运行这个时,我得到了

“System.InvalidOperationException”类型的第一次意外异常 发生在System.Windows.Forms.dll中


有人知道会出什么问题吗?谢谢。

您的代码运行正常,但似乎您正在尝试从UI线程以外的其他线程访问richtextbox

您可以按如下方式更改扩展方法:

public static class RichTextBoxExtensions
{
    public static void AppendText(this RichTextBox box, string text, Color color)
    {
        if (box.InvokeRequired)
            box.Invoke((Action)(() => AppendText(box, text, color)));
        else
        {
            box.SelectionStart = box.TextLength;
            box.SelectionLength = 0;

            box.SelectionColor = color;
            box.AppendText(text);
            box.SelectionColor = box.ForeColor;
        }

    }
}

您的代码正常工作,但似乎您正试图从UI线程以外的其他线程访问richtextbox

您可以按如下方式更改扩展方法:

public static class RichTextBoxExtensions
{
    public static void AppendText(this RichTextBox box, string text, Color color)
    {
        if (box.InvokeRequired)
            box.Invoke((Action)(() => AppendText(box, text, color)));
        else
        {
            box.SelectionStart = box.TextLength;
            box.SelectionLength = 0;

            box.SelectionColor = color;
            box.AppendText(text);
            box.SelectionColor = box.ForeColor;
        }

    }
}

您的代码正常工作,但似乎您正试图从UI线程以外的其他线程访问richtextbox

您可以按如下方式更改扩展方法:

public static class RichTextBoxExtensions
{
    public static void AppendText(this RichTextBox box, string text, Color color)
    {
        if (box.InvokeRequired)
            box.Invoke((Action)(() => AppendText(box, text, color)));
        else
        {
            box.SelectionStart = box.TextLength;
            box.SelectionLength = 0;

            box.SelectionColor = color;
            box.AppendText(text);
            box.SelectionColor = box.ForeColor;
        }

    }
}

您的代码正常工作,但似乎您正试图从UI线程以外的其他线程访问richtextbox

您可以按如下方式更改扩展方法:

public static class RichTextBoxExtensions
{
    public static void AppendText(this RichTextBox box, string text, Color color)
    {
        if (box.InvokeRequired)
            box.Invoke((Action)(() => AppendText(box, text, color)));
        else
        {
            box.SelectionStart = box.TextLength;
            box.SelectionLength = 0;

            box.SelectionColor = color;
            box.AppendText(text);
            box.SelectionColor = box.ForeColor;
        }

    }
}

您是否试图在其他线程中访问
RichTextBox
?可能是计时器?您是否尝试在其他线程中访问
RichTextBox
?可能是计时器?您是否尝试在其他线程中访问
RichTextBox
?可能是计时器?您是否尝试在其他线程中访问
RichTextBox
?可能是计时器吗?