Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Mon Oct 23, 2017 9:08 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: ListBox - SaveRows/LoadRows
PostPosted: Tue Jan 24, 2012 1:29 pm 
Offline
User avatar

Joined: Sat May 27, 2006 3:27 pm
Posts: 480
Hello
I am going to show you a simple way how to load and save your listbox content to a data file.

Hope this helps

1. Make a Module with two methods one LoadData the other SaveData
2. Call the methods in a windows Open/Close event

Sub LoadData( L As ListBox )
Dim Stream As BinaryStream
Dim Count1,Count2,i,x As Integer
Dim f As folderitem= GetFolderItem("C:\DataFile", FolderItem.PathTypeShell )
If f.Exists Then
If f = Nil Then Exit
Stream = f.OpenAsBinaryFile(False)
Count1 = Stream.ReadLong
Count2 = Stream.ReadLong
For i = 0 To Count1-1
L.AddRow Stream.ReadPString
For x = 1 To Count2-1
L.Cell( i , x ) = Stream.ReadPString
Next
Next
Stream.Close
End
End Sub


Sub SaveData( L As ListBox )
Dim i,x As integer
Dim Stream As BinaryStream
Dim f As FolderItem = New FolderItem( "C:\DataFile" , FolderItem.PathTypeShell )
if f <> NIL Then
Stream=f.CreateBinaryFile(".data")
Stream.WriteLong L.ListCount
Stream.WriteLong L.ColumnCount
For i = 0 To L.ListCount-1
For x = 0 To L.ColumnCount-1
Stream.WritePString L.Cell( i , x )
Next
Next
Stream.Close
End
End Sub


Call it like this in the Windows Open/Close

Sub Window Open()
Call LoadData( MyListBox1 )
End Sub


Sub Window Close()
Call SaveData( MyListBox1 )
End Sub


Top
 Profile  
Reply with quote  
 Post subject: Re: ListBox - SaveRows/LoadRows
PostPosted: Tue Jan 24, 2012 1:53 pm 
Offline

Joined: Mon Aug 14, 2006 9:33 pm
Posts: 1774
If f.Exists Then
If f = Nil Then Exit

This is backwards. You need to check for f = Nil BEFORE calling f.exists. If f is = nil, then your program has crashed before you get to the line "if f = Nil"

Stream = f.OpenAsBinaryFile(False)

This method is deprecated. The correct call is
Stream = binaryStreamOpen(f)


You should also check for
Stream <> Nil after the above call. It is a definite possibility that the stream can be Nil or encounter an I/O exception which will also cause your code to crash.

_________________
Roger Clary
Class One Software
Educational Software for Lifelong Learning
http://www.classonesoftware.com


Top
 Profile  
Reply with quote  
 Post subject: Re: ListBox - SaveRows/LoadRows
PostPosted: Tue Jan 24, 2012 2:07 pm 
Offline
Real Software Engineer

Joined: Sat Dec 24, 2005 8:18 pm
Posts: 7858
Location: Canada, Alberta, Near Red Deer
[quote="sinner"]
L.AddRow Stream.ReadPString

The use of PString limits you to 255 characters of data

Avoiding that use and making the write write a int32 that is the count then the bytes would let you put as much data as you want
Also you probably need to know the encoding of the data you write & read

I've adjusted things as suggested (but this is entirely written in the forum so it's not tested)
But it should give you the idea

Sub SaveData( L As ListBox )
Dim i,x As integer
Dim Stream As BinaryStream
Dim f As FolderItem = New FolderItem( "C:\DataFile" , FolderItem.PathTypeShell )
if f <> NIL Then
Stream=f.CreateBinaryFile(".data")
Stream.WriteLong L.ListCount
Stream.WriteLong L.ColumnCount
For i = 0 To L.ListCount-1
For x = 0 To L.ColumnCount-1
dim data as string = ConvertEncoding( L.Cell( i , x ), Encodings.UTF8 )
Stream.WriteInt32 lenB(data) // yes LenB as you need to know how many bytes you write
Stream.Write data
Next
Next
Stream.Close
End
End Sub


Sub LoadData( L As ListBox )
Dim Stream As BinaryStream
Dim Count1,Count2,i,x As Integer
Dim f As folderitem= GetFolderItem("C:\DataFile", FolderItem.PathTypeShell )
If f.Exists Then
If f = Nil Then Exit
Stream = f.OpenAsBinaryFile(False)
Count1 = Stream.ReadLong
Count2 = Stream.ReadLong
For i = 0 To Count1-1
L.AddRow Stream.ReadPString
For x = 1 To Count2-1
dim readLen as Int32 = Stream.ReadInt32
L.Cell( i , x ) = Stream.Read( readLen, Encodings.UTF8 )
Next
Next
Stream.Close
End
End Sub


The other thing you might want to do is pass in the folderitem to read from / write to as a parameter

_________________
Norman Palardy (Real Software)


Top
 Profile  
Reply with quote  
 Post subject: Re: Re edited - ListBox - SaveRows/LoadRows
PostPosted: Sat Oct 06, 2012 12:11 pm 
Offline
User avatar

Joined: Sat May 27, 2006 3:27 pm
Posts: 480
I would also like to add this to this to this topic!

This is the most effective way i had found to save passwords & names to multiple columns , rows in a listbox without no hazel!

1. Make a new global method in a module an add the following code

Sub SaveLoadListbox( L As ListBox , Type As String )
Dim f As folderitem
Dim Stream As BinaryStream
Dim Count1,Count2,i,x As Integer

Select Case Type

Case "Save"
f= New FolderItem( "C:\DataFile")
If f <> NIL Then
Stream=f.CreateBinaryFile(".data")
Stream.WriteLong L.ListCount
Stream.WriteLong L.ColumnCount
For i =0 To L.ListCount-1
For x=0 To L.ColumnCount-1
Stream.WritePString L.Cell(i,x)
Next
Next
Stream.Close
End

Case "Load"
f= GetFolderItem("C:\DataFile")
If f <> NIL AND f.Exists Then
Stream=f.OpenAsBinaryFile(False)
Count1=Stream.ReadLong
Count2=Stream.ReadLong
If Count1 = 0 Then Return
For i =0 To Count1-1
L.AddRow Stream.ReadPString
For x=1 To Count2-1
L.Cell(i,x)=Stream.ReadPString
Next
Next
Stream.Close
End
End Select
End Sub


2. Call it like this in a pushbutton or a listbox's open close event

'Save
Call SaveLoadListbox( listbox1, "Save" )
'Load
Call SaveLoadListbox( listbox1, "Load" )


Simple as that hope this helps :D


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group