Author Topic: Listview problem  (Read 2241 times)

Brian Reynolds

  • Newbie
  • *
  • Posts: 22
    • View Profile
Listview problem
« on: February 24, 2022, 09:20:24 pm »
I am using two listviews so that data in Listview1 can be multiplied by data in Listview2 and wish to highlight the items being used in both Listviews.
What I need advice on is how to colour the background on an item when I click on it in Listview1 and again on an item in Listview2.
I am aware that when and item is clicked on it appears in white text with a blue background but immediately the listview loses focus it reverts to its original appearance.
Can this be done?

Chris Boss

  • Administrator
  • Jr. Member
  • *****
  • Posts: 60
    • View Profile
Re: Listview problem
« Reply #1 on: February 25, 2022, 12:03:24 am »
The easiest approach is to use CustomDraw !

In the codeclips folder is an example using a Listview (custdrw1.bas) :

Here is the code:

Code: [Select]
' *************************************************************************************
'                    Portions: Copyright Christoper R. Boss, 2003 to 2011
'                                    All Rights Reserved !
'                 Registered EZGUI 5.0 users may use this code Royalty Free !
' *************************************************************************************
#COMPILE EXE
#DIM ALL        '   This is helpful to prevent errors in coding
' --------------------
#INCLUDE "..\includes\ezgui50.inc"                          ' EZGUI Include file for Declares

' --------------------
' *************************************************************************************



' *************************************************************************************
'                         Application Constants and Declares
' *************************************************************************************
DECLARE SUB Form1_Display(BYVAL Parent$)
DECLARE SUB Form1_Design()
DECLARE SUB Form1_Events(CID&, CMsg&, CVal&, Cancel&)
' ------------------------------------------------

%FORM1_LISTVIEW1          = 100
' ------------------------------------------------

DECLARE SUB FORM1_LISTVIEW1_Fill(BYVAL Mode&)

' *************************************************************************************
'                         Application Global Variables and Types
' *************************************************************************************



'    Note:     Do NOT change the names of the EZGUI Callback Procedures !

' --------------------
#INCLUDE "..\includes\ezwmain50.inc"                          ' EZGUI Include file for WinMain
' --------------------
' *************************************************************************************
'                               EZGUI Program Control Functions
' *************************************************************************************

SUB EZ_Main(VerNum&)
    EZ_DefFont 6, "Arial", 12, "BIV"
    EZ_DefFont 7, "Arial", 12, "BV"
    EZ_DefFont 8, "Arial", 12, "V"
    EZ_DefFont 9, "Arial", 12, "IV"
    Form1_Display ""
END SUB

' -------------------------------------------------------------------------------------

SUB EZ_DesignWindow(FormName$)
    '      - NOTE:   EZGUI passes back Form Name in uppercase letters
    SELECT CASE FormName$
        CASE "FORM1"
            Form1_Design
        CASE ELSE
    END SELECT
END SUB

' -------------------------------------------------------------------------------------

SUB EZ_Events(FormName$, CID&, CMsg&, CVal&, Cancel&)
    '      - NOTE:   EZGUI passes back Form Name in uppercase letters
    SELECT CASE FormName$
        CASE "FORM1"
            Form1_Events CID&, CMsg&, CVal&, Cancel&
        CASE ELSE
    END SELECT
END SUB

' -------------------------------------------------------------------------------------






' *************************************************************************************
'                                 Put Your Code Here
' *************************************************************************************

SUB Form1_Display(BYVAL Parent$)
    EZ_Color -1, -1
    EZ_Form "FORM1", Parent$, "Your Dialog", 0, 0, 50, 18, "CK"
END SUB
' ------------------------------------------------

GLOBAL Form1_FF&

SUB Form1_Design()
    LOCAL FF&
    '---------------------------------------------------------------
    FF& =  9              '          -  Offset for Font Numbers
    Form1_FF& = FF&          ' Global for ODButtons Draw code
    '---------------------------------------------------------------
    EZ_Color-1,-1
    EZ_UseFont -1
    EZ_UseFont 6
    EZ_ListView %FORM1_LISTVIEW1, 1, 1, 48, 15, "Column 1{15}|Column 2{15}{C}|Column 3{15}{R}|", "SVT-#"
    FORM1_LISTVIEW1_Fill -1
    ' --------------------------------------------------------------
END SUB
' ------------------------------------------------

