✅ How do I change background color of a label from a different namespace part of my app?

APArnold Palmer12/12/2022
In my main program class I run Application.Run(new Form1()); how do I access this instance of my winform to change a label background?
APArnold Palmer12/12/2022
GameChanger.Form1.Label2.BackColor = System.Drawing.Color.Green;
APArnold Palmer12/12/2022
I want to change label2 background color from red to green once my program connects to a socket
APArnold Palmer12/12/2022
i changed the modifer to public
APArnold Palmer12/12/2022
Severity Code Description Project File Line Suppression State
Error CS0120 An object reference is required for the non-static field, method, or property 'Form1.label2'
PPendramon12/12/2022
I think if you store new Form1() in a variable and then use that variable to access and set Label2's BackColor the UI should reflect the changes, however I am not entirely certain as I've never done that before.

The proper way would be to not tie your business logic to UI, have the BackColor of the label binded to a property in your view model and implement INotifyPropertyChanged and call the PropertyChanged event in the set accessor every time the value of your BackColor property changes. Then have your business logic fire off Connected/Disconnected events and in the view model where your BackColor property is, listen for those events and change the color correspondingly to the state. But I understand that this may be overkill for what you may be trying to achieve.
APArnold Palmer12/13/2022
Thanks for the response. I have a console app, trying to use the winform as a basic dashboard.
APArnold Palmer12/13/2022
I tried making the form as a var in my main program
     Form mainForm = new Form1();
            Application.Run(mainForm);
APArnold Palmer12/13/2022
I can't seem to access it Program.mainForm.label2_Update("green");
APArnold Palmer12/13/2022
if I try to make it as a var in in Program class it says it needs to be static since Program is a static class
APArnold Palmer12/13/2022
but if I make the form static it messes up the winform stuff downstream
APArnold Palmer12/13/2022
I was able to make it satic but it says the form instance is inaccessible due to protection level, weird I made it public
APArnold Palmer12/13/2022
i tried creating the form var in the instance of my business logic class public Form mainForm = new Form1();
APArnold Palmer12/13/2022
but it never opens the win form
APArnold Palmer12/13/2022
ApplicationConfiguration.Initialize();
Application.Run(busClassInstance.mainForm);
EEro12/13/2022
I mean isn't there an Application.MainForm...?
APArnold Palmer12/13/2022
no? I don't think I even have an Application class
APArnold Palmer12/13/2022
I can do Form mainForm = new Form1(); Application.Run(mainForm); in my main program class
APArnold Palmer12/13/2022
but then the Form mainForm = new Form1(); is stuck as a local variable I can't access
APArnold Palmer12/13/2022
if I make it a field in the main program class I can't access it, if I move it to my business logic class instance it never runs
APArnold Palmer12/13/2022
i was getting this weird error
Trouble with Windows Forms, in method "SetCompatibleTextRenderingDefault"
APArnold Palmer12/13/2022
the way i've seen it done is move the business logic to the form class but I won't want to move everything over
EEro12/13/2022
I mean you literally do Application.Run, so i don't know where you're getting this...
APArnold Palmer12/13/2022
Application is System.Windows.Forms.Application
APArnold Palmer12/13/2022
there is no MainForm in that System class
APArnold Palmer12/13/2022
I just don't get why is says inaccessible
APArnold Palmer12/13/2022
i made a class
    public class MainForm
    {
        Form mainForm = new Form1();
    }
APArnold Palmer12/13/2022
then in my main program
          MainForm formInstance = new();
            Application.Run(formInstance.mainForm);
APArnold Palmer12/13/2022
I get CS0122 'MainForm.mainForm' is inaccessible due to its protection level
APArnold Palmer12/13/2022
ahh I understand
APArnold Palmer12/13/2022
Form mainForm = new Form1(); is internal by default you have to specify public
EEro12/13/2022
System is not a class
EEro12/13/2022
Application is
EEro12/13/2022
ApplicationContext.MainForm does exist
EEro12/13/2022
Honestly, you're providing so little context, it's genuinely hard to understand what you even want to do
APArnold Palmer12/13/2022
I'm making a winform dashboard to update a label when I connect to a socket
APArnold Palmer12/13/2022
I was able to store the label color in a field
APArnold Palmer12/13/2022
public static string label2Color { get; set; }
EEro12/13/2022
Bad idea that, but go on
APArnold Palmer12/13/2022
but i'm not sure how to update the label color when the field changes
EEro12/13/2022
I'm not entirely sure why this matters in the first place
EEro12/13/2022
What's the actual problem you're facing?
EEro12/13/2022
What're you trying to solve?
EEro12/13/2022
Why can't you just do myForm.label2.BackgroundColor = Color.Red?
APArnold Palmer12/13/2022
so would I do Form myForm = new Form1(); in my main program?
EEro12/13/2022
Why?
APArnold Palmer12/13/2022
how do i define myForm
APArnold Palmer12/13/2022
instead of Application.Run(new Form1());
EEro12/13/2022
I don't know, I'm not sure what problem you're facing
EEro12/13/2022
Is it that your Socket doesn't know about the form? Or what's going on?
APArnold Palmer12/13/2022
I can't do myForm.label2.BackgroundColor = Color.Red becuase myForm doesn't exist
APArnold Palmer12/13/2022
I just have an instance of Form1 but can't access that instance
EEro12/13/2022
So make it exist. Not program-wide, just let your socket (or whatever you're using) know about the form
EEro12/13/2022
There has to be some sort of event, or a constructor, or whatever you're doing (you're genuinely not giving enough details) that allows you to just pass the form instance to the socket
APArnold Palmer12/13/2022
i'm inside my api class
APArnold Palmer12/13/2022
    internal class GSPApi
    {
        public void ConnectToGSP()
        {
APArnold Palmer12/13/2022
I don't even understand what my form instance is called
APArnold Palmer12/13/2022
when i do Application.Run(new Form1());
APArnold Palmer12/13/2022
so i just reference Form1? that's not static
APArnold Palmer12/13/2022
I was trying Label label2 = Application.OpenForms["Form1"].Controls["label2"] as Label;
APArnold Palmer12/13/2022
 Label label2 = (Label)Application.OpenForms["Form1"].Controls["label2"];
                    label2.BackColor = Color.Yellow;
APArnold Palmer12/13/2022
I get Object reference not set to an instance of an object when i run Label label2 = (Label)Application.OpenForms["Form1"].Controls["label2"];
APArnold Palmer12/13/2022
EEro12/13/2022
So since I'm absolutely not understanding whatever the world you're doing, let me just show what I'd do
class SocketController
{
  private readonly MainForm _form;

  public SocketController(MainForm form)
  {
    _form = form;
  }

  private void OnConntected()
  {
    _form.Label.BackColor = Color.Yellow;
  }
}

public Form MainForm
{
  private readonly SocketController _socket;

  public MainForm()
  {
    // initialize

    _socket = new(this);
  }
}
EEro12/13/2022
I just don't see how passing an instance of the form into whatever socket or API class your have isn't possible
APArnold Palmer12/13/2022
thanks i'll try that
AAccord12/14/2022
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
APArnold Palmer12/15/2022
finally resolved it, so simple https://stackoverflow.com/a/41526184
APArnold Palmer12/15/2022
I think my issue was not awaiting an async task that was preventing the win form from running when I was using an instance like this