CS0120: An object reference is required for the nonstatic field, method, or property 'foo'

前端 未结 7 1085
挽巷
挽巷 2020-11-21 04:34

Consider:

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


        
7条回答
  •  后悔当初
    2020-11-21 05:19

    For this case, where you want to get a Control of a Form and are receiving this error, then I have a little bypass for you.

    Go to your Program.cs and change

    Application.Run(new Form1());
    

    to

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

    Now you can access a control with

    Program.form1.
    

    Also: Don't forget to set your Control-Access-Level to Public.

    And yes I know, this answer does not fit to the question caller, but it fits to googlers who have this specific issue with controls.

提交回复
热议问题