✅ How do I change background color of a label from a different namespace part of my app?
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?25 Replies
GameChanger.Form1.Label2.BackColor = System.Drawing.Color.Green;
I want to change label2
background color from red to green once my program connects to a socket
i changed the modifer to public
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'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.Thanks for the response. I have a console app, trying to use the winform as a basic dashboard.
I tried making the form as a var in my main program
I can't seem to access it
Program.mainForm.label2_Update("green");
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
but if I make the form static it messes up the winform stuff downstream
I was able to make it satic but it says the form instance is inaccessible due to protection level, weird I made it public
i tried creating the form var in the instance of my business logic class public Form mainForm = new Form1();
but it never opens the win form
ApplicationConfiguration.Initialize();
Application.Run(busClassInstance.mainForm);I mean isn't there an Application.MainForm...?
no? I don't think I even have an Application class
I can do
Form mainForm = new Form1();
Application.Run(mainForm);
in my main program class
but then the Form mainForm = new Form1();
is stuck as a local variable I can't access
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
i was getting this weird error
the way i've seen it done is move the business logic to the form class but I won't want to move everything overI mean you literally do Application.Run, so i don't know where you're getting this...
Application is
System.Windows.Forms.Application
there is no MainForm
in that System class
I just don't get why is says inaccessible
i made a class
then in my main program
I get CS0122 'MainForm.mainForm' is inaccessible due to its protection level
ahh I understand
Form mainForm = new Form1();
is internal by default you have to specify publicSystem is not a class
Application is
ApplicationContext.MainForm does exist
Honestly, you're providing so little context, it's genuinely hard to understand what you even want to do
I'm making a winform dashboard to update a label when I connect to a socket
I was able to store the label color in a field
public static string label2Color { get; set; }
Bad idea that, but go on
but i'm not sure how to update the label color when the field changes
I'm not entirely sure why this matters in the first place
What's the actual problem you're facing?
What're you trying to solve?
Why can't you just do
myForm.label2.BackgroundColor = Color.Red
?so would I do
Form myForm = new Form1();
in my main program?Why?
how do i define myForm
instead of
Application.Run(new Form1());
I don't know, I'm not sure what problem you're facing
Is it that your Socket doesn't know about the form? Or what's going on?
I can't do
myForm.label2.BackgroundColor = Color.Red
becuase myForm
doesn't exist
I just have an instance of Form1
but can't access that instanceSo make it exist. Not program-wide, just let your socket (or whatever you're using) know about the form
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
i'm inside my api class
I don't even understand what my form instance is called
when i do Application.Run(new Form1());
so i just reference
Form1
? that's not static
I was trying Label label2 = Application.OpenForms["Form1"].Controls["label2"] as Label;
I get Object reference not set to an instance of an object
when i run Label label2 = (Label)Application.OpenForms["Form1"].Controls["label2"];
Stack Overflow
Accessing Form's Controls from another class
I have a windows forms application with some controls added to the designer. When I want to change something (LIKE) enabling a text box from inside the Form1.cs, I simply use:
textBox1.Enabled = tr...
So since I'm absolutely not understanding whatever the world you're doing, let me just show what I'd do
I just don't see how passing an instance of the form into whatever socket or API class your have isn't possible
thanks i'll try that
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.finally resolved it, so simple https://stackoverflow.com/a/41526184
Stack Overflow
CS0120: An object reference is required for the nonstatic field, me...
Consider:
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Cl...
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