非静态字段需要对象引用,为什么?

Dan*_*zes 0 c#

我做了一些关于这个错误的研究,我找到的所有发现包括从方法或属性中删除静态,但在我的代码中没有任何静态,所以我不知道发生了什么,谢谢你的帮助.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class textoTitular : Form
    {
        public textoTitular()
        {
            InitializeComponent();
        }


        private void textoTitular_Load(object sender, EventArgs e)
        {

            textoTitular.Text = "testing";   /// prints testing on the textbox
        }
    } 
}
Run Code Online (Sandbox Code Playgroud)

Adr*_*der 6

你的问题在于

private void textoTitular_Load(object sender, EventArgs e)
{
    textoTitular.Text = "testing";   /// prints testing on the textbox
}
Run Code Online (Sandbox Code Playgroud)

您以静态方式引用表单类.

而是尝试使用this.就像是

private void textoTitular_Load(object sender, EventArgs e)
{
    this.Text = "testing";   /// prints testing on the textbox
}
Run Code Online (Sandbox Code Playgroud)

添加奖金,您可以省略this并使用对象属性

private void textoTitular_Load(object sender, EventArgs e)
{
    Text = "testing";   /// prints testing on the textbox
}
Run Code Online (Sandbox Code Playgroud)