-2

I cannot get the BackColor to change, I have 3 errors:

1)

An object reference is required for the non-static field, method, or property 'Form.BackColor' (Line 32, same error on 36)

2)

The program has more than one entry point defined. Compile with /main to specify the type that contains the entry point. (Line 28)

Thanks.

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

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }

    public class CapsLockIndicator
    {
        public static void Main()
        {
            if (Control.IsKeyLocked(Keys.CapsLock))
            {
                Form1.BackColor = Color.FromArgb(102, 204, 0);
            }
            else
            {
                Form1.BackColor = Color.FromArgb(204, 0, 0);
            }
        }
    }

}

I expect Form1's back color to change to the different colors.

STLDev
  • 5,950
  • 25
  • 36
  • 1
    You might want to learn some of the fundamentals of Object Oriented Programming first, in particularly, the terms *Class* and *Object* (or *Instance*). – goodvibration May 01 '19 at 04:09
  • 1
    [This is related, or possible duplicate](https://stackoverflow.com/a/36497081/5133585) – Sweeper May 01 '19 at 05:58

1 Answers1

0

Form1 is a class. You need to create an instance of that class before you can change fields/properties of the instance e.g.

var form1 = new Form1();
form1.BackColor = Color.FromArgb(102, 204, 0);
[...]

As for the multiple entry points: an entry point in C# is defined here

It looks like you have made a default WinForms application. If so then your entry point should be in a file called Program.cs which should create a new instance of your Form1.

Example:

enter image description here

As mentioned in comments, you should probably follow some tutorials or read up on the basics / fundamentals of C# and Object Orientated Programming.

Minijack
  • 726
  • 7
  • 23
  • This is really a bad advice: `var form1 = new Form1()` (not because of the missing semicolon). That's a new instance of the Form. Setting its BackColor won't change the color of the existing instance. – Jimi May 01 '19 at 04:51
  • @Jimi OP doesn't specify that Form1 is instantiated elsewhere, which is why I Mentioned where the entry point probably is if they created a *normal* WinForms Application. Will edit to include the semicolon, thanks for that! – Minijack May 01 '19 at 04:54
  • `public partial class Form1 : Form` at the top of the code the OP presented. – Jimi May 01 '19 at 04:55
  • @Jimi you can call any class a `Form`, that doesn't mean that's definitely a `System.Windows.Form` (there could be another `Form` user-defined). It is a reasonable assumption, which is why I mention it in my answer, but I try not to make that assumption. – Minijack May 01 '19 at 04:58
  • The class (of course) is not called Form, is called Form1. Which is usually the starting Form of a new application. But this not the point (no matter if the OP didn't mention the existence of another starting Form). This: `var form1 = new Form1();` won't do anything, because the Form must be first shown to change its BackColor. You're just newing it, also assuming it's not the starting Form. If it is, it won't do anything. If it isn't, it won't do anything. – Jimi May 01 '19 at 05:04
  • @jimi Ah I see your point now and yes, you are correct. However OP was not asking how to display the form, just changing that prop/field – Minijack May 01 '19 at 05:05
  • This is exactly the point: that code won't do anything. Except, maybe, confusing someone, that is. The existing instance of the Form affected by the change must be passed to the class that changes its properties (in the class constructor usually). Or, probably better, the class that, in this case, detects a change in state of a key, could raise an event that Form1 (or any other class/Form) can subscribe to and be notified of. – Jimi May 01 '19 at 05:09