PDA

View Full Version : opening 2 forms



hendryawan
11-02-2011, 06:24
Dear,
I already try the example from thin basic but not found the example for opening two forms.
In the microsoft VB6 use command:
Private Sub Command1_Click()
Form1.Show
Unload Me
End Sub
With command above, how I do in thin basic?
Please give me simple example in thin basic language.
thank you verymuch

best regard

hendry

ErosOlmi
11-02-2011, 08:15
Hi hendry, welcome to thinBasic community forum.

The best thing I can suggest is to have a look at the many UI (User Interface) examples you can find into \thinBasic\SampleScripts\UI\ directory.

Creating and opening one or more windows in thinBasic is not so close to VB6 way but more close to C.
I will try to find some spare time this evening (CET time) to post a simple example.

Ciao
Eros

Petr Schreiber
11-02-2011, 16:21
Hi Hendryawan,

you can get some information on UI programming with callback mechanism in article "Callbacks – new approach for UI programming" published in ThinBASIC Journal #2 (http://www.thinbasic.com/community/showthread.php?9450-ThinBasic-Journal-Issue-2).

In ThinBASIC, the dialogs are not represented as object with methods, but dialogs are identified by handle instead. This handle is automagically passed to callback functions (which take care of proper reaction to various events) as pre-filled variable Callback_Handle aka CBHNDL.

Here is little example of one dialog launching other dialog, both in modal mode (= dialog active at time):


'
' One dialog launching second dialog - MODAL edition :)
' Petr Schreiber
'

Uses "UI"

' -- ID numbers of controls
Begin ControlID
%bShowDialogLaunched
%bClose
End ControlID

' -- Here program starts
Function TBMain()

ShowDialogMain(%HWND_DESKTOP)

End Function

' -----------------------------------------------------------------------------
' Dialog 1
' -----------------------------------------------------------------------------
Function ShowDialogMain( hParent As Long )
Local hDlgMain As DWord

Dialog New Pixels, hParent, "Main dialog", 100, 100, 320, 240, _
%WS_POPUP Or %WS_VISIBLE Or %WS_CAPTION Or %WS_SYSMENU Or %WS_MINIMIZEBOX To hDlgMain

' -- Place controls here

' -- This button events are entirely handled in cbShowDialogLaunched routine
Control Add Button, hDlgMain, %bShowDialogLaunched, "Show dialog 2", 5, 100, 100, 25, Call cbShowDialogLaunched

' -- This button events are entirely handled dialog callback cbDialogMain, but could be handled in dedicated routine as well
Control Add Button, hDlgMain, %bClose, "Click to close", 215, 100, 100, 25

' -- All dialog events are handled in dialog callback
Dialog Show Modal hDlgMain, Call cbDialogMain

End Function

' -- Callback for dialog
CallBack Function cbDialogMain()

' -- Test for messages
Select Case Callback_Message

Case %WM_INITDIALOG
' -- Put code to be executed after dialog creation here

Case %WM_COMMAND
' -- You can handle controls here
Select Case Callback_Control
Case %bClose
If Callback_Control_Message = %BN_CLICKED Then
' -- Closes the dialog
Dialog End Callback_Handle
End If
End Select

Case %WM_CLOSE
' -- Put code to be executed before dialog end here

End Select

End Function

' -- Callback for launching button
CallBack Function cbShowDialogLaunched()

If Callback_Message = %WM_COMMAND Then
If Callback_Control_Message = %BN_CLICKED Then
ShowDialogLaunched(Callback_Handle)
End If
End If

End Function

' -----------------------------------------------------------------------------
' Dialog 2
' -----------------------------------------------------------------------------
Function ShowDialogLaunched( hParent As Long )
Local hDlgLaunched As DWord

Dialog New Pixels, hParent, "Launched dialog",-1,-1, 300, 300, _
%WS_POPUP Or %WS_VISIBLE Or %WS_CAPTION Or %WS_SYSMENU Or %WS_MINIMIZEBOX To hDlgLaunched

' -- Place controls here

' -- This button events are entirely handled dialog callback cbDialogMain, but could be handled in dedicated routine as well
Control Add Button, hDlgLaunched, %bClose, "Click to close", 100, 140, 100, 25

' -- All dialog events are handled in dialog callback
Dialog Show Modal hDlgLaunched, Call cbDialogLaunched

End Function

' -- Callback for dialog
CallBack Function cbDialogLaunched()

' -- Test for messages
Select Case Callback_Message

Case %WM_INITDIALOG
' -- Put code to be executed after dialog creation here

Case %WM_COMMAND
' -- You can handle controls here
Select Case Callback_Control
Case %bClose
If Callback_Control_Message = %BN_CLICKED Then
' -- Closes the dialog
Dialog End Callback_Handle
End If
End Select

Case %WM_CLOSE
' -- Put code to be executed before dialog end here

End Select

End Function
And here almost identical sample, with second dialog MODELESS, which means both main window and launched dialog can be active at the same time:


