考虑:

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //int[] val = { 0, 0};
            int val;
            if (textBox1.Text == "")
            {
                MessageBox.Show("Input any no");
            }
            else
            {
                val = Convert.ToInt32(textBox1.Text);
                Thread ot1 = new Thread(new ParameterizedThreadStart(SumData));
                ot1.Start(val);
            }
        }

        private static void ReadData(object state)
        {
            System.Windows.Forms.Application.Run();
        }

        void setTextboxText(int result)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new IntDelegate(SetTextboxTextSafe), new object[] { result });
            }
            else
            {
                SetTextboxTextSafe(result);
            }
        }

        void SetTextboxTextSafe(int result)
        {
            label1.Text = result.ToString();
        }

        private static void SumData(object state)
        {
            int result;
            //int[] icount = (int[])state;
            int icount = (int)state;

            for (int i = icount; i > 0; i--)
            {
                result += i;
                System.Threading.Thread.Sleep(1000);
            }
            setTextboxText(result);
        }

        delegate void IntDelegate(int result);

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

为什么会出现这个错误?

对于非静态字段、方法或属性,需要一个对象引用


当前回答

它看起来像是从静态方法(特别是SumData)调用一个非静态成员(属性或方法,特别是setTextboxText)。你需要:

Make the called member static also: static void setTextboxText(int result) { // Write static logic for setTextboxText. // This may require a static singleton instance of Form1. } Create an instance of Form1 within the calling method: private static void SumData(object state) { int result = 0; //int[] icount = (int[])state; int icount = (int)state; for (int i = icount; i > 0; i--) { result += i; System.Threading.Thread.Sleep(1000); } Form1 frm1 = new Form1(); frm1.setTextboxText(result); } Passing in an instance of Form1 would be an option also. Make the calling method a non-static instance method (of Form1): private void SumData(object state) { int result = 0; //int[] icount = (int[])state; int icount = (int)state; for (int i = icount; i > 0; i--) { result += i; System.Threading.Thread.Sleep(1000); } setTextboxText(result); }

关于此错误的更多信息可以在MSDN上找到。

其他回答

它看起来像是从静态方法(特别是SumData)调用一个非静态成员(属性或方法,特别是setTextboxText)。你需要:

Make the called member static also: static void setTextboxText(int result) { // Write static logic for setTextboxText. // This may require a static singleton instance of Form1. } Create an instance of Form1 within the calling method: private static void SumData(object state) { int result = 0; //int[] icount = (int[])state; int icount = (int)state; for (int i = icount; i > 0; i--) { result += i; System.Threading.Thread.Sleep(1000); } Form1 frm1 = new Form1(); frm1.setTextboxText(result); } Passing in an instance of Form1 would be an option also. Make the calling method a non-static instance method (of Form1): private void SumData(object state) { int result = 0; //int[] icount = (int[])state; int icount = (int)state; for (int i = icount; i > 0; i--) { result += i; System.Threading.Thread.Sleep(1000); } setTextboxText(result); }

关于此错误的更多信息可以在MSDN上找到。

从我的寻找你给一个空值的文本框,并返回一个ToString(),因为它是一个静态方法。你可以用Convert.ToString()替换它,它可以启用空值。

这要归功于@COOLGAMETUBE,是他向我透露了最终适合我的方法。他的想法很好,但我在申请时遇到了一个问题。SetCompatibleTextRenderingDefault在表单创建后被调用。所以稍微改变一下,这对我来说是可行的:

静态类程序 { public static Form1;// = new Form1();//把这个变量放在构造函数外面

/// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(form1 = new Form1()); } }

您的方法必须是静态的

static void setTextboxText(int result)
{
    if (this.InvokeRequired)
    {
        this.Invoke(new IntDelegate(SetTextboxTextSafe), new object[] { result }); 
    }
    else
    {
        SetTextboxTextSafe(result);
    }
}

对于这种情况,当您想要获得窗体的控件并接收此错误时,我为您提供了一个小绕过。

进入Program.cs并进行更改

Application.Run(new Form1());

to

public static Form1 form1 = new Form1(); // Place this var out of the constructor
Application.Run(form1);

现在可以访问控件

Program.form1.<Your control>

另外:不要忘记将你的Control-Access-Level设置为Public。

是的,我知道,这个答案不适合问题调用者,但它适合谷歌人谁有这个特定的问题与控制。