Jeremy Walmsley
Jeremy Walmsley

Reputation: 177

Change form background colour from Program.cs

I've been at this for ages and am unable to find a solution. I want to change the background colour of a form when the computer is unlocked. I am struggling to access the form BackColour property from the Program.cs file.

I've created a method in my form.cs that I can references from program.cs However, I don't know how to change the background colour from within my method.

Here is my code. Any ideas would be really appreciated.

//Program.cs

namespace Lums_Status_Client
{
    static class Program
    {
        public static Form statusform = new Form1();
        public static string status = "available";
        private static SessionSwitchEventHandler sseh;

        [STAThread]
        static void Main()
        {

            ThreadStart job = new ThreadStart(ThreadJob);
            Thread thread = new Thread(job);
            thread.Start();

            sseh = new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
            SystemEvents.SessionSwitch += sseh;
            while (true) { }    
        }

        static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
        {

            //get the username
            string get_userName = Environment.UserName;
            Debug.WriteLine(e.Reason);
            Form1.colourchanger();
        }

        static void ThreadJob()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);        
            Application.Run(statusform);
        } 
    }
}

and my form.cs

namespace Lums_Status_Client
{

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();

        this.BackColor = System.Drawing.Color.Green;
        System.Drawing.Rectangle workingRectangle =
        Screen.PrimaryScreen.WorkingArea;


        this.Left = workingRectangle.Width - 120;

    }

    private void Status_Change(object sender, MouseEventArgs e)
    {

        if (e.Button == MouseButtons.Left && Program.status == "available")
        {
            this.BackColor = System.Drawing.Color.Orange;
            Program.status = "Busy";
            MessageBox.Show("You status has been updated to " + Program.status);



        }

        else if (e.Button == MouseButtons.Left && Program.status == "Busy")
        {
            this.BackColor = System.Drawing.Color.Green;
            Program.status = "Available";
            MessageBox.Show("You status has been updated to " + Program.status);



        }

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    public static void colourchanger()

    {

        //Debug.WriteLine("This class is working");

        this.BackColor = System.Drawing.Color.Aqua;
    }




}

}

Upvotes: 0

Views: 524

Answers (2)

TaW
TaW

Reputation: 54433

Assuming you only need one instance of Form1 you can change Program.cs to keep a Form1 reference ready:

using System.Drawing;
//...
//...

static class Program
{

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        F1 = new Form1();
        Application.Run(F1);
    }

    static Form1 F1;

    public static void ChangeColor(Color newColor)
    {
        F1.BackColor = newColor;
    }

}

Now you can call the ChangeColor method as:

Program.ChangeColor(Color.Aqua);

Upvotes: 0

Jonesopolis
Jonesopolis

Reputation: 25370

you have to reference the instance of Form1 you've already declared, and make it an instance method, not static:

 public void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
    {

        //get the username
        string get_userName = Environment.UserName;
        Debug.WriteLine(e.Reason);
        statusform.colourchanger(); //access instance object
    }

Upvotes: 1

Related Questions