How to pass many parameters to a FORM

Started by Jean-Pierre LEROY, July 05, 2009, 03:00:14 PM

Previous topic - Next topic

Jean-Pierre LEROY

Dear FireFly users,

Since version 2.1 of FireFly (if I remember well), we have the ability to pass a optional user defined long value to a Form. The value ("UserData") can be accessed in the WM_CREATE message of the FORM.

In some cases I have more than one long value to pass to a form; so to avoid using many global variables I define a user-defined Data Type (UDT) with all my parameters.

By example:


Type MyParameters
    Description As String * 50
    Column      As Long
    SortOrder   As Long
End Type


In FORM1, when the user clicks on a button I would like to open FORM2 and to pass the address of the structure MyParameters.


Function FORM1_COMMAND1_BN_CLICKED ( _
                                   ControlIndex     As Long,  _  ' index in Control Array
                                   hWndForm         As Dword, _  ' handle of Form
                                   hWndControl      As Dword, _  ' handle of Control
                                   idButtonControl  As Long   _  ' identifier of button
                                   ) As Long
                                   
    Local lMyParameters  As MyParameters
    Local lpMyParameters As MyParameters Ptr
                                                               
    ' Fill the parameters of the structure
    lMyParameters.Description = "How to pass many parameters between FORMs"
    lMyParameters.Column      = 12
    lMyParameters.SortOrder   =  1                                                                                         

    ' get the address of the UDT
    lpMyParameters = VarPtr(lMyParameters) 
   
    ' call the second FORM and convert the Double-Word value to Long value
    FORM2_SHOW (%HWND_DESKTOP, %FALSE, CLng(lpMyParameters))
   
End Function


Then in the WM_CREATE message of the FORM2.


Function FORM2_WM_CREATE ( _
                         hWndForm As Dword, _      ' handle of Form
                         ByVal UserData As Long _  ' optional user defined Long value
                         ) As Long
                         
    Local lpMyParameters As MyParameters Ptr

    ' convert the Long value to a Double-Word value           
    lpMyParameters = CDwd(UserData)                     
       
    MsgBox("Description="+@lpMyParameters.Description)
    MsgBox("Column="+Format$(@lpMyParameters.Column))
    MsgBox("SortOrder="+Format$(@lpMyParameters.SortOrder))
                           
End Function


It seems to work well that way; nevertheless I have some questions.

Q1. The UserData is defined as "Long" and not as "Dword" ? so is-it necessary to convert the Double-Word value returned by the VarPtr() function to a Long value with the CLng() function before passing the adress of the UDT ?
Q2. Is-it necessary to convert the Long value received in the WM_CREATE message of the function to a Double-Word value with the CDwd() function before affecting the address to a pointer to the UDT ?
Q3. To FireFly users: did you already use this feature to pass the address of a UDT to a FORM ?
Q4. To Paul, don't you think it could simpler if the UserData was a Double-Word variable and not a Long Variable ? could you change the type of this variable in a future version of FF ?

Thanks
Jean-Pierre

Jose Roca

Quote
Q1. The UserData is defined as "Long" and not as "Dword" ? so is-it necessary to convert the Double-Word value returned by the VarPtr() function to a Long value with the CLng() function before passing the adress of the UDT ?

No.

Quote
Q2. Is-it necessary to convert the Long value received in the WM_CREATE message of the function to a Double-Word value with the CDwd() function before affecting the address to a pointer to the UDT ?

No.

Quote
Q4. To Paul, don't you think it could simpler if the UserData was a Double-Word variable and not a Long Variable ? could you change the type of this variable in a future version of FF ?

Since no conversion is needed, it is irrelevant.

Jean-Pierre LEROY

Hi Jose,

Thank you for your answer.

Can you tell me a bit more why it is not necessary to make the conversion from Long value to Dword value and vice versa ?

Thanks
Jean-Pierre

Jose Roca

Because PB does it automatically.

From the help file:

Quote
These conversion functions are rarely needed as PowerBASIC automatically performs any necessary conversions when executing an assignment statement or passing parameters.