Listview Set Select Line

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
MattC
Posts: 114
Joined: Mon 16 Apr 2018, 06:17

Listview Set Select Line

Post by MattC »

Hi, can someone explain why I can't get the line to set select.

Code: Select all

      INSTALL @lib$ + "WINLIB5A"

      REM!WC
      LVIS_DROPHILITED = 8
      LVIF_TEXT = 1
      LVM_INSERTITEM = 4103
      LVM_SETITEMTEXT = 4142
      LVM_GETEXTENDEDLISTVIEWSTYLE = 4151
      LVM_SETEXTENDEDLISTVIEWSTYLE = 4150
      LVS_EX_FULLROWSELECT = 32
      LVS_EX_GRIDLINES = 1
      LVCF_FMT = 1
      LVCF_SUBITEM = 8
      LVCF_TEXT = 4
      LVCF_WIDTH = 2
      LVM_INSERTCOLUMN = 4123
      LVIF_PARAM = 4
      LVIF_STATE = 8
      LVIS_FOCUSED = 1
      LVIS_SELECTED = 2
      LVM_SETITEMSTATE = 4139
      LVS_REPORT = 1
      WS_CLIPCHILDREN = &2000000

      DIM LVCOLUMN{ mask%, fmt%, cx%, pszText%, cchTextMax%, iSubItem% }
      DIM LVITEM{mask%, iItem%, iSubItem%, state%, stateMask%, pszText%, cchTextMax%, iImage%, lParam%}

      DIM H$(3), W%(3), J%(3), A$(3)
      H$() = "Col 1", "Col 2", "Col 3", "Col 4"
      W%() = 50, 50, 50, 50
      J%() = 0, 0, 0, 0
      A$() = "test1", "test2", "test3", "test4"

      REM SETUP LISTVIEW
      LVCOLUMN.mask%   = LVCF_FMT OR LVCF_SUBITEM OR LVCF_TEXT OR LVCF_WIDTH
      SYS "InitCommonControls"
      lv% = FN_createwindow(@hwnd%, "SysListView32", "", 100, 100, 280, 180, 0, LVS_REPORT OR WS_CLIPCHILDREN, 0)
      FOR C% = 0 TO 3
        H$                   = H$(C%) + CHR$(0)
        LVCOLUMN.fmt%        = J%(C%) MOD 3
        LVCOLUMN.cx%         = W%(C%)
        LVCOLUMN.pszText%    = !^H$
        LVCOLUMN.cchTextMax% = LEN(H$)
        LVCOLUMN.iSubItem%   = C%
        SYS "SendMessage", lv%, LVM_INSERTCOLUMN, C%, LVCOLUMN{}
      NEXT
      REM GRIDLINES & FULLROW
      SYS "SendMessage", lv%, LVM_GETEXTENDEDLISTVIEWSTYLE TO S%
      SYS "SendMessage", lv%, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, S% OR LVS_EX_GRIDLINES OR LVS_EX_FULLROWSELECT

      LVITEM.mask%       = LVIF_TEXT OR LVIF_STATE OR LVIF_PARAM
      FOR I% = 0 TO 9
        FOR S% = 0 TO 3
          T$                 = A$(S%) + CHR$(0)
          LVITEM.iItem%      = I%
          LVITEM.iSubItem%   = S%
          LVITEM.pszText%    = !^T$
          IF S% THEN
            SYS "SendMessage", lv%, LVM_SETITEMTEXT, I%, LVITEM{}
          ELSE
            SYS "SendMessage", lv%, LVM_INSERTITEM, I%, LVITEM{}
          ENDIF
        NEXT
      NEXT

      ON SYS sysclick% = @wparam% : RETURN

      LVITEM.mask%      = LVIF_STATE    OR LVIF_PARAM
      LVITEM.state%     = LVIS_SELECTED OR LVIS_FOCUSED
      LVITEM.stateMask% = LVIS_SELECTED OR LVIS_FOCUSED
      LVITEM.iItem%     = 1: REM ???
      LVITEM.iSubItem%  = 0: REM ???
      LVITEM.lParam%    = 1: REM ???
      SYS "SendMessage", lv%, LVM_SETITEMSTATE, 1, LVITEM{}
      END
The LVM_SETITEMSTATE should set the indexed item 1 to selected - I think. It doesn't. Can someone please explain to me why it's not happening? I've been bashing this one for hours and MS help doesn't seem to give any clues. Adding ' OR LVIS_DROPHILITED ' to the state and stateMask does select the item but this isn't what I want as it permanently selects it (" as a drag and drop target"). I've tried the item, subitem and lparam, but these don't seem to work. I've also tried numerous other combinations, but to no avail. I'm obviously missing something. Help! :x

Matt
Hated Moron

Re: Listview Set Select Line

Post by Hated Moron »

MattC wrote: Wed 09 Feb 2022, 21:11 I've also tried numerous other combinations, but to no avail. I'm obviously missing something. Help! :x
You are successfully selecting the item, but because the List View doesn't currently have focus you can't see it. Simply set focus to the List View as follows and you will see the selection:

Code: Select all

      ...
      SYS "SendMessage", lv%, LVM_SETITEMSTATE, 1, LVITEM{}
      PROC_setfocus(lv%)
      END
Normal behaviour for a Windows control is not to highlight a selection unless it has focus. The Edit Control is an exception in that there is a special style ES_NOHIDESEL which will force the selection to remain highlighted even when it loses focus, but that is not generally available for other controls.
MattC
Posts: 114
Joined: Mon 16 Apr 2018, 06:17

Re: Listview Set Select Line

Post by MattC »

Thanks. Much appreciate your response. After I posted this, I played around for a good many days and finally came up with the same answer.

Matt
Hated Moron

Re: Listview Set Select Line (SOLVED)

Post by Hated Moron »

MattC wrote: Thu 14 Apr 2022, 16:38 I played around for a good many days and finally came up with the same answer.
It would have been helpful to report that or edit your original post accordingly. I spent a fair amount of time researching the problem and experimenting until I found the solution, which as it turns out was completely unnecessary. :x
MattC
Posts: 114
Joined: Mon 16 Apr 2018, 06:17

Re: Listview Set Select Line

Post by MattC »

You're absolutely right. My apologies. As no one had replied, I assumed no one was still watching it - not to mention that my memory isn't too good. I do appreciate the time you took, and I will certainly try to remember to do that in the future. Please accept my apologies. Thanks.

Matt