Announcement

Collapse
No announcement yet.

Manipulate Dialog from a Function

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Manipulate Dialog from a Function

    I have problem to manipulate dialog window from a function called by the callback!
    The code below is a simple example:
    I Expect that "%IDC_LABEL2" count from 0 to 100.

    Please help me!


    #COMPILE EXE
    #DIM ALL

    '------------------------------------------------------------------------------
    ' ** Includes **
    '------------------------------------------------------------------------------
    #PBFORMS BEGIN INCLUDES
    #IF NOT %DEF(%WINAPI)
    #INCLUDE "WIN32API.INC"
    #ENDIF
    #INCLUDE "PBForms.INC"
    #PBFORMS END INCLUDES
    '------------------------------------------------------------------------------

    '------------------------------------------------------------------------------
    ' ** Constants **
    '------------------------------------------------------------------------------
    #PBFORMS BEGIN CONSTANTS
    %IDD_DIALOG1 = 101
    %IDC_CALLFUNCTION = 1001
    %IDC_LABEL1 = 1002
    %IDC_LABEL2 = 1003
    #PBFORMS END CONSTANTS
    '------------------------------------------------------------------------------

    '------------------------------------------------------------------------------
    ' ** Declarations **
    '------------------------------------------------------------------------------
    DECLARE CALLBACK FUNCTION ShowDIALOG1Proc()
    DECLARE FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
    #PBFORMS DECLARATIONS
    '------------------------------------------------------------------------------

    '------------------------------------------------------------------------------
    ' ** 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

    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_CALLFUNCTION
    IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN

    CALL Funct

    END IF

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

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

    #PBFORMS BEGIN DIALOG %IDD_DIALOG1->->
    LOCAL hDlg AS DWORD
    LOCAL hFont1 AS DWORD

    DIALOG NEW hParent, "Dialog1", 136, 180, 90, 80, %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 BUTTON, hDlg, %IDC_CALLFUNCTION, "Call Function", 5, 5, 80, _
    20
    CONTROL ADD LABEL, hDlg, %IDC_LABEL1, "Function Step:", 20, 30, 50, 10
    CONTROL ADD LABEL, hDlg, %IDC_LABEL2, "0", 25, 45, 40, 25, %WS_CHILD OR _
    %WS_VISIBLE OR %SS_CENTER OR %SS_CENTERIMAGE, %WS_EX_LEFT OR _
    %WS_EX_LTRREADING

    hFont1 = PBFormsMakeFont("MS Sans Serif", 14, 700, %FALSE, %FALSE, _
    %FALSE, %ANSI_CHARSET)

    CONTROL SEND hDlg, %IDC_LABEL2, %WM_SETFONT, hFont1, 0
    #PBFORMS END DIALOG

    DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt

    #PBFORMS BEGIN CLEANUP %IDD_DIALOG1
    DeleteObject hFont1
    #PBFORMS END CLEANUP

    FUNCTION = lRslt
    END FUNCTION
    '------------------------------------------------------------------------------
    '------------------------------------------------------------------------------
    ' ** My Function **
    '------------------------------------------------------------------------------
    FUNCTION funct AS LONG
    LOCAL cont AS INTEGER
    FOR cont=1 TO 100

    ' This line dosen't work! Why?
    CONTROL SET TEXT %IDD_DIALOG1, %IDC_LABEL2, STR$(cont)
    ' This line is the same
    CONTROL SET TEXT 101, 1003, STR$(cont)


    SLEEP 20
    NEXT cont
    END FUNCTION

  • #2
    Please do wrap code with code tags, it helps reading.

    Note changes to the actual function and function call. %IDD_DIALOG1 is not the appropriate number to use.

    Function call:

    Code:
        CALL Funct(CBHNDL)
    Function:

    Code:
    FUNCTION funct(hDlg AS DWORD) AS LONG
        LOCAL cont AS INTEGER
        FOR cont=1 TO 100
            CONTROL SET TEXT hDlg, %IDC_LABEL2, STR$(cont)
        SLEEP 20
        NEXT cont
    END FUNCTION
    Adam Drake
    Drake Software

    Comment


    • #3
      Tank you!
      It's work!

      Comment


      • #4
        Cerrato, another way is to make the hDlg (depending on pb version) as a global variable, in which case your original code should work.

        Or instead of Funct(CBHNDL)
        lResult&=Funct(hDlg)

        FUNCTION(hWnd as dword) as long
        'do control statements
        CONTROL whatever hWnd,Control_id,whatever
        END FUNCTION

        Just to say there are several ways to get to the same result though Adam's is the most compact. However, if you start another dialog before the current
        one is closed, it may get confusing as to which dialog CBHNDL refers.
        Client Writeup for the CPA

        buffs.proboards2.com

        Links Page

        Comment


        • #5
          Originally posted by Fred Buffington View Post
          However, if you start another dialog before the current one is closed, it may get confusing as to which dialog CBHNDL refers.
          How can that be? CBHNDL only exists in the context of the dialog's callback function.

          Comment


          • #6
            I think that was a typo... I think the 'conflict' referred to the putative GLOBAL variable "hDlg"

            (I shall heroically resist using this opportunity to preach the evils of needless GLOBAL variables).
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              In this case MCM, I think a sermon is due for the almighty evils of Globals (this is a case in point, using "hDlg" for your dialog, and not a unique name for each Dialog)

              See you in sunrise service for this one

              Another service could be the evils of not only Globals, but the evils caused by a Global, that you also declared (even accidentally) as local to a function.
              (Do NOT get me started on what that mix of problems caused in the past
              Engineer's Motto: If it aint broke take it apart and fix it

              "If at 1st you don't succeed... call it version 1.0"

              "Half of Programming is coding"....."The other 90% is DEBUGGING"

              "Document my code????" .... "WHYYY??? do you think they call it CODE? "

              Comment


              • #8
                You got me wrong.

                GLOBALS are not inherently bad, but overuse and misuse can lead to big trouble.

                In any DDT callback function a GLOBAL variable for a handle to the dialog is needless, since the CBHNDL system variable is always available and that value can be passed to any called supporting function as a parameter.

                MCM
                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment

                Working...
                X