Updating StatusStrip text from class
-
I have a frmMain which has a StatusStrip on it. Inside my class I have a function which checks for internet connection and when the system is not online, I would like for it to update one of the labels on the StatusStrip. I have tried to do some searching and have tried some examples but can not seem to get it to work. I created the below function in frmMain.cs
public void Set_StatusStrip(string status) { toolStripStatusInternetConnection.Text = ""; }
In inc_functions.cs I have the following function
public void InternetConnectionAvailable() { // code to check for internet connection goes here frmMain.Set_StatusStrip("Application Online"); }
The error I get is "An object reference is required for the non-static field, method, or property." Can anyone see what I am doing wrong?
-
I have a frmMain which has a StatusStrip on it. Inside my class I have a function which checks for internet connection and when the system is not online, I would like for it to update one of the labels on the StatusStrip. I have tried to do some searching and have tried some examples but can not seem to get it to work. I created the below function in frmMain.cs
public void Set_StatusStrip(string status) { toolStripStatusInternetConnection.Text = ""; }
In inc_functions.cs I have the following function
public void InternetConnectionAvailable() { // code to check for internet connection goes here frmMain.Set_StatusStrip("Application Online"); }
The error I get is "An object reference is required for the non-static field, method, or property." Can anyone see what I am doing wrong?
In the "IntererConnectionAvailable" method, you need to be calling Set_StatusStrip on an instance of the form, not on the form class
Object test = new Object(); Object.TestMethod1() <-- Static Method test.TestMethod2() <-- Instance method
You are calling it as if it was a static method, but it's not, it's an instance method. Good luck. (This is a bad design by the way. You shouldn't be using a helper object to set a UI property on the form. Consider reading up about MVC, MVP or other GUI patterns)
Simon
-
In the "IntererConnectionAvailable" method, you need to be calling Set_StatusStrip on an instance of the form, not on the form class
Object test = new Object(); Object.TestMethod1() <-- Static Method test.TestMethod2() <-- Instance method
You are calling it as if it was a static method, but it's not, it's an instance method. Good luck. (This is a bad design by the way. You shouldn't be using a helper object to set a UI property on the form. Consider reading up about MVC, MVP or other GUI patterns)
Simon
-
I have a frmMain which has a StatusStrip on it. Inside my class I have a function which checks for internet connection and when the system is not online, I would like for it to update one of the labels on the StatusStrip. I have tried to do some searching and have tried some examples but can not seem to get it to work. I created the below function in frmMain.cs
public void Set_StatusStrip(string status) { toolStripStatusInternetConnection.Text = ""; }
In inc_functions.cs I have the following function
public void InternetConnectionAvailable() { // code to check for internet connection goes here frmMain.Set_StatusStrip("Application Online"); }
The error I get is "An object reference is required for the non-static field, method, or property." Can anyone see what I am doing wrong?
You could do something like
public void InternetConnectionAvailable(frmMain f)
{
// code to check for internet connection goes here
f.Set_StatusStrip("Application Online");
}And when you call
InternetConnectionAvailable
, you pass an instance of your frmMain. -
I have a frmMain which has a StatusStrip on it. Inside my class I have a function which checks for internet connection and when the system is not online, I would like for it to update one of the labels on the StatusStrip. I have tried to do some searching and have tried some examples but can not seem to get it to work. I created the below function in frmMain.cs
public void Set_StatusStrip(string status) { toolStripStatusInternetConnection.Text = ""; }
In inc_functions.cs I have the following function
public void InternetConnectionAvailable() { // code to check for internet connection goes here frmMain.Set_StatusStrip("Application Online"); }
The error I get is "An object reference is required for the non-static field, method, or property." Can anyone see what I am doing wrong?
This is a perfect situation for using your own custom event. Something like this: Create a delegate and event
public delegate void ConnectionChangedEventHandler(object sender, ConnectionEventArgs e);
public event ConnectionChangedEventHandler ConnectionChanged;and a ConnectionEventArgs class
public class ConnectionEventArgs
{
public ConnectionEventArgs(bool isConnected)
{
m_IsConnected = isConnected;
}
private bool m_IsConnected;
public bool IsConnected
{
get { return m_IsConnected; }
}
}then make the InternetConnection call a method
private void InternetConnectionAvailable()
{
OnConnectionChanged(new ConnectionEventArgs(true));
}
protected virtual void OnConnectionChanged(ConnectionEventArgs e)
{
if (ConnectionChanged != null)
ConnectionChanged(this, e);
}Now you can subscribe to the ConnectionChanged event and update the StatusStrip
MyClassInstance.ConnectionChanged += new MyClass.ConnectionChangedEventHandler(MyClassInstance_ConnectionChanged);
void MyClassInstance_ConnectionChanged(object sender, ConnectionEventArgs e)
{
// Set status strip
}[edit] This is (obviously) also reusable for when it's not available too
private void InternetConnectionUnavailable()
{
OnConnectionChanged(new ConnectionEventArgs(false));
}Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)modified on Thursday, October 16, 2008 1:07 PM
-
You could do something like
public void InternetConnectionAvailable(frmMain f)
{
// code to check for internet connection goes here
f.Set_StatusStrip("Application Online");
}And when you call
InternetConnectionAvailable
, you pass an instance of your frmMain.He's trying to avoid bad/not-recommended practices and this is one of those - why on earth would his Connection class or this particular method in it need to know about the form? It's nothing to do with the connection. Events/delegates are the way to handle this scenario.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)