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. Retaining selection on a ListCtrl Item

Retaining selection on a ListCtrl Item

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
6 Posts 2 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
    Donguy1976
    wrote on last edited by
    #1

    Hello all, I have a dialog with a ListControl and some other buttons and edit boxes. The ListControl has bunch of items in it. Now when the user clicks on one item in the ListControl, that particular item gets highlighted in blue color. After the selection if user goes on to click on a edit box or buttons then the blue selection color disappears. Is it possible to persist with the color even though user clicks on other controls in the same dialog? How to do that? Thanks in advance.

    S 1 Reply Last reply
    0
    • D Donguy1976

      Hello all, I have a dialog with a ListControl and some other buttons and edit boxes. The ListControl has bunch of items in it. Now when the user clicks on one item in the ListControl, that particular item gets highlighted in blue color. After the selection if user goes on to click on a edit box or buttons then the blue selection color disappears. Is it possible to persist with the color even though user clicks on other controls in the same dialog? How to do that? Thanks in advance.

      S Offline
      S Offline
      SoMad
      wrote on last edited by
      #2

      In the dialog resource editor, right-click the list control and select Properties. Set the "Always Show Selection" property to TRUE. That's it. I don't know why the default is not TRUE for this property - I always end up setting it like that. Soren Madsen

      "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

      D 1 Reply Last reply
      0
      • S SoMad

        In the dialog resource editor, right-click the list control and select Properties. Set the "Always Show Selection" property to TRUE. That's it. I don't know why the default is not TRUE for this property - I always end up setting it like that. Soren Madsen

        "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

        D Offline
        D Offline
        Donguy1976
        wrote on last edited by
        #3

        That doesn't work. I am looking for something by which the blue highlight will stay on the list item.

        S 1 Reply Last reply
        0
        • D Donguy1976

          That doesn't work. I am looking for something by which the blue highlight will stay on the list item.

          S Offline
          S Offline
          SoMad
          wrote on last edited by
          #4

          Donguy1976 wrote:

          I am looking for something by which the blue highlight will stay on the list item.

          Yes, that is what that property does for you. Actually, it should be shown as gray, not blue to indicate the does not have focus, but at least it shows the selection. Do you by any chance override the list control styles during initialization? If so, you should include LVS_SHOWSELALWAYS. Soren Madsen

          "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

          D 1 Reply Last reply
          0
          • S SoMad

            Donguy1976 wrote:

            I am looking for something by which the blue highlight will stay on the list item.

            Yes, that is what that property does for you. Actually, it should be shown as gray, not blue to indicate the does not have focus, but at least it shows the selection. Do you by any chance override the list control styles during initialization? If so, you should include LVS_SHOWSELALWAYS. Soren Madsen

            "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

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

            I want it to be BLUE and not GRAY. Anyone know how to do that?

            S 1 Reply Last reply
            0
            • D Donguy1976

              I want it to be BLUE and not GRAY. Anyone know how to do that?

              S Offline
              S Offline
              SoMad
              wrote on last edited by
              #6

              Things like that are normally done by overloading the list controls OnCustomdraw(). To do this, you use your own class derived from CListCtrl. If you want more information on that, check this (or you can Google): http://msdn.microsoft.com/en-us/library/ms364048(v=vs.80).aspx[^] Here is an OnCustomdraw() I copied from existing code and modified to do what you want it to do (hopefully). It is untested, so I don't actually know if it will work correctly (or even compile), but go ahead and try it out.

              void CYourListCtrl::OnCustomdraw(NMHDR* pNMHDR, LRESULT* pResult)
              {
              NMLVCUSTOMDRAW* pLVCD = reinterpret_cast( pNMHDR );

              // Take the default processing unless we set this to something else below.
              \*pResult = 0;
              
              // First thing - check the draw stage. If it's the control's prepaint
              // stage, then tell Windows we want messages for every item.
              if ( CDDS\_PREPAINT == pLVCD->nmcd.dwDrawStage )
              {
                  \*pResult = CDRF\_NOTIFYITEMDRAW;
              }
              else if ( CDDS\_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
              {
                  // This is the prepaint stage for an item. Here's where we set the
                  // item's text color. Our return value will tell Windows to draw the
                  // item itself, but it will use the new color we set here.
              
                  // Do not apply special behavior if the list control is disabled.
                  if (IsWindowEnabled())
                  {
                      int nItem = (int)pLVCD->nmcd.dwItemSpec;
              
                      // Always draw the selected item with the highlight color.
                      if (GetItemState(nItem, LVIS\_SELECTED | LVIS\_FOCUSED) != 0)
                      {
                          // Store the color back in the NMLVCUSTOMDRAW struct.
                          pLVCD->clrText = GetSysColor(COLOR\_HIGHLIGHTTEXT);
              
                          pLVCD->clrTextBk = GetSysColor(COLOR\_HIGHLIGHT);
              
                          // Tell Windows to paint the control itself.
                          \*pResult = CDRF\_DODEFAULT;
                      }
                  }
              }
              

              }

              Soren Madsen

              "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

              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