C
C#2y ago
.dark4

✅ 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
.dark4
.dark4OP2y ago
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'
Pendramon
Pendramon2y ago
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.
.dark4
.dark4OP2y ago
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
Form mainForm = new Form1();
Application.Run(mainForm);
Form mainForm = new Form1();
Application.Run(mainForm);
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);
ero
ero2y ago
I mean isn't there an Application.MainForm...?
.dark4
.dark4OP2y ago
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
Trouble with Windows Forms, in method "SetCompatibleTextRenderingDefault"
Trouble with Windows Forms, in method "SetCompatibleTextRenderingDefault"
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
ero
ero2y ago
I mean you literally do Application.Run, so i don't know where you're getting this...
.dark4
.dark4OP2y ago
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
public class MainForm
{
Form mainForm = new Form1();
}
public class MainForm
{
Form mainForm = new Form1();
}
then in my main program
MainForm formInstance = new();
Application.Run(formInstance.mainForm);
MainForm formInstance = new();
Application.Run(formInstance.mainForm);
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 public
ero
ero2y ago
System 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
.dark4
.dark4OP2y ago
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; }
ero
ero2y ago
Bad idea that, but go on
.dark4
.dark4OP2y ago
but i'm not sure how to update the label color when the field changes
ero
ero2y ago
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?
.dark4
.dark4OP2y ago
so would I do Form myForm = new Form1(); in my main program?
ero
ero2y ago
Why?
.dark4
.dark4OP2y ago
how do i define myForm instead of Application.Run(new Form1());
ero
ero2y ago
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?
.dark4
.dark4OP2y ago
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 instance
ero
ero2y ago
So 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
.dark4
.dark4OP2y ago
i'm inside my api class
internal class GSPApi
{
public void ConnectToGSP()
{
internal class GSPApi
{
public void ConnectToGSP()
{
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;
Label label2 = (Label)Application.OpenForms["Form1"].Controls["label2"];
label2.BackColor = Color.Yellow;
Label label2 = (Label)Application.OpenForms["Form1"].Controls["label2"];
label2.BackColor = Color.Yellow;
I get Object reference not set to an instance of an object when i run Label label2 = (Label)Application.OpenForms["Form1"].Controls["label2"];
.dark4
.dark4OP2y ago
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...
ero
ero2y ago
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);
}
}
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);
}
}
I just don't see how passing an instance of the form into whatever socket or API class your have isn't possible
.dark4
.dark4OP2y ago
thanks i'll try that
Accord
Accord2y ago
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.
.dark4
.dark4OP2y ago
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...
.dark4
.dark4OP2y ago
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
Want results from more Discord servers?
Add your server