Author Topic: I do my best ..but..  (Read 4007 times)

0 Members and 1 Guest are viewing this topic.

Offline Robert Sarrazin

  • Newbie
  • *
  • Posts: 20
  • User-Rate: +0/-2
I do my best ..but..
« on: February 11, 2008, 03:49:33 PM »
Hi,
If you load this file you probably realize my problem.
When I click the next button I can't have the perfect result
It work one click after, and start very wrong.

I build this example to show you my problem.

Thank for your time :-[ 

Code: [Select]
#COMPILE EXE
#DIM ALL

'------------------------------------------------------------------------------
'   ** Includes **
'------------------------------------------------------------------------------

#IF NOT %DEF(%WINAPI)
    #INCLUDE "WIN32API.INC"
#ENDIF


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

'------------------------------------------------------------------------------
'   ** Constants **
'------------------------------------------------------------------------------

%IDD_DIALOG1  =  101
%IDC_LISTBOX1 = 1001
%IDC_LABEL1   = 1002
%IDC_Bnext    = 1003

'------------------------------------------------------------------------------
GLOBAL p AS LONG
GLOBAL txt AS STRING
'------------------------------------------------------------------------------
'   ** Declarations **
'------------------------------------------------------------------------------
DECLARE CALLBACK FUNCTION ShowDIALOG1Proc()
DECLARE FUNCTION SampleListBox(BYVAL hDlg AS DWORD, BYVAL lID AS LONG, BYVAL _
    lCount AS LONG) AS LONG
DECLARE FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG

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

'------------------------------------------------------------------------------
'   ** Main Application Entry Point **
'------------------------------------------------------------------------------
FUNCTION PBMAIN()
    ShowDIALOG1 %HWND_DESKTOP
END FUNCTION
'------------------------------------------------------------------------------

'------------------------------------------------------------------------------
'   ** CallBacks **
'------------------------------------------------------------------------------
CALLBACK FUNCTION ShowDIALOG1Proc()

    SELECT CASE AS LONG CBMSG
        CASE %WM_INITDIALOG
            ' Initialization handler
            LOCAL hList1 AS DWORD
        CASE %WM_NCACTIVATE
            STATIC hWndSaveFocus AS DWORD
            IF ISFALSE CBWPARAM THEN
                ' Save control focus
                hWndSaveFocus = GetFocus()
            ELSEIF hWndSaveFocus THEN
                ' Restore control focus
                SetFocus(hWndSaveFocus)
                hWndSaveFocus = 0
            END IF

        CASE %WM_COMMAND
            ' Process control notifications
            SELECT CASE AS LONG CBCTL
                CASE %IDC_LISTBOX1
                    IF CBCTLMSG = %LBN_SELCHANGE OR CBCTLMSG = %LBN_SETFOCUS THEN
                        CONTROL HANDLE CBHNDL, %IDC_LISTBOX1 TO hList1
                        p = SendMessage(hList1, %LB_GETCURSEL, 0, 0)
                        LISTBOX GET TEXT CBHNDL, %IDC_LISTBOX1 TO txt
                        CONTROL SET TEXT CBHNDL, %IDC_LABEL1, STR$(p) + "   " + txt
                    END IF
                   
                CASE %IDC_LABEL1

                CASE %IDC_Bnext
                    IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                        CONTROL SET FOCUS CBHNDL, %IDC_LISTBOX1

                        LISTBOX SELECT CBHNDL, %IDC_LISTBOX1, p+2

                    END IF

            END SELECT
    END SELECT
END FUNCTION
'------------------------------------------------------------------------------

'------------------------------------------------------------------------------
'   ** Sample Code **
'------------------------------------------------------------------------------
FUNCTION SampleListBox(BYVAL hDlg AS DWORD, BYVAL lID AS LONG, BYVAL lCount _
    AS LONG) AS LONG
     LISTBOX ADD hDlg, lID, "Robert"
     LISTBOX ADD hDlg, lID, "John"
     LISTBOX ADD hDlg, lID, "Frank"
     LISTBOX ADD hDlg, lID, "Paul"
     LISTBOX ADD hDlg, lID, "José"
     LISTBOX ADD hDlg, lID, "Rick"
     LISTBOX ADD hDlg, lID, "Tom"
     LISTBOX ADD hDlg, lID, "Tim"
     LISTBOX ADD hDlg, lID, "Julia"
     LISTBOX ADD hDlg, lID, "Aland"
     LISTBOX ADD hDlg, lID, "Richard"
     LISTBOX ADD hDlg, lID, "Jason"
     LISTBOX ADD hDlg, lID, "Jadan"
     LISTBOX ADD hDlg, lID, "Rod"
     LISTBOX ADD hDlg, lID, "Judith"
     LISTBOX ADD hDlg, lID, "Sylvie"
     LISTBOX SELECT hDlg, %IDC_LISTBOX1, 1

END FUNCTION
'------------------------------------------------------------------------------

'------------------------------------------------------------------------------
'   ** Dialogs **
'------------------------------------------------------------------------------
FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
    LOCAL lRslt  AS LONG


    LOCAL hDlg   AS DWORD
    LOCAL hFont1 AS DWORD
    LOCAL hFont2 AS DWORD

    DIALOG NEW hParent, "small problem", 70, 70, 252, 121, %WS_POPUP OR _
        %WS_BORDER OR %WS_DLGFRAME OR %WS_SYSMENU OR %WS_CLIPSIBLINGS OR _
        %WS_VISIBLE OR %DS_MODALFRAME OR %DS_3DLOOK OR %DS_NOFAILCREATE OR _
        %DS_SETFONT, %WS_EX_CONTROLPARENT OR %WS_EX_LEFT OR _
        %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR, TO hDlg
    CONTROL ADD LISTBOX, hDlg, %IDC_LISTBOX1, , 11, 12, 62, 101
    CONTROL ADD LABEL,   hDlg, %IDC_LABEL1, "Label1", 79, 12, 160, 40, _
        %WS_CHILD OR %WS_VISIBLE OR %SS_CENTER, %WS_EX_LEFT OR _
        %WS_EX_LTRREADING
    CONTROL ADD BUTTON,  hDlg, %IDC_Bnext, "Next", 180, 84, 47, 19

    CONTROL SEND hDlg, %IDC_LABEL1, %WM_SETFONT, hFont1, 0
    CONTROL SEND hDlg, %IDC_Bnext, %WM_SETFONT, hFont2, 0


    SampleListBox  hDlg, %IDC_LISTBOX1, 30

    DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt


    DeleteObject hFont1
    DeleteObject hFont2


    FUNCTION = lRslt
END FUNCTION
'------------------------------------------------------------------------------     

IF you use the mouse on the listbox work perfect or the arrow up or
down still work perfect, ONLY whent I try to use the button(Next) to
control the listbox. It may be not possible what I want or It might be
the best I can have, I let you check the problem if you fine beter
solution.

Nice problem thanks again :(

Offline Patrice Terrier

  • ROMs
  • Hero Member
  • *****
  • Posts: 934
  • User-Rate: +62/-1
    • www.zapsolution.com
Re: I do my best ..but..
« Reply #1 on: February 11, 2008, 06:05:43 PM »
Because DDT is a propriatary syntax of PowerBASIC, it is better to ask on their forum.

However you must be aware that a ListBox is zero based.
Negative value means error,
0 = 1
1 = 2
2 = 3
etc.

Here are some SDK code to handle ListBox using base 1 instead of 0.

Code: [Select]
SUB ListDeleteAll(BYVAL hList AS LONG)
    IF hList THEN CALL SendMessage(hList, %LB_RESETCONTENT, 0, 0)
END SUB

SUB ListDelete(BYVAL hList AS LONG, BYVAL nIndex AS LONG)
    IF nIndex > 0 THEN DECR nIndex '// Because ListBox is zero based
    IF hList THEN CALL SendMessage(hList, %LB_DELETESTRING, nIndex, 0)
END SUB

SUB ListSelectPlus(BYVAL hList AS LONG, BYVAL nSelected AS LONG)
    IF nSelected > 0 THEN DECR nSelected '// Because ListBox is zero based
    IF hList THEN CALL SendMessage(hList, %LB_SETCURSEL, nSelected, 0)
END SUB

FUNCTION ListFindString(BYVAL hList AS LONG, zTmp AS ASCIIZ) AS LONG
    LOCAL nIndex AS LONG
    nIndex = SendMessage(hList, %LB_FINDSTRING, -1, VARPTR(zTmp))
    IF nIndex > -1 THEN INCR nIndex: FUNCTION = nIndex
END FUNCTION

FUNCTION ListCount(BYVAL hList AS LONG) AS LONG
    IF hList THEN FUNCTION = SendMessage(hList, %LB_GETCOUNT, 0, 0)
END FUNCTION

FUNCTION ListGetCursel(BYVAL hList AS LONG) AS LONG
    LOCAL nSelected AS LONG
    IF hList THEN
       nSelected = SendMessage(hList, %LB_GETCURSEL, 0, 0)
       IF nSelected > -1 THEN INCR nSelected '// Because ListBox is zero based
    END IF
    FUNCTION = nSelected
END FUNCTION

FUNCTION ListAdd(BYVAL hList AS LONG, zTmp AS ASCIIZ) AS LONG
    IF hList THEN
       IF LEN(zTmp) THEN FUNCTION = SendMessage(hList, %LB_ADDSTRING, 0, VARPTR(zTmp))
    END IF
END FUNCTION

FUNCTION ListGetText(BYVAL hList AS LONG, BYVAL Item AS LONG) AS STRING
    LOCAL sItem AS STRING, nLEN AS LONG
    IF Item > 0 THEN
       DECR Item
       nLEN = SendMessage(hList, %LB_GETTEXTLEN, Item, 0)
       sItem = SPACE$(nLEN)
       CALL SendMessage(hList, %LB_GETTEXT, Item, BYVAL STRPTR(sItem))
       FUNCTION = sItem
    END IF
END FUNCTION
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2483
  • User-Rate: +204/-0
Re: I do my best ..but..
« Reply #2 on: February 11, 2008, 06:45:40 PM »
 
Hi Robert,

You have to change

Code: [Select]
p = SendMessage(hList1, %LB_GETCURSEL, 0, 0)

to:

Code: [Select]
p = SendMessage(hList1, %LB_GETCURSEL, 0, 0) + 1

Because SDK is zero based and DDT one based.

And change

Code: [Select]

                CASE %IDC_Bnext
                    IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                        CONTROL SET FOCUS CBHNDL, %IDC_LISTBOX1
                        LISTBOX SELECT CBHNDL, %IDC_LISTBOX1, p+2
                    END IF

to:

Code: [Select]

                CASE %IDC_Bnext
                    IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                        LISTBOX SELECT CBHNDL, %IDC_LISTBOX1, p+1
                        CONTROL SET FOCUS CBHNDL, %IDC_LISTBOX1
                    END IF

CONTROL SET FOCUS must be after LISTBOX SELECT. Otherwise, you will receive the LBN_SETFOCUS message before you have selected the item.

Here is the complete modified program:

Code: [Select]
#COMPILE EXE
#DIM ALL

'------------------------------------------------------------------------------
'   ** Includes **
'------------------------------------------------------------------------------

#IF NOT %DEF(%WINAPI)
    #INCLUDE "WIN32API.INC"
#ENDIF


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

'------------------------------------------------------------------------------
'   ** Constants **
'------------------------------------------------------------------------------

%IDD_DIALOG1  =  101
%IDC_LISTBOX1 = 1001
%IDC_LABEL1   = 1002
%IDC_Bnext    = 1003

'------------------------------------------------------------------------------
GLOBAL p AS LONG
GLOBAL txt AS STRING
'------------------------------------------------------------------------------
'   ** Declarations **
'------------------------------------------------------------------------------
DECLARE CALLBACK FUNCTION ShowDIALOG1Proc()
DECLARE FUNCTION SampleListBox(BYVAL hDlg AS DWORD, BYVAL lID AS LONG, BYVAL _
    lCount AS LONG) AS LONG
DECLARE FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG

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

'------------------------------------------------------------------------------
'   ** Main Application Entry Point **
'------------------------------------------------------------------------------
FUNCTION PBMAIN()
    ShowDIALOG1 %HWND_DESKTOP
END FUNCTION
'------------------------------------------------------------------------------

'------------------------------------------------------------------------------
'   ** CallBacks **
'------------------------------------------------------------------------------
CALLBACK FUNCTION ShowDIALOG1Proc()

    SELECT CASE AS LONG CBMSG
        CASE %WM_INITDIALOG
            ' Initialization handler
            LOCAL hList1 AS DWORD
        CASE %WM_NCACTIVATE
            STATIC hWndSaveFocus AS DWORD
            IF ISFALSE CBWPARAM THEN
                ' Save control focus
                hWndSaveFocus = GetFocus()
            ELSEIF hWndSaveFocus THEN
                ' Restore control focus
                SetFocus(hWndSaveFocus)
                hWndSaveFocus = 0
            END IF

        CASE %WM_COMMAND
            ' Process control notifications
            SELECT CASE AS LONG CBCTL
                CASE %IDC_LISTBOX1
                    IF CBCTLMSG = %LBN_SELCHANGE OR CBCTLMSG = %LBN_SETFOCUS THEN
                        CONTROL HANDLE CBHNDL, %IDC_LISTBOX1 TO hList1
                        p = SendMessage(hList1, %LB_GETCURSEL, 0, 0) + 1
                        LISTBOX GET TEXT CBHNDL, %IDC_LISTBOX1 TO txt
                        CONTROL SET TEXT CBHNDL, %IDC_LABEL1, STR$(p) + "   " + txt
                    END IF
                   
                CASE %IDC_LABEL1

                CASE %IDC_Bnext
                    IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                        LISTBOX SELECT CBHNDL, %IDC_LISTBOX1, p+1
                        CONTROL SET FOCUS CBHNDL, %IDC_LISTBOX1
                    END IF

            END SELECT
    END SELECT
END FUNCTION
'------------------------------------------------------------------------------

'------------------------------------------------------------------------------
'   ** Sample Code **
'------------------------------------------------------------------------------
FUNCTION SampleListBox(BYVAL hDlg AS DWORD, BYVAL lID AS LONG, BYVAL lCount _
    AS LONG) AS LONG
     LISTBOX ADD hDlg, lID, "Robert"
     LISTBOX ADD hDlg, lID, "John"
     LISTBOX ADD hDlg, lID, "Frank"
     LISTBOX ADD hDlg, lID, "Paul"
     LISTBOX ADD hDlg, lID, "José"
     LISTBOX ADD hDlg, lID, "Rick"
     LISTBOX ADD hDlg, lID, "Tom"
     LISTBOX ADD hDlg, lID, "Tim"
     LISTBOX ADD hDlg, lID, "Julia"
     LISTBOX ADD hDlg, lID, "Aland"
     LISTBOX ADD hDlg, lID, "Richard"
     LISTBOX ADD hDlg, lID, "Jason"
     LISTBOX ADD hDlg, lID, "Jadan"
     LISTBOX ADD hDlg, lID, "Rod"
     LISTBOX ADD hDlg, lID, "Judith"
     LISTBOX ADD hDlg, lID, "Sylvie"
     LISTBOX SELECT hDlg, %IDC_LISTBOX1, 1

END FUNCTION
'------------------------------------------------------------------------------

'------------------------------------------------------------------------------
'   ** Dialogs **
'------------------------------------------------------------------------------
FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
    LOCAL lRslt  AS LONG


    LOCAL hDlg   AS DWORD
    LOCAL hFont1 AS DWORD
    LOCAL hFont2 AS DWORD

    DIALOG NEW hParent, "small problem", 70, 70, 252, 121, %WS_POPUP OR _
        %WS_BORDER OR %WS_DLGFRAME OR %WS_SYSMENU OR %WS_CLIPSIBLINGS OR _
        %WS_VISIBLE OR %DS_MODALFRAME OR %DS_3DLOOK OR %DS_NOFAILCREATE OR _
        %DS_SETFONT, %WS_EX_CONTROLPARENT OR %WS_EX_LEFT OR _
        %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR, TO hDlg
    CONTROL ADD LISTBOX, hDlg, %IDC_LISTBOX1, , 11, 12, 62, 101
    CONTROL ADD LABEL,   hDlg, %IDC_LABEL1, "Label1", 79, 12, 160, 40, _
        %WS_CHILD OR %WS_VISIBLE OR %SS_CENTER, %WS_EX_LEFT OR _
        %WS_EX_LTRREADING
    CONTROL ADD BUTTON,  hDlg, %IDC_Bnext, "Next", 180, 84, 47, 19

    CONTROL SEND hDlg, %IDC_LABEL1, %WM_SETFONT, hFont1, 0
    CONTROL SEND hDlg, %IDC_Bnext, %WM_SETFONT, hFont2, 0


    SampleListBox  hDlg, %IDC_LISTBOX1, 30

    DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt


    DeleteObject hFont1
    DeleteObject hFont2


    FUNCTION = lRslt
END FUNCTION
'------------------------------------------------------------------------------


Offline Robert Sarrazin

  • Newbie
  • *
  • Posts: 20
  • User-Rate: +0/-2
Re: I do my best ..but..
« Reply #3 on: February 11, 2008, 10:22:07 PM »
Hi,

I test with inversion and result
perfect thanks again
                      :o WoW  :o