'For use in GFA Basic programs


In these examples we use file streaming to load and save data from or

to GFA Basic listbox and combobox objects and files.


This type of file input/output streams each line of data in or out line by line.

The buffer data is really the string.


We read the lines into the object from the test.dat file and add a line.

Then the contents are written to a new file named testhello.dat.


Several listboxes an comboboxes can use the same procedure in a

program once we place the name of the object in a string, for example

"lb1" or "cb1".


The filename and the object name are passed to the procedure parameter

list as strings.


Note: the comboboxes or listbox must be placed on the form with the

form editor first, unless the examples in the .zip file are used.



Click here to download GB32_Streaming_ListBox_Combobox_Files.zip



'Example 1: Listboxes


'GFA Basic Standard ListBox File Streaming IO


LoadForm frm1


Global ListBxNam As String

ListBxNam = "lb1"

Global Fnam As String


Fnam = App.Path + "\" + "Test" + ".dat"

@LoadFileStream_lb(Fnam, ListBxNam)


Global a$

a$ = "Hello World"

If ListBxNam = "lb1"

lb1.AddItem a$

EndIf


Fnam = App.Path + "\" + "Testhello" + ".dat"

@SaveFileStream_lb(Fnam, ListBxNam)


Do : Sleep : Until Me Is Nothing


Procedure LoadFileStream_lb(Fnam As String, ListBxNam As String)


Local sfile = FreeFile

Local itm$


If ListBxNam = "lb1"

lb1.TopIndex

EndIf

Try

Open Fnam for Input As # sfile


While Not EOF(# sfile)

Input # sfile, itm$


If ListBxNam = "lb1" And itm$ <> ""

lb1.AddItem itm$

EndIf


Wend


Close # sfile

Catch

Message Err$

Close # sfile

EndCatch


Return

Procedure SaveFileStream_lb(Fnam As String, ListBxNam As String)


Local sfile = FreeFile

Local item As Int


Try

sfile = FreeFile

Open Fnam for Output As # sfile


If ListBxNam = "lb1"

For item = 0 To lb1.ListCount - 1

Print # sfile, lb1.List(item)

Next item

EndIf


Close # sfile

Catch

Message Err$

Close # sfile

EndCatch


Return



'Example 2: Comboboxes


'GFA Basic Standard ComboBox File Streaming IO


'Allan Shura


LoadForm frm1


Global ComboBxNam As String

ComboBxNam = "cb1"

Global Fnam As String


Fnam = App.Path + "\" + "Test" + ".dat"

LoadFileStream_cb(Fnam, ComboBxNam)


Global a$

a$ = "Hello World"

If ComboBxNam = "cb1"

cb1.AddItem a$

EndIf


Fnam = App.Path + "\" + "Testhello" + ".dat"

SaveFileStream_cb(Fnam, ComboBxNam)


Do : Sleep : Until Me Is Nothing


Procedure LoadFileStream_cb(Fnam As String, ComboBxNam As String)


Local sfile = FreeFile

Local itm$


If ComboBxNam = "cb1"

cb1.TopIndex

EndIf

Try

Open Fnam for Input As # sfile


While Not EOF(# sfile)

Input # sfile, itm$


If ComboBxNam = "cb1" And itm$ <> ""

cb1.AddItem itm$

EndIf


Wend


Close # sfile

Catch

Message Err$

Close # sfile

EndCatch


Return

Procedure SaveFileStream_cb(Fnam As String, ComboBxNam As String)


Local sfile = FreeFile

Local item As Int


Try

sfile = FreeFile

Open Fnam for Output As # sfile


If ComboBxNam = "cb1"

For item = 0 To cb1.ListCount - 1

Print # sfile, cb1.List(item)

Next item

EndIf


Close # sfile

Catch

Message Err$

Close # sfile

EndCatch


Return