Lionheart008
13-05-2009, 19:13
dear thinbasic users :)
theme UI/Textfile: how can I save a Text file ??? I am confused... :? have found this text input example and wanted to add a 'save button' %ID_Save... thought I have done all right.. but here's something missing.. perhaps anybody can give a little advice... thanks in advance... and you may beat me... when I am so clumsy ;)
USES "UI"
uses "console"
DIM Msg AS LONG '---Returned messages
DIM wParam AS LONG '---wParam in message pump
DIM lParam AS LONG '---lParam in message pump
DIM hDlg AS LONG '---Dialog handle
DIM sFile AS string
DIM sInput AS string '---Used to get input from field
DIM sBuffer AS string '---Used to set output buffer
dim wx as long '---Used to get client width
dim hy as long '---Used to get client height
DIM Counter AS LONG '---Counter to count output lines
'---Control IDs
%ID_TXTBUFFER = 10 '---Output field
%ID_LBLENTER = 20 '---Input field label
%ID_TXTENTER = 30 '---Input field
%ID_SAVE = 40
%ID_OPEN = 50
%ID_SAVE = 60
%ID_CANCEL = 70
'---Set starting width and height of the window
wx = 220
hy = 250
'---Create main dialog
DIALOG NEW 0, "Textsample using Enter key from input field", -1, -1, wx, hy, _
%WS_DLGFRAME OR _
%DS_CENTER OR _
%WS_CAPTION OR _
%WS_SYSMENU OR _
%WS_OVERLAPPEDWINDOW , _
0 TO hDlg
'---Show dialog
DIALOG SHOW MODELESS hDlg
'---Now start window message pump loop
WHILE IsWindow(hDlg)
'---Get the message and fill wParam and lParam
Msg = GetMessage(hDlg, wParam, lParam)
if Msg then printl msg, wparam, lparam
'---Test the returned message
SELECT CASE Msg
'---Message fired at the very beginning when dialog is initialized
'---We use it to add necessary controls
case %WM_INITDIALOG
CONTROL ADD TEXTBOX , hDlg, %ID_TXTBUFFER, "" , 2, 2, wx - 10, hy - 60, _
%WS_TABSTOP OR _
%ES_WANTRETURN OR _
%ES_MULTILINE OR _
%ES_AUTOHSCROLL OR _
%ES_AUTOVSCROLL OR _
%WS_VSCROLL OR _
%WS_HSCROLL
CONTROL ADD label , hDlg, %ID_LBLENTER, "Type some text and press <ENTER> key", 2, hy - 30, wx - 5, 12
CONTROL ADD TEXTBOX , hDlg, %ID_TXTENTER, "" , 2, hy - 20, wx - 5, 12, %WS_TABSTOP
CONTROL ADD BUTTON , hDlg, %ID_SAVE, "Save your text as ...", 55, 200, 80, 18, %WS_BORDER OR %WS_TABSTOP, %WS_EX_CLIENTEDGE
'---Configure the controls to automatically resize as needed
CONTROL SET RESIZE hDlg, %ID_TXTBUFFER, 1, 1, 1, 1 '---This control must resize every side
CONTROL SET RESIZE hDlg, %ID_LBLENTER , 1, 1, 0, 1 '---This control must have top size movable
CONTROL SET RESIZE hDlg, %ID_TXTENTER , 1, 1, 0, 1 '---This control must have top size movable
CONTROL SET RESIZE hDlg, %ID_SAVE , 1, 1, 1, 1 '---This control must have top size movable
'---In case of keyboard pressed ...
CASE %wm_keydown
'---Check which key using wParam
SELECT CASE wParam
'---In case of <ENTER> key ...
case 13
DoTheJob(%ID_TXTENTER, %ID_TXTBUFFER, "")
'end select
case 14
DoTheJob(%ID_TXTENTER, %ID_TXTBUFFER, %ID_SAVE "")
end select
'---Get the message and fill wParam and lParam
Msg = GetMessage(hDlg, wParam, lParam)
'---Now test the message
SELECT CASE Msg
CASE %WM_COMMAND
SELECT CASE wParam
CASE %ID_OPEN
sFile = OpenFile
if sFile <> "" then
CONTROL SET TEXT hDlg, 50, FILE_LOAD(sFile)
msgbox 0, "File " & sFile & " loaded into textbox"
else
msgbox 0, "No file selected"
end if
CASE %ID_SAVE
sFile = SaveFile
sFile = trim$(sFile)
if sFile <> "" then
CONTROL SET TEXT hDlg, 60, FILE_SAVE(sFile)
msgbox 0, "Save file name " & sFile & "." & $crlf & "Nothing done. Just an example."
else
msgbox 0, "No file selected, no save performed."
end if
END SELECT
CASE %WM_SYSCOMMAND
SELECT CASE wParam
CASE %SC_CLOSE
EXIT WHILE
END SELECT
'---Here we go in any other case
'---This is used like an idle time, when Msg = 0
case %WM_NOTIFY
printl "notify"
' case %WM_IDLE
'
' '---%WM_IDLE is in reality equal to zero
' '---It is like testing a CASE ELSE and in general can be used
' '---when you need to do something in idle time
'
' '---So, if idle, set back the focus to data entry field
' CONTROL set focus hDlg, %ID_TXTENTER
' doevents
'
END SELECT
end select
WEND
'---Close the dialog
DIALOG END hDlg
function DoTheJob(byval ID_In as long, byval ID_Out as long, AdditionalText as string) as long
control get text hDlg, ID_In to sInput
if trim$(sInput) = "" then
sInput = "<<<- Please type some text ->>>"
end if
Counter += 1
sInput = AdditionalText & format$(Counter, "0000") & " " & sInput
'control get text hDlg, ID_Out to sBuffer
control appendtotop text hDlg, ID_Out, sInput & $crlf '& sBuffer
control set text hDlg, ID_In, ""
CONTROL set focus hDlg, ID_In
end function
function OpenFile() as string
dim sFile as string
dim sFilter as string
sFilter = "thinBasic Files (*.tBasic, *.tBasicc)|*.tBasic;*.tBasicc|"
sFilter += "Basic Files (*.BAS, *.INC)|*.BAS;*.INC|"
sFilter += "Resource Files (*.RC)|*.RC|"
sFilter += "Help files (*.HLP)|*.HLP|"
sFilter += "Text Files (*.TXT)|*.TXT|"
sFilter += "Word processing (*.RTF)|*.RTF|"
sFilter += "All Files (*.*)|*.*"
sFile = Dialog_OpenFile(hDlg, _
"Open an file", _
DIR_GetCurrent, _
sFilter, _
"tBasic", _
%OFN_FILEMUSTEXIST OR %OFN_HIDEREADONLY or %OFN_ENABLESIZING)
function = sFile
end function
function SaveFile() as string
dim sFile as string
dim sFilter as string
sFilter = "thinBasic Files (*.tBasic, *.tBasicc)|*.tBasic;*.tBasicc|"
sFilter += "Basic Files (*.BAS, *.INC)|*.BAS;*.INC|"
sFilter += "Resource Files (*.RC)|*.RC|"
sFilter += "Help files (*.HLP)|*.HLP|"
sFilter += "Text Files (*.TXT)|*.TXT|"
sFilter += "Word processing (*.RTF)|*.RTF|"
sFilter += "All Files (*.*)|*.*"
sFile = Dialog_SaveFile(hDlg, _
"Open an file", _
DIR_GetCurrent, _
sFilter, _
"tBasic", _
%OFN_FILEMUSTEXIST OR %OFN_HIDEREADONLY or %OFN_ENABLESIZING)
function = sFile
end function
ciao, Lionheart
theme UI/Textfile: how can I save a Text file ??? I am confused... :? have found this text input example and wanted to add a 'save button' %ID_Save... thought I have done all right.. but here's something missing.. perhaps anybody can give a little advice... thanks in advance... and you may beat me... when I am so clumsy ;)
USES "UI"
uses "console"
DIM Msg AS LONG '---Returned messages
DIM wParam AS LONG '---wParam in message pump
DIM lParam AS LONG '---lParam in message pump
DIM hDlg AS LONG '---Dialog handle
DIM sFile AS string
DIM sInput AS string '---Used to get input from field
DIM sBuffer AS string '---Used to set output buffer
dim wx as long '---Used to get client width
dim hy as long '---Used to get client height
DIM Counter AS LONG '---Counter to count output lines
'---Control IDs
%ID_TXTBUFFER = 10 '---Output field
%ID_LBLENTER = 20 '---Input field label
%ID_TXTENTER = 30 '---Input field
%ID_SAVE = 40
%ID_OPEN = 50
%ID_SAVE = 60
%ID_CANCEL = 70
'---Set starting width and height of the window
wx = 220
hy = 250
'---Create main dialog
DIALOG NEW 0, "Textsample using Enter key from input field", -1, -1, wx, hy, _
%WS_DLGFRAME OR _
%DS_CENTER OR _
%WS_CAPTION OR _
%WS_SYSMENU OR _
%WS_OVERLAPPEDWINDOW , _
0 TO hDlg
'---Show dialog
DIALOG SHOW MODELESS hDlg
'---Now start window message pump loop
WHILE IsWindow(hDlg)
'---Get the message and fill wParam and lParam
Msg = GetMessage(hDlg, wParam, lParam)
if Msg then printl msg, wparam, lparam
'---Test the returned message
SELECT CASE Msg
'---Message fired at the very beginning when dialog is initialized
'---We use it to add necessary controls
case %WM_INITDIALOG
CONTROL ADD TEXTBOX , hDlg, %ID_TXTBUFFER, "" , 2, 2, wx - 10, hy - 60, _
%WS_TABSTOP OR _
%ES_WANTRETURN OR _
%ES_MULTILINE OR _
%ES_AUTOHSCROLL OR _
%ES_AUTOVSCROLL OR _
%WS_VSCROLL OR _
%WS_HSCROLL
CONTROL ADD label , hDlg, %ID_LBLENTER, "Type some text and press <ENTER> key", 2, hy - 30, wx - 5, 12
CONTROL ADD TEXTBOX , hDlg, %ID_TXTENTER, "" , 2, hy - 20, wx - 5, 12, %WS_TABSTOP
CONTROL ADD BUTTON , hDlg, %ID_SAVE, "Save your text as ...", 55, 200, 80, 18, %WS_BORDER OR %WS_TABSTOP, %WS_EX_CLIENTEDGE
'---Configure the controls to automatically resize as needed
CONTROL SET RESIZE hDlg, %ID_TXTBUFFER, 1, 1, 1, 1 '---This control must resize every side
CONTROL SET RESIZE hDlg, %ID_LBLENTER , 1, 1, 0, 1 '---This control must have top size movable
CONTROL SET RESIZE hDlg, %ID_TXTENTER , 1, 1, 0, 1 '---This control must have top size movable
CONTROL SET RESIZE hDlg, %ID_SAVE , 1, 1, 1, 1 '---This control must have top size movable
'---In case of keyboard pressed ...
CASE %wm_keydown
'---Check which key using wParam
SELECT CASE wParam
'---In case of <ENTER> key ...
case 13
DoTheJob(%ID_TXTENTER, %ID_TXTBUFFER, "")
'end select
case 14
DoTheJob(%ID_TXTENTER, %ID_TXTBUFFER, %ID_SAVE "")
end select
'---Get the message and fill wParam and lParam
Msg = GetMessage(hDlg, wParam, lParam)
'---Now test the message
SELECT CASE Msg
CASE %WM_COMMAND
SELECT CASE wParam
CASE %ID_OPEN
sFile = OpenFile
if sFile <> "" then
CONTROL SET TEXT hDlg, 50, FILE_LOAD(sFile)
msgbox 0, "File " & sFile & " loaded into textbox"
else
msgbox 0, "No file selected"
end if
CASE %ID_SAVE
sFile = SaveFile
sFile = trim$(sFile)
if sFile <> "" then
CONTROL SET TEXT hDlg, 60, FILE_SAVE(sFile)
msgbox 0, "Save file name " & sFile & "." & $crlf & "Nothing done. Just an example."
else
msgbox 0, "No file selected, no save performed."
end if
END SELECT
CASE %WM_SYSCOMMAND
SELECT CASE wParam
CASE %SC_CLOSE
EXIT WHILE
END SELECT
'---Here we go in any other case
'---This is used like an idle time, when Msg = 0
case %WM_NOTIFY
printl "notify"
' case %WM_IDLE
'
' '---%WM_IDLE is in reality equal to zero
' '---It is like testing a CASE ELSE and in general can be used
' '---when you need to do something in idle time
'
' '---So, if idle, set back the focus to data entry field
' CONTROL set focus hDlg, %ID_TXTENTER
' doevents
'
END SELECT
end select
WEND
'---Close the dialog
DIALOG END hDlg
function DoTheJob(byval ID_In as long, byval ID_Out as long, AdditionalText as string) as long
control get text hDlg, ID_In to sInput
if trim$(sInput) = "" then
sInput = "<<<- Please type some text ->>>"
end if
Counter += 1
sInput = AdditionalText & format$(Counter, "0000") & " " & sInput
'control get text hDlg, ID_Out to sBuffer
control appendtotop text hDlg, ID_Out, sInput & $crlf '& sBuffer
control set text hDlg, ID_In, ""
CONTROL set focus hDlg, ID_In
end function
function OpenFile() as string
dim sFile as string
dim sFilter as string
sFilter = "thinBasic Files (*.tBasic, *.tBasicc)|*.tBasic;*.tBasicc|"
sFilter += "Basic Files (*.BAS, *.INC)|*.BAS;*.INC|"
sFilter += "Resource Files (*.RC)|*.RC|"
sFilter += "Help files (*.HLP)|*.HLP|"
sFilter += "Text Files (*.TXT)|*.TXT|"
sFilter += "Word processing (*.RTF)|*.RTF|"
sFilter += "All Files (*.*)|*.*"
sFile = Dialog_OpenFile(hDlg, _
"Open an file", _
DIR_GetCurrent, _
sFilter, _
"tBasic", _
%OFN_FILEMUSTEXIST OR %OFN_HIDEREADONLY or %OFN_ENABLESIZING)
function = sFile
end function
function SaveFile() as string
dim sFile as string
dim sFilter as string
sFilter = "thinBasic Files (*.tBasic, *.tBasicc)|*.tBasic;*.tBasicc|"
sFilter += "Basic Files (*.BAS, *.INC)|*.BAS;*.INC|"
sFilter += "Resource Files (*.RC)|*.RC|"
sFilter += "Help files (*.HLP)|*.HLP|"
sFilter += "Text Files (*.TXT)|*.TXT|"
sFilter += "Word processing (*.RTF)|*.RTF|"
sFilter += "All Files (*.*)|*.*"
sFile = Dialog_SaveFile(hDlg, _
"Open an file", _
DIR_GetCurrent, _
sFilter, _
"tBasic", _
%OFN_FILEMUSTEXIST OR %OFN_HIDEREADONLY or %OFN_ENABLESIZING)
function = sFile
end function
ciao, Lionheart