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. How to get current background color info from a form

How to get current background color info from a form

Scheduled Pinned Locked Moved C#
questioncomtutoriallounge
8 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.
  • M Offline
    M Offline
    MichCl
    wrote on last edited by
    #1

    I have a current form that is either lime or red. I think that (due to background worker threads), the best way to check the current state is to check the color of the form. How do I do this? I set it with

    SetBackground(Color.Lime);

    I saw the following threads, but they didn't seem to be the same thing, as far as I can tell. If it's the same, I'm not sure how to apply it to my situation. [^] http://www.masonmc.com/2008/getting-system-colors-in-c/[^]

    L R 2 Replies Last reply
    0
    • M MichCl

      I have a current form that is either lime or red. I think that (due to background worker threads), the best way to check the current state is to check the color of the form. How do I do this? I set it with

      SetBackground(Color.Lime);

      I saw the following threads, but they didn't seem to be the same thing, as far as I can tell. If it's the same, I'm not sure how to apply it to my situation. [^] http://www.masonmc.com/2008/getting-system-colors-in-c/[^]

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      MichCl wrote:

      I have a current form that is either lime or red. I think that (due to background worker threads), the best way to check the current state is to check the color of the form.

      The way I read this, the forms' color determines the "state"? Usually, we keep it in a separate variable, and set the color based on that. I'm having trouble understanding the question; am I correct in guessing that you want to "read/access" a property from a background-worker?

      Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

      M 1 Reply Last reply
      0
      • L Lost User

        MichCl wrote:

        I have a current form that is either lime or red. I think that (due to background worker threads), the best way to check the current state is to check the color of the form.

        The way I read this, the forms' color determines the "state"? Usually, we keep it in a separate variable, and set the color based on that. I'm having trouble understanding the question; am I correct in guessing that you want to "read/access" a property from a background-worker?

        Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

        M Offline
        M Offline
        MichCl
        wrote on last edited by
        #3

        I'm having trouble recording the new state because I'm on a different thread than I started on (using background worker). But I was able to set the color of the background on this thread, so I can wait until I exit the other thread, read the color of the background, and set my state variable. I've tried looking into setting variables created in different threads, but had trouble with what was suggested, and so far I've been able to avoid the issue. For this case, I program something and have success, so I set the form color for success. Then I have to record the number of success' or failures, and that's the problem. My success/failure count and the form field it's shown in were created in a different thread. Any idea how to get the form color so I can increment my counts based on that?

        L 1 Reply Last reply
        0
        • M MichCl

          I'm having trouble recording the new state because I'm on a different thread than I started on (using background worker). But I was able to set the color of the background on this thread, so I can wait until I exit the other thread, read the color of the background, and set my state variable. I've tried looking into setting variables created in different threads, but had trouble with what was suggested, and so far I've been able to avoid the issue. For this case, I program something and have success, so I set the form color for success. Then I have to record the number of success' or failures, and that's the problem. My success/failure count and the form field it's shown in were created in a different thread. Any idea how to get the form color so I can increment my counts based on that?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          MichCl wrote:

          For this case, I program something and have success, so I set the form color for success. Then I have to record the number of success' or failures, and that's the problem. My success/failure count and the form field it's shown in were created in a different thread.

          Let's call that thread the mainthread or the UI-thread. The "something" you speak of, is that running in it's own thread? You could poll a property in the form from a thread, but that requires locking -something that's always cool to avoid.

          Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

          M 1 Reply Last reply
          0
          • L Lost User

            MichCl wrote:

            For this case, I program something and have success, so I set the form color for success. Then I have to record the number of success' or failures, and that's the problem. My success/failure count and the form field it's shown in were created in a different thread.

            Let's call that thread the mainthread or the UI-thread. The "something" you speak of, is that running in it's own thread? You could poll a property in the form from a thread, but that requires locking -something that's always cool to avoid.

            Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

            M Offline
            M Offline
            MichCl
            wrote on last edited by
            #5

            I could avoid locking and thread jumping by just querying the color of the background. Any idea how to do it?

            L A 2 Replies Last reply
            0
            • M MichCl

              I could avoid locking and thread jumping by just querying the color of the background. Any idea how to do it?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              You cannot; the background-color is owned by the form, owned by the mainthread. How are you querying this value from the worker-thread? Are you polling it, checking it every so seconds?

              Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

              1 Reply Last reply
              0
              • M MichCl

                I have a current form that is either lime or red. I think that (due to background worker threads), the best way to check the current state is to check the color of the form. How do I do this? I set it with

                SetBackground(Color.Lime);

                I saw the following threads, but they didn't seem to be the same thing, as far as I can tell. If it's the same, I'm not sure how to apply it to my situation. [^] http://www.masonmc.com/2008/getting-system-colors-in-c/[^]

                R Offline
                R Offline
                Ravi Bhavnani
                wrote on last edited by
                #7

                MichCl wrote:

                I have a current form that is either lime or red. ... check the color of the form. How do I do this?

                That doesn't smell right. The color of the form should be driven the state of an object, not the other way around.  It seems what you want to do is to raise an event when the status of your object changes (presumably from "good" to "bad").  Any number of observers can listen for this change and respond to it an appropriate manner. /ravi

                My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                1 Reply Last reply
                0
                • M MichCl

                  I could avoid locking and thread jumping by just querying the color of the background. Any idea how to do it?

                  A Offline
                  A Offline
                  Alan N
                  wrote on last edited by
                  #8

                  The Control.Invoke method has an object return type and so with an appropriate delegated method any information can be obtained from a Control. e.g.

                  private delegate Color ColourDelegate(Control c);
                  private void GetColourInfo(Control c) {
                    // With explicit method
                    Color bgcol = (Color)c.Invoke(new ColourDelegate(GetColour), c);
                    
                    // or with an anonymous method
                    Color bgcol2 = (Color)c.Invoke(new ColourDelegate(delegate { return c.BackColor; }));
                  }
                  
                  private Color GetColour(Control ctrl) {
                    return ctrl.BackColor;
                  }
                  

                  Alan.

                  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