SUB Form1_Events(CID&, CMsg&, CVal&, Cancel&)
    LOCAL SI&, I&
    SELECT CASE CID&
        CASE %EZ_Window
            IF CMsg&=%EZ_Close THEN
            END IF
        CASE  %FORM1_LISTVIEW1
            IF CMsg&=%EZ_NoCustomDraw THEN Cancel&=1
            IF CMsg&=%EZ_CustomDraw THEN
                I&=EZ_GetCDrawItem(CVal&, SI&)
                SELECT CASE SI&
                    CASE 0
                        EZ_SetCDrawItem CVal&, 7, 4,-1
                    CASE 1
                        EZ_SetCDrawItem CVal&, 8, 2,-1
                    CASE 2
                        EZ_SetCDrawItem CVal&, 9, 1,-1
                    CASE ELSE
                        EZ_SetCDrawItem CVal&, 6, 0,-1
                END SELECT
            END IF
        CASE ELSE
    END SELECT
END SUB
' ------------------------------------------------


' ------------------------------------------------

SUB FORM1_LISTVIEW1_Fill(BYVAL Mode&)
    LOCAL R&, C&, Tmp$
    IF Mode&=-1 THEN    ' Initial Data
        FOR R&=0 TO 50    ' Rows
            FOR C&=0 TO 5    ' Columns
                Tmp$="Item "+RIGHT$("00"+LTRIM$(STR$(R&)),2)+","+STR$(C&)
                EZ_AddLVItem "Form1", %FORM1_LISTVIEW1, Tmp$, 0, R&, C&, ""
            NEXT C&
        NEXT R&
    END IF
END SUB

' ------------------------------------------------

Chris Boss

  • Administrator
  • Jr. Member
  • *****
  • Posts: 60
    • View Profile
Re: Listview problem
« Reply #2 on: February 25, 2022, 12:05:46 am »
A more complex approach is to use OnwerDraw.

From an example in the codeclips folder (cdrawlv.bas):

Code: [Select]
' *************************************************************************************
'                    Portions: Copyright Christoper R. Boss, 2003 to 2011
'                                    All Rights Reserved !
'                 Registered EZGUI 5.0 users may use this code Royalty Free !
' *************************************************************************************
#COMPILE EXE
#DIM ALL
' --------------------
#INCLUDE "..\includes\ezgui50.inc"
' --------------------

DECLARE SUB Form1_Display(BYVAL Parent$)
DECLARE SUB Form1_Design()
DECLARE SUB Form1_Events(CID&, CMsg&, CVal&, Cancel&)
' ------------------------------------------------
%FORM1_LISTVIEW1          = 105
' ------------------------------------------------
DECLARE SUB FORM1_LISTVIEW1_Fill(BYVAL Mode&)
DECLARE SUB FORM1_LISTVIEW1_Change(BYVAL CVal&)
DECLARE SUB FORM1_LISTVIEW1_Select(BYVAL CVal&)

' --------------------
#INCLUDE "..\includes\ezwmain50.inc"
' --------------------

SUB EZ_Main(VerNum&)
    EZ_DefFont 6, "Arial", 14, "BVA"
    EZ_DefFont 7, "Arial", 10, "VA"
    EZ_DefFont 8, "Arial", 10, "BVA"
    EZ_DefFont 9, "Arial", 10, "IVA"
    EZ_LoadPatternLib "", ""
    Form1_Display ""
END SUB

SUB EZ_DesignWindow(FormName$)
    SELECT CASE FormName$
        CASE "FORM1"
            Form1_Design
        CASE ELSE
    END SELECT
END SUB

SUB EZ_Events(FormName$, CID&, CMsg&, CVal&, Cancel&)
    SELECT CASE FormName$
        CASE "FORM1"
            Form1_Events CID&, CMsg&, CVal&, Cancel&
        CASE ELSE
    END SELECT
END SUB

SUB Form1_Display(BYVAL Parent$)
    EZ_Color -1, -1
    EZ_Form "FORM1", Parent$, "OwnerDraw Listview", 0, 0, 67, 25, "C"
END SUB

GLOBAL Form1_FF&

SUB Form1_Design()
    LOCAL FF&
    '---------------------------------------------------------------
    FF& =  9              '          -  Offset for Font Numbers
    Form1_FF& = FF&          ' Global for ODButtons Draw code
    '---------------------------------------------------------------
    EZ_UseFont 7
    EZ_Color 0,15
    EZ_ListView %FORM1_LISTVIEW1, 3, 2, 60, 20, "Column 1{12}|Column 2{12}{C}|Column 3{12}{C}|", "SV-O?"
    FORM1_LISTVIEW1_Fill -1
    ' --------------------------------------------------------------
END SUB
'
SUB SizeListViewItem(BYVAL CID&, BYVAL CVal&)
LOCAL W&, H&, Item1&, Item2&, NW!, NH!
IF EZ_StartOwnerSize(CVal&, W&, H&, Item1&, Item2&) THEN
    EZ_GetTextSize 7, "XXX", NW!, NH!,0
    H&=INT(NH!*1.5*2)
    EZ_EndOwnerSize CVal&, W&, H&
