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. Accessing a control from a separate class

Accessing a control from a separate class

Scheduled Pinned Locked Moved C#
sysadminhelptutorialquestion
7 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.
  • J Offline
    J Offline
    JF2015
    wrote on last edited by
    #1

    Hi at all, on my Windows Form i have some Controls whose properties need to be changed by another class that is not derived from the Forms class. How can this be achieved? (is it possible at all?) For example: public class Form : System.Windows.Forms.Form { ... public System.Windows.Forms.ToolStripStatusLabel tlStrpStatusLblState; ... } public class PB_OPC { public static void OnServerShutdown(string reason) { Form.tlStrpStatusLblState.Text = "OPC Server Not Connected"; } } I originally thought that would be possible by changing the "modifiers" Attribute to "Public", but it didn't help. Thanks for your efforts

    F 1 Reply Last reply
    0
    • J JF2015

      Hi at all, on my Windows Form i have some Controls whose properties need to be changed by another class that is not derived from the Forms class. How can this be achieved? (is it possible at all?) For example: public class Form : System.Windows.Forms.Form { ... public System.Windows.Forms.ToolStripStatusLabel tlStrpStatusLblState; ... } public class PB_OPC { public static void OnServerShutdown(string reason) { Form.tlStrpStatusLblState.Text = "OPC Server Not Connected"; } } I originally thought that would be possible by changing the "modifiers" Attribute to "Public", but it didn't help. Thanks for your efforts

      F Offline
      F Offline
      fang_eric
      wrote on last edited by
      #2

      try it as "public static",it will work well. but it's not the best sovlution.

      J 1 Reply Last reply
      0
      • F fang_eric

        try it as "public static",it will work well. but it's not the best sovlution.

        J Offline
        J Offline
        JF2015
        wrote on last edited by
        #3

        Hi, thanks a lot, it really did help, but i truly agree that this isn't the best solution. What about the programming style in OOP? Is it a bad habit to access a control from a separate class? I just wondered if there's an alternative.

        M 1 Reply Last reply
        0
        • J JF2015

          Hi, thanks a lot, it really did help, but i truly agree that this isn't the best solution. What about the programming style in OOP? Is it a bad habit to access a control from a separate class? I just wondered if there's an alternative.

          M Offline
          M Offline
          Martin 0
          wrote on last edited by
          #4

          Hello,

          JF2015 wrote:

          Is it a bad habit to access a control from a separate class?

          Yes, it is! Couse in most cases you only need a property of this control! Look at this Article from Colin Angus Mackay: http://www.codeproject.com/dotnet/passingvaluesbetweenforms.asp[^] Hope it helps! All the best, Martin

          C 1 Reply Last reply
          0
          • M Martin 0

            Hello,

            JF2015 wrote:

            Is it a bad habit to access a control from a separate class?

            Yes, it is! Couse in most cases you only need a property of this control! Look at this Article from Colin Angus Mackay: http://www.codeproject.com/dotnet/passingvaluesbetweenforms.asp[^] Hope it helps! All the best, Martin

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            Thanks for the link. :-D


            Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

            M 1 Reply Last reply
            0
            • C Colin Angus Mackay

              Thanks for the link. :-D


              Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

              M Offline
              M Offline
              Martin 0
              wrote on last edited by
              #6

              Yes, I thought a beginner like you can profit from that article! :-D:laugh:;)

              C 1 Reply Last reply
              0
              • M Martin 0

                Yes, I thought a beginner like you can profit from that article! :-D:laugh:;)

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #7

                :laugh: Yes, I sure did. :cool:


                Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

                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