Announcement

Collapse
No announcement yet.

From VB to PB: The basics. But how?

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

  • From VB to PB: The basics. But how?

    Hi all
    I'm a VB6 programmer and would like to get way of the .Net, so decided to go with PowerBasic as my next step in programming. Just bought PB and PB Forms today and I'm trying my first window. It is a very simple one: a text box and a button. All I want is when I click the button the text in the textbox changes to something I define in the code.
    From the PB help, it says that when I add a control, a callback is added for that control with all the possible events (did I misundestood the help?).

    That is not happening. I only get a callback for the dialogShow. Why is that? Where are the callbacks for the controllers? The code I get is following:

    Code:
     
    #PBFORMS CREATED V1.51
    '------------------------------------------------------------------------------
    ' The first line in this file is a PB/Forms metastatement.
    ' It should ALWAYS be the first line of the file. Other
    ' PB/Forms metastatements are placed at the beginning and
    ' end of "Named Blocks" of code that should be edited
    ' with PBForms only. Do not manually edit or delete these
    ' metastatements or PB/Forms will not be able to reread
    ' the file correctly.  See the PB/Forms documentation for
    ' more information.
    ' Named blocks begin like this:    #PBFORMS BEGIN ...
    ' Named blocks end like this:      #PBFORMS END ...
    ' Other PB/Forms metastatements such as:
    '     #PBFORMS DECLARATIONS
    ' are used by PB/Forms to insert additional code.
    ' Feel free to make changes anywhere else in the file.
    '------------------------------------------------------------------------------
    #COMPILE EXE
    #DIM ALL
    '------------------------------------------------------------------------------
    '   ** Includes **
    '------------------------------------------------------------------------------
    #PBFORMS BEGIN INCLUDES
    #IF NOT %DEF(%WINAPI)
        #INCLUDE "WIN32API.INC"
    #ENDIF
    #PBFORMS END INCLUDES
    '------------------------------------------------------------------------------
    '------------------------------------------------------------------------------
    '   ** Constants **
    '------------------------------------------------------------------------------
    #PBFORMS BEGIN CONSTANTS
    %IDD_DIALOG1  =  101
    %IDC_BUTTON1  = 1001
    %IDC_TEXTBOX1 = 1002
    #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_BUTTON1
                        IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                            MSGBOX "%IDC_BUTTON1=" + FORMAT$(%IDC_BUTTON1), _
                                %MB_TASKMODAL
                        END IF
                    CASE %IDC_TEXTBOX1
                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
        DIALOG NEW hParent, "Dialog1", 296, 118, 201, 121, TO hDlg
        CONTROL ADD BUTTON,  hDlg, %IDC_BUTTON1, "Button1", 105, 85, 50, 15
        CONTROL ADD TEXTBOX, hDlg, %IDC_TEXTBOX1, "TextBox1", 45, 40, 100, 13
    #PBFORMS END DIALOG
        DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt
    #PBFORMS BEGIN CLEANUP %IDD_DIALOG1
    #PBFORMS END CLEANUP
        FUNCTION = lRslt
    END FUNCTION
    '------------------------------------------------------------------------------
    Last edited by Virginio Reis; 10 Mar 2008, 04:25 PM. Reason: Removed manualy entered test code

  • #2
    The button callback would have to be defined with the 'CONTROL ADD BUTTON' statement, but not necessary for what you want to do.

    If you'll notice, when you click the button, a Message Box displays '%IDC_BUTTON1=1001'.

    Remove the MSGBOX command, and use CONTROL SET TEXT to change the text in the textbox.
    Adam Drake
    PowerBASIC

    Comment


    • #3
      This is just a test. I want to see how the events appear. And the 'CONTROL ADD BUTTON' statement is there...

      Tryed that, but didn't work (??).

      Comment


      • #4
        change the text in the button handler:
        Code:
                   CONTROL SET TEXT CBHNDL, %IDC_TEXTBOX1, "MY TEXT"
                   'MSGBOX "%IDC_BUTTON1=" + FORMAT$(%IDC_BUTTON1), %MB_TASKMODAL

        Comment


        • #5
          You have the callback in there. The entire proc, not just a button callback. Look at this code:

          Code:
          #PBFORMS CREATED V1.51
          '------------------------------------------------------------------------------
          ' The first line in this file is a PB/Forms metastatement.
          ' It should ALWAYS be the first line of the file. Other
          ' PB/Forms metastatements are placed at the beginning and
          ' end of "Named Blocks" of code that should be edited
          ' with PBForms only. Do not manually edit or delete these
          ' metastatements or PB/Forms will not be able to reread
          ' the file correctly.  See the PB/Forms documentation for
          ' more information.
          ' Named blocks begin like this:    #PBFORMS BEGIN ...
          ' Named blocks end like this:      #PBFORMS END ...
          ' Other PB/Forms metastatements such as:
          '     #PBFORMS DECLARATIONS
          ' are used by PB/Forms to insert additional code.
          ' Feel free to make changes anywhere else in the file.
          '------------------------------------------------------------------------------
          
          #COMPILE EXE
          #DIM ALL
          
          '------------------------------------------------------------------------------
          '   ** Includes **
          '------------------------------------------------------------------------------
          #PBFORMS BEGIN INCLUDES 
          #IF NOT %DEF(%WINAPI)
              #INCLUDE "WIN32API.INC"
          #ENDIF
          #PBFORMS END INCLUDES
          '------------------------------------------------------------------------------
          
          '------------------------------------------------------------------------------
          '   ** Constants **
          '------------------------------------------------------------------------------
          #PBFORMS BEGIN CONSTANTS 
          %IDD_DIALOG1  =  101
          %IDC_BUTTON1  = 1001
          %IDC_TEXTBOX1 = 1002
          #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
                       'make sure textbox is clear
                       CONTROL SET TEXT CBHNDL,%IDC_TEXTBOX1,""
          
                  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_BUTTON1
                              IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                                  CONTROL SET TEXT CBHNDL,%IDC_TEXTBOX1,"You clicked the button"
                              END IF
          
                          CASE %IDC_TEXTBOX1
          
                      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
          
              DIALOG NEW hParent, "Dialog1", 70, 70, 201, 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 BUTTON,  hDlg, %IDC_BUTTON1, "Button1", 5, 5, 50, 15
              CONTROL ADD TEXTBOX, hDlg, %IDC_TEXTBOX1, "TextBox1", 50, 50, 100, 33, _
                  %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %ES_LEFT OR _
                  %ES_AUTOHSCROLL OR %ES_READONLY, %WS_EX_CLIENTEDGE OR %WS_EX_LEFT OR _
                  %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR
          #PBFORMS END DIALOG
          
              DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt
          
          #PBFORMS BEGIN CLEANUP %IDD_DIALOG1
          #PBFORMS END CLEANUP
          
              FUNCTION = lRslt
          END FUNCTION
          '------------------------------------------------------------------------------
          Barry

          Comment


          • #6
            One of the confusing thing between going from VB to PB is that with VB, the IDE generates the callback automatically for you, and will even generate the callback for the various subtypes of callbacks. Under the PB model, you are in total control over what you design into the code, drawback is you don't get that wonderful instant event handler from a double click in the IDE, but able to construct callbacks where all the events you want to capture are contained within one chunk of code, or several, depending on how you want to code things.

            My general preference is using the "global" dialog callback structure to handle all my events, which can make for a large main, (one of my projects has 1,300 lines of code in WinMain), but with the convenience of being able to "common" things with the Select Case functions where I got several buttons which act kin to one another with minor differences.
            Furcadia, an interesting online MMORPG in which you can create and program your own content.

            Comment


            • #7
              Similar to another thread being discussed here, if you're coming from a VB background and aren't really familiar with SDK style programming, I'd recommend taking a look at Paul Squires Firefly designer. He offers a 30 day evaluation, and I suspect that you'll find the development environment to be very familiar. There are differences due to the language and syntax, of course, but overall you'll probably find it to be much easier to jump into. Drop a button control on a form, double-click on the button, you switch to a code view for the click event handler for that button. Add your code, compile, run. Pretty much the same as you would with VB. Also, as a VB programmer, if there's ActiveX controls that you want to use, Firefly makes it a fairly simple process.

              My company develops networking libraries (DLLs) and ActiveX controls, and right now we offer PB examples written using our libraries with PBForms/DDT. We're going to be expanding that to also include console based examples, and we're also looking to include PB examples for our ActiveX controls. I'm going to recommend that for the ActiveX examples, we include ones written using Firefly.

              There are folks here who disagree and think that you should basically stick to straight PB/DDT code and learn about Windows SDK programming. While I understand the argument, it's not something that I completely agree with. Completely switching gears from the VB quasi-objected oriented style of programming to the SDK style is non-trivial for a lot of folks. And when the bottom line is productivity and rapid development -- the emphasis there being rapid -- then a designer like Firefly can turn the frustration of learning a new language into something that is not so fearsome. In short, it lowers the "barrier to entry" for VB programmers coming to PB, and that should be considered a good thing. I suggest checking it out.
              Mike Stefanik
              sockettools.com

              Comment


              • #8
                Very interesting discussion....

                I'm looking into FireFly...

                Comment


                • #9
                  I'm a .NET refugee too Virginio. I also have Firefly and can highly recommend it.

                  At...



                  ...is my api programming example series, and the third post in that series is Form3.bas, and it shows exactly how to get to...

                  Private Sub cmdButton1_Click()

                  End Sub

                  ...which I believe is where you are at with PowerBASIC. I just tested it and it seems to have come through the Forum transfer process OK (I posted it a year or two ago).
                  Fred
                  "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

                  Comment

                  Working...
                  X