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. What is the magic incantation to select a row in a CListCtrl in report mode ?

What is the magic incantation to select a row in a CListCtrl in report mode ?

Scheduled Pinned Locked Moved C / C++ / MFC
questiondatabaselearning
4 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.
  • M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #1

    I've look at different (many) suggested solutions but they are always failing me in some regards. I have a list control in report mode with the "always show selection" flag set in the resource editor. I have a button that will set the selection on a item in the list (a row). The code to set the selection is :

    // the index is valid
    m_PositionListBox.SetFocus();
    m_PositionListBox.SetItemState(nItem, LVIS_SELECTED, LVIS_SELECTED);
    m_PositionListBox.SetItemState(nItem, LVIS_FOCUSED, LVIS_FOCUSED);

    When I do the above, I see the selection change (highlighted), but then just disappears. Am I missing something ?

    I'd rather be phishing!

    D 1 Reply Last reply
    0
    • M Maximilien

      I've look at different (many) suggested solutions but they are always failing me in some regards. I have a list control in report mode with the "always show selection" flag set in the resource editor. I have a button that will set the selection on a item in the list (a row). The code to set the selection is :

      // the index is valid
      m_PositionListBox.SetFocus();
      m_PositionListBox.SetItemState(nItem, LVIS_SELECTED, LVIS_SELECTED);
      m_PositionListBox.SetItemState(nItem, LVIS_FOCUSED, LVIS_FOCUSED);

      When I do the above, I see the selection change (highlighted), but then just disappears. Am I missing something ?

      I'd rather be phishing!

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

      Something seems amiss here. I dummied up a project with VS2008 and your code seems to do what is expected.

      CONTROL "",IDC_LIST1,"SysListView32",LVS_REPORT | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP,14,15,278,153
      ...
      BOOL CTempDlg::OnInitDialog()
      {
      CDialog::OnInitDialog();

      // m\_PositionListBox is a CListCtrl
      m\_PositionListBox.InsertColumn(0, \_T("Name"), LVCFMT\_LEFT, 100);
      
      m\_PositionListBox.InsertItem(0, \_T("Ford"));
      m\_PositionListBox.InsertItem(1, \_T("Chevrolet"));
      m\_PositionListBox.InsertItem(2, \_T("Honda"));
      m\_PositionListBox.InsertItem(3, \_T("Toyota"));
      m\_PositionListBox.InsertItem(4, \_T("GMC"));
      m\_PositionListBox.InsertItem(5, \_T("Jeep"));
      
      return TRUE;
      

      }
      ...
      void CTempDlg::OnBnClickedSet()
      {
      int nItem = 3;
      m_PositionListBox.SetFocus();
      m_PositionListBox.SetItemState(nItem, LVIS_SELECTED, LVIS_SELECTED);
      }

      When I click the Set button, item #3 (Toyota) is selected (blue). If I then press the Tab key, focus goes to the OK button but item #3 stays selected (gray).

      "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

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      M 2 Replies Last reply
      0
      • D David Crow

        Something seems amiss here. I dummied up a project with VS2008 and your code seems to do what is expected.

        CONTROL "",IDC_LIST1,"SysListView32",LVS_REPORT | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP,14,15,278,153
        ...
        BOOL CTempDlg::OnInitDialog()
        {
        CDialog::OnInitDialog();

        // m\_PositionListBox is a CListCtrl
        m\_PositionListBox.InsertColumn(0, \_T("Name"), LVCFMT\_LEFT, 100);
        
        m\_PositionListBox.InsertItem(0, \_T("Ford"));
        m\_PositionListBox.InsertItem(1, \_T("Chevrolet"));
        m\_PositionListBox.InsertItem(2, \_T("Honda"));
        m\_PositionListBox.InsertItem(3, \_T("Toyota"));
        m\_PositionListBox.InsertItem(4, \_T("GMC"));
        m\_PositionListBox.InsertItem(5, \_T("Jeep"));
        
        return TRUE;
        

        }
        ...
        void CTempDlg::OnBnClickedSet()
        {
        int nItem = 3;
        m_PositionListBox.SetFocus();
        m_PositionListBox.SetItemState(nItem, LVIS_SELECTED, LVIS_SELECTED);
        }

        When I click the Set button, item #3 (Toyota) is selected (blue). If I then press the Tab key, focus goes to the OK button but item #3 stays selected (gray).

        "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

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

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

        THanks, I will bake a dummy project on my side either, see if there's something different. :thumbsup:

        I'd rather be phishing!

        1 Reply Last reply
        0
        • D David Crow

          Something seems amiss here. I dummied up a project with VS2008 and your code seems to do what is expected.

          CONTROL "",IDC_LIST1,"SysListView32",LVS_REPORT | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP,14,15,278,153
          ...
          BOOL CTempDlg::OnInitDialog()
          {
          CDialog::OnInitDialog();

          // m\_PositionListBox is a CListCtrl
          m\_PositionListBox.InsertColumn(0, \_T("Name"), LVCFMT\_LEFT, 100);
          
          m\_PositionListBox.InsertItem(0, \_T("Ford"));
          m\_PositionListBox.InsertItem(1, \_T("Chevrolet"));
          m\_PositionListBox.InsertItem(2, \_T("Honda"));
          m\_PositionListBox.InsertItem(3, \_T("Toyota"));
          m\_PositionListBox.InsertItem(4, \_T("GMC"));
          m\_PositionListBox.InsertItem(5, \_T("Jeep"));
          
          return TRUE;
          

          }
          ...
          void CTempDlg::OnBnClickedSet()
          {
          int nItem = 3;
          m_PositionListBox.SetFocus();
          m_PositionListBox.SetItemState(nItem, LVIS_SELECTED, LVIS_SELECTED);
          }

          When I click the Set button, item #3 (Toyota) is selected (blue). If I then press the Tab key, focus goes to the OK button but item #3 stays selected (gray).

          "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

          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

          M Offline
          M Offline
          Maximilien
          wrote on last edited by
          #4

          I've got it working with

          m_PositionListBox.SetItemState(-1, 0, LVIS_SELECTED| LVIS_FOCUSED);
          m_PositionListBox.SetItemState(currentStep - 1, LVIS_SELECTED| LVIS_FOCUSED, LVIS_SELECTED| LVIS_FOCUSED);

          Thanks.

          I'd rather be phishing!

          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