Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Updating StatusStrip text from class

Updating StatusStrip text from class

Scheduled Pinned Locked Moved C#
algorithmshelpquestionannouncement
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Brad Wick
    wrote on last edited by
    #1

    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?

    S D D 3 Replies Last reply
    0
    • B Brad Wick

      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?

      S Offline
      S Offline
      Simon P Stevens
      wrote on last edited by
      #2

      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

      B 1 Reply Last reply
      0
      • S Simon P Stevens

        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

        B Offline
        B Offline
        Brad Wick
        wrote on last edited by
        #3

        I have read that it is bad practice, but I have been searching and reading all morning and I can not seem to find anything that is helping me do this the correct way. Can anyone help out with this?

        1 Reply Last reply
        0
        • B Brad Wick

          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?

          D Offline
          D Offline
          Dirso
          wrote on last edited by
          #4

          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.

          D 1 Reply Last reply
          0
          • B Brad Wick

            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?

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • D Dirso

              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.

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              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)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups