Real Software Forums
http://forums.realsoftware.com/

ListBox - SaveRows/LoadRows
http://forums.realsoftware.com/viewtopic.php?f=21&t=42374
Page 1 of 1

Author:  sinner [ Tue Jan 24, 2012 1:29 pm ]
Post subject:  ListBox - SaveRows/LoadRows

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

Author:  classone [ Tue Jan 24, 2012 1:53 pm ]
Post subject:  Re: ListBox - SaveRows/LoadRows

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.

Author:  npalardy [ Tue Jan 24, 2012 2:07 pm ]
Post subject:  Re: ListBox - SaveRows/LoadRows

[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

Author:  sinner [ Sat Oct 06, 2012 12:11 pm ]
Post subject:  Re: Re edited - ListBox - SaveRows/LoadRows

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

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/