END IF
END SUB
'
SUB DrawListviewItem(BYVAL CID&, BYVAL CVal&)
LOCAL hMyDC&, X2&, Y2&, IsSel&, IsGry&, IsDis&, IsChk&, IsFoc&, IsDef&
LOCAL X1&, Y1&, Item1&, IsEdit&, SubItem&, SubItem2&, CP$
LOCAL SX1&, SY1&, SX2&, SY2&, CW&, AW!, AH!
LOCAL IText$, IPictNum&, LVProp$
IF EZ_StartOwnerDraw(CVal&, hMyDC&, X2&, Y2&, IsSel&, IsGry&, IsDis&, IsChk&, IsFoc&, IsDef&) THEN
    EZ_GetODItem CVal&, X1&, Y1&, Item1&, IsEdit&
    CP$=EZ_GetLVColOrder("Form1", CID&,3)
    IF Item1&>=0 THEN
        SX1&=0
        EZ_SaveState
        FOR SubItem&=0 TO 2
            CW&=EZ_GetLVColWidth("Form1", CID&, SubItem&)
            SX2&=SX1&+CW&-1
            IF IsSel& THEN
                EZ_Color 15, 9
            ELSE
                IF (SubItem& MOD 2)=1 THEN
                    EZ_Color 0, 31
                ELSE
                    EZ_Color 0, 23
                END IF
            END IF
            IF SubItem&=1 THEN
                ' draw as a button
                EZ_DCDraw  hMyDC&, %EZ_FILL, SX1&, Y1&, SX2&, Y2&, 1, 1
'                EZ_DCDraw  hMyDC&, %EZ_EDGE_R, SX1&, Y1&, SX2&, Y2&, 1, 1
            ELSE
                EZ_DCDraw  hMyDC&, %EZ_FILL, SX1&, Y1&, SX2&, Y2&, 1, 1
            END IF
            EZ_SaveState
            IF IsSel& THEN EZ_Color 0,0
            EZ_DCDraw  hMyDC&, %EZ_LINE, SX2&, Y1&, SX2&, Y2&, 1, 0
            EZ_DCDraw  hMyDC&, %EZ_LINE, SX1&, Y2&, SX2&, Y2&, 1, 0
            EZ_RestoreState
            EZ_UseFont 7+SubItem&     ' use different fonts
            SubItem2&=VAL(PARSE$(CP$,"|",SubItem&+1))   ' get actual column
            IF EZ_GetLVItem("Form1", CID&, IText$, IPictNum&, Item1&, SubItem2&, LVProp$ ) THEN
                EZ_GetTextSize 7+SubItem2&, IText$, AW!,AH!,0
                IF AW!>CW& THEN
                    EZ_SetPrintFormat 2, SX2&, Y2&, %EZ_LEFT OR %EZ_VCenter, 0
                ELSE
                    EZ_SetPrintFormat 2, SX2&, Y2&, %EZ_CENTER OR %EZ_VCenter, 0
                END IF
                EZ_DCPrint hMyDC&, SX1&,Y1&, IText$
            END IF
            SX1&=SX2&+1
        NEXT SubItem&
        EZ_RestoreState
        EZ_SetPrintFormat 0,0,0,0,0
    END IF
    EZ_EndOwnerDraw
END IF
END SUB
'
SUB Form1_Events(CID&, CMsg&, CVal&, Cancel&)
    SELECT CASE CID&
        CASE %EZ_Window
            IF CMsg&=%EZ_Close THEN
            END IF
        CASE  %FORM1_LISTVIEW1
            IF CMsg&=%EZ_Change THEN
                FORM1_LISTVIEW1_Change CVal&
            END IF
            IF CMsg&=%EZ_Selected THEN
                FORM1_LISTVIEW1_Select CVal&
            END IF
            IF CMsg&=%EZ_OwnerSize THEN
                SizeListViewItem CID&, CVal&
            END IF
            IF CMsg&=%EZ_OwnerDraw THEN
                DrawListviewItem CID&, CVal&
            END IF
        CASE ELSE
    END SELECT
END SUB

SUB FORM1_LISTVIEW1_Fill(BYVAL Mode&)
    LOCAL R&, C&, Tmp$
    IF Mode&=-1 THEN    ' Initial Data
        FOR R&=0 TO 50    ' Rows
            FOR C&=0 TO 5    ' Columns
                Tmp$="Item "+RIGHT$("00"+LTRIM$(STR$(R&)),2)+","+STR$(C&)+CHR$(13)+CHR$(10)+"OK -"+STR$(R&)
                EZ_AddLVItem "Form1", %FORM1_LISTVIEW1, Tmp$, 0, R&, C&, ""
            NEXT C&
        NEXT R&
    END IF
END SUB

SUB FORM1_LISTVIEW1_Change(BYVAL CVal&)
END SUB

SUB FORM1_LISTVIEW1_Select(BYVAL CVal&)
END SUB