'
' One dialog launching second dialog - MODELESS edition :)
' Petr Schreiber
'

Uses "UI"

' -- ID numbers of controls
Begin ControlID
%bShowDialogLaunched
%bClose
End ControlID

' -- Here program starts
Function TBMain()

ShowDialogMain(%HWND_DESKTOP)

End Function

' -----------------------------------------------------------------------------
' Dialog 1
' -----------------------------------------------------------------------------
Function ShowDialogMain( hParent As Long )
Local hDlgMain As DWord

Dialog New Pixels, hParent, "Main dialog", 100, 100, 320, 240, _
%WS_POPUP Or %WS_VISIBLE Or %WS_CAPTION Or %WS_SYSMENU Or %WS_MINIMIZEBOX To hDlgMain

' -- Place controls here

' -- This button events are entirely handled in cbShowDialogLaunched routine
Control Add Button, hDlgMain, %bShowDialogLaunched, "Show dialog 2", 5, 100, 100, 25, Call cbShowDialogLaunched

' -- This button events are entirely handled dialog callback cbDialogMain, but could be handled in dedicated routine as well
Control Add Button, hDlgMain, %bClose, "Click to close", 215, 100, 100, 25

' -- All dialog events are handled in dialog callback
Dialog Show Modal hDlgMain, Call cbDialogMain

End Function

' -- Callback for dialog
CallBack Function cbDialogMain()

' -- Test for messages
Select Case Callback_Message

Case %WM_INITDIALOG
' -- Put code to be executed after dialog creation here

Case %WM_COMMAND
' -- You can handle controls here
Select Case Callback_Control
Case %bClose
If Callback_Control_Message = %BN_CLICKED Then
' -- Closes the dialog
Dialog End Callback_Handle
End If
End Select

Case %WM_CLOSE
' -- Put code to be executed before dialog end here

End Select

End Function

' -- Callback for launching button
CallBack Function cbShowDialogLaunched()

If Callback_Message = %WM_COMMAND Then
If Callback_Control_Message = %BN_CLICKED Then
ShowDialogLaunched(Callback_Handle)
End If
End If

End Function

' -----------------------------------------------------------------------------
' Dialog 2
' -----------------------------------------------------------------------------
Function ShowDialogLaunched( hParent As Long )
Static hDlgLaunched As DWord

If hDlgLaunched > 0 Then Exit Function ' -- That would mean window already exists

Dialog New Pixels, hParent, "Launched dialog",-1,-1, 300, 300, _
%WS_POPUP Or %WS_VISIBLE Or %WS_CAPTION Or %WS_SYSMENU Or %WS_MINIMIZEBOX To hDlgLaunched

' -- Place controls here

' -- This button events are entirely handled dialog callback cbDialogMain, but could be handled in dedicated routine as well
Control Add Button, hDlgLaunched, %bClose, "Click to close", 100, 140, 100, 25

' -- All dialog events are handled in dialog callback
Dialog Show Modeless hDlgLaunched, Call cbDialogLaunched

' -- We keep the window pumped and test its size
' -- If size is 0, window has been closed
Local width, height As DWord
Do
Dialog DoEvents 0
Dialog Get Client hDlgLaunched To width, height
Loop While width > 0

hDlgLaunched = 0

End Function

' -- Callback for dialog
CallBack Function cbDialogLaunched()

' -- Test for messages
Select Case Callback_Message

Case %WM_INITDIALOG
' -- Put code to be executed after dialog creation here

Case %WM_COMMAND
' -- You can handle controls here
Select Case Callback_Control
Case %bClose
If Callback_Control_Message = %BN_CLICKED Then
' -- Closes the dialog
Dialog End Callback_Handle
End If
End Select

Case %WM_CLOSE
' -- Put code to be executed before dialog end here

End Select

End Function

Petr

ErosOlmi
12-02-2011, 09:19
Thanks Petr, always very kind!

hendryawan
14-02-2011, 19:33
Dear Eros and Petr,
Thank you verymuch.
I has been tried, but I mean If I open dialog2 then dialog1 is disappear and if I close dialog2 then dialog1 is appear again and dialog2 is disappear.

Best regard

hendryawan

Petr Schreiber
14-02-2011, 20:15
Hi hendryawan,

if you need just to hide the dialog, you can use:


CallBack Function cbShowDialogLaunched()

If Callback_Message = %WM_COMMAND Then
If Callback_Control_Message = %BN_CLICKED Then
' -- First we hide dialog
Dialog Show State Callback_Handle, %SW_HIDE

' -- Show the new one
ShowDialogLaunched(Callback_Handle)

' -- After the launched dialog is closed, we show the original dialog again
Dialog Show State Callback_Handle, %SW_SHOW
End If
End If

End Function


Full code is attached to this post.


Petr

hendryawan
15-02-2011, 11:19
Thank Petr,
that example I need.
Ok I will continue my project related this example.

best regard

hendryawan