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 / C++ / MFC
  4. How to keep contents of a TextBox in black when the TextBox is disabled?

How to keep contents of a TextBox in black when the TextBox is disabled?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
12 Posts 6 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.
  • D Offline
    D Offline
    dipuks
    wrote on last edited by
    #1

    Hello When a TextBox is disabled the contents of that will all become greyish. How to avoid that, and keep the contents in black even when the TextBox is in disabled state? Thanks in advance

    C _ K 3 Replies Last reply
    0
    • D dipuks

      Hello When a TextBox is disabled the contents of that will all become greyish. How to avoid that, and keep the contents in black even when the TextBox is in disabled state? Thanks in advance

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      You should really stick to the standards, otherwise your UI will be a hell to use. Maybe you don't want to disable the edit box but set it read-only[^] ?

      Cédric Moonen Software developer
      Charting control [v2.0] OpenGL game tutorial in C++

      1 Reply Last reply
      0
      • D dipuks

        Hello When a TextBox is disabled the contents of that will all become greyish. How to avoid that, and keep the contents in black even when the TextBox is in disabled state? Thanks in advance

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        Handle the WM_CTLCOLORSTATIC[^] message. This message is sent when a read-only or disables text box needs to be painted. The wParam parameter is the handle to the device context of the text box. You can select a black pen into this device context using SelectObject[^] which will make the text black. If you're using MFC override the OnCtlColor[^] function.

        «_Superman_» I love work. It gives me something to do between weekends.
        Microsoft MVP (Visual C++)

        D 2 Replies Last reply
        0
        • _ _Superman_

          Handle the WM_CTLCOLORSTATIC[^] message. This message is sent when a read-only or disables text box needs to be painted. The wParam parameter is the handle to the device context of the text box. You can select a black pen into this device context using SelectObject[^] which will make the text black. If you're using MFC override the OnCtlColor[^] function.

          «_Superman_» I love work. It gives me something to do between weekends.
          Microsoft MVP (Visual C++)

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

          Superman, Do you have any sample code that explains how to do this? Thanks

          D 1 Reply Last reply
          0
          • D dipuks

            Superman, Do you have any sample code that explains how to do this? Thanks

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            Does Google not work in your neighborhood? He gave you the functions/messages to use, so how hard can it be to find samples of them?

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            1 Reply Last reply
            0
            • D dipuks

              Hello When a TextBox is disabled the contents of that will all become greyish. How to avoid that, and keep the contents in black even when the TextBox is in disabled state? Thanks in advance

              K Offline
              K Offline
              krmed
              wrote on last edited by
              #6

              Instead of disabling the text box, just set it to read only (hine - ModifyStyle) - the text will stay black. Hope that helps.

              Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

              D 1 Reply Last reply
              0
              • K krmed

                Instead of disabling the text box, just set it to read only (hine - ModifyStyle) - the text will stay black. Hope that helps.

                Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

                D Offline
                D Offline
                dipuks
                wrote on last edited by
                #7

                Problem with setting to read only, is that it will let the user select the text using the mouse. Where as in disabled mode, it will not let any mouse selections. I tried using WM_CTLCOLORSTATIC but not able to make just the text BLACK

                K 1 Reply Last reply
                0
                • _ _Superman_

                  Handle the WM_CTLCOLORSTATIC[^] message. This message is sent when a read-only or disables text box needs to be painted. The wParam parameter is the handle to the device context of the text box. You can select a black pen into this device context using SelectObject[^] which will make the text black. If you're using MFC override the OnCtlColor[^] function.

                  «_Superman_» I love work. It gives me something to do between weekends.
                  Microsoft MVP (Visual C++)

                  D Offline
                  D Offline
                  dipuks
                  wrote on last edited by
                  #8

                  Superman, I am using an Edit Control. I tried using the WM_CTLCOLORSTATIC message and added the, SetBkCOlor and SetTextColor function. But this is not working Is it due to the fact that am using Edit Control? I also tried using SelectObject and passed a BLACK brush, but that also doesn't seem to work As soon as the Edit Control is disabled, the text in Edit Control becomes grayish.

                  _ 1 Reply Last reply
                  0
                  • D dipuks

                    Superman, I am using an Edit Control. I tried using the WM_CTLCOLORSTATIC message and added the, SetBkCOlor and SetTextColor function. But this is not working Is it due to the fact that am using Edit Control? I also tried using SelectObject and passed a BLACK brush, but that also doesn't seem to work As soon as the Edit Control is disabled, the text in Edit Control becomes grayish.

                    _ Offline
                    _ Offline
                    _Superman_
                    wrote on last edited by
                    #9

                    You have to use SelectObject and pass in a BLACK pen, not a brush.

                    «_Superman_» I love work. It gives me something to do between weekends.
                    Microsoft MVP (Visual C++)

                    D 1 Reply Last reply
                    0
                    • _ _Superman_

                      You have to use SelectObject and pass in a BLACK pen, not a brush.

                      «_Superman_» I love work. It gives me something to do between weekends.
                      Microsoft MVP (Visual C++)

                      D Offline
                      D Offline
                      dipuks
                      wrote on last edited by
                      #10

                      Inside WM_CTLCOLORSTATIC, i check whether lParam is equal to Edit Control handle, if thats TRUE then i CreatePen by passing RGB(0, 0, 0), the resultant PEN is then passed to SelectObject(HDC, PEN); But this is not working. Any idea why it's not? Do i need to "return" something for the WM_CTLCOLORSTATIC ? Like, GetStockObject or something?

                      PJ ArendsP 1 Reply Last reply
                      0
                      • D dipuks

                        Problem with setting to read only, is that it will let the user select the text using the mouse. Where as in disabled mode, it will not let any mouse selections. I tried using WM_CTLCOLORSTATIC but not able to make just the text BLACK

                        K Offline
                        K Offline
                        krmed
                        wrote on last edited by
                        #11

                        So what's wrong with letting the user select the text - they can't change it, and they may want to copy it and paste it in the clipboard or another app. (You can also disable the tab stop so they can't tab to the control but would have to use the mouse to select the text.

                        Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

                        1 Reply Last reply
                        0
                        • D dipuks

                          Inside WM_CTLCOLORSTATIC, i check whether lParam is equal to Edit Control handle, if thats TRUE then i CreatePen by passing RGB(0, 0, 0), the resultant PEN is then passed to SelectObject(HDC, PEN); But this is not working. Any idea why it's not? Do i need to "return" something for the WM_CTLCOLORSTATIC ? Like, GetStockObject or something?

                          PJ ArendsP Offline
                          PJ ArendsP Offline
                          PJ Arends
                          wrote on last edited by
                          #12

                          If your pen that you created is going out of scope when WM_CTLCOLORSTATIC returns then it will be destroyed and the control will be greyed. Watch your object lifetimes. If this does not help, then post some code.


                          You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

                          Within you lies the power for good; Use it!

                          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