djshades2004

Hi im having difficuilty finding the type arraylist in the settings...

Im trying to save items of a listbox to the settings.

Thanks,



Re: Visual Basic Express Edition Type Array List

Kea

The ArrayList type is contained in the System.Collections namespace, however do not use that one, use the generic List type in the System.Collections.Generic namespace instead. This is imported automatically for you in VB2005.




Re: Visual Basic Express Edition Type Array List

djshades2004

Thats brill managed to find it cheers lol.

wats the easiest way to do this heres some code Im trying to do

Private Sub MainForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
My.Settings.SelDrive = FDlg.Text

Dim I As String
For Each I In ListBox2.Items
My.Settings.listboxsave.Add(I)
Next
My.Settings.Save()

MsgBox(I) 'using this to test results.

End Sub





Re: Visual Basic Express Edition Type Array List

Kea

I was thinking about serialization, but I wonder what kind of data you're trying to store - ListBox'es don't store much besides strings, so I only use them for displaying part of the data contained internally in my programs. If strings are okay with you, check the type part of the setting in the settings browser, and choose System.Collections.Specialized.StringCollection. This allows you to persist a collection of strings in your app settings.
Tip: Store you're settings save statements in a separate routine from the FormClosing handler, and call it from there.




Re: Visual Basic Express Edition Type Array List

djshades2004

Thanks for the reply Kea,

The listbox will be saving filenames, I would like it to keep these items in the list box when you re-launch the program hence needing to save the items.

What routine are you suggesting to put the code in





Re: Visual Basic Express Edition Type Array List

Kea

Perhaps something like this (I just jut this down from the top of my head):

Code Snippet

Public Sub YourForm_FormClose(...) Handles Me.FormClose

SaveFormSettings()

End Sub


''' <summary>
''' Saves the settings of the form at any time.
''' </summary>

Private Sub SaveFormSettings()

SaveFileNamesSettings()
My.Resources.Settings.Save()

End Sub


''' <summary>
''' Persists the file names in the file names list box.
''' </summary>

Private Sub SaveFileNamesSettings()

Dim fileNames As System.Collections.Specialized.StringCollection = new System.Collections.Specialized.StringCollection()

For Each fileName As String In FileNamesListBox.Items

fileNames.Add(fileName)

Next

My.Resources.Settings.FileNames = fileNames

End Sub



It's just an attempt at trying to be more modular. You might want to save your program's settings before the user actually quits the program, in case your program crashes.




Re: Visual Basic Express Edition Type Array List

Kea

When I think of it, the StringCollection maybe supports a constructor using an IEnumerable. In that case, you can

Code Snippet

My.Resources.Settings.FileNames = new System.Collections.Specialized(fileNamesListBox.Items)






Re: Visual Basic Express Edition Type Array List

SJWhiteley

Kea wrote:
ListBox'es don't store much besides strings,

Incorrect: listboxes 'store' Objects.

You can store any kind of object in a list box and use a property of the object for display. For example, adding a listbox to a form in a new project:

Code Snippet

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

ListBox1.DisplayMember = "DisplayString"

ListBox1.Items.Add(New Storage(5))

ListBox1.Items.Add(New Storage(23))

ListBox1.Items.Add(New Storage(2))

ListBox1.Items.Add(New Storage(88))

ListBox1.Items.Add(New Storage(9))

End Sub

Private Class Storage

Private _number As Integer

Public Sub New(ByVal newNumber As Integer)

_number = newNumber

End Sub

Public Property Number() As Integer

Get

Return _number

End Get

Set(ByVal value As Integer)

_number = value

End Set

End Property

Public ReadOnly Property DisplayString() As String

Get

Return "Number is " & Number

End Get

End Property

End Class

End Class

However, as correctly pointed out, there is a StringCollection setting 'Type' that could be used.






Re: Visual Basic Express Edition Type Array List

djshades2004

Thank you Kea for you help very much appreciated.





Re: Visual Basic Express Edition Type Array List

djshades2004

Thank you also SJ for your help I will try that out now.

Tried it out SJ, It works but how do I save this data to the settings





Re: Visual Basic Express Edition Type Array List

SJWhiteley

Well, things start getting a bit more complicated....

It depends hat you actually want to 'save'. The example shows how to display text from a complex object. Obviously, you don't want to save the text, but the actual underlying data.

What I would possibly do, is provide a mechanism in your object to convert it To/From a string used to 'save' a humanly readable representation of that string. This is where serialization comes in.

If you want to save it to the settings class, then you could do it this way:

Code Snippet

Public Class Form1

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

'

' Save these items

Dim savedItems As New System.Collections.Specialized.StringCollection

' Go through the itemsin the list

For Each itm As Object In ListBox1.Items

' Get a storage item

Dim thisStorage As Storage = TryCast(itm, Storage)

If thisStorage Is Nothing Then Continue For

' Add this item to our list of items to save

savedItems.Add(thisStorage.ToString)

Next

' Set the saveditems

My.Settings.StorageValues = savedItems

' Actually save these values

My.Settings.Save()

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

My.Settings.Reload()

' Get the saved items

Dim savedItems As System.Collections.Specialized.StringCollection = My.Settings.StorageValues

'

' Create a new storage from each saved item and add it to the list

If savedItems IsNot Nothing Then

For Each s As String In savedItems

Dim newStorage As Storage = Storage.FromString(s)

ListBox1.Items.Add(newStorage)

Next

End If

ListBox1.DisplayMember = "DisplayString"

ListBox1.Items.Add(New Storage(5))

ListBox1.Items.Add(New Storage(23))

ListBox1.Items.Add(New Storage(2))

ListBox1.Items.Add(New Storage(88))

ListBox1.Items.Add(New Storage(9))

End Sub

Private Class Storage

Private _number As Integer

Public Sub New(ByVal newNumber As Integer)

_number = newNumber

End Sub

Public Property Number() As Integer

Get

Return _number

End Get

Set(ByVal value As Integer)

_number = value

End Set

End Property

Public ReadOnly Property DisplayString() As String

Get

Return "Number is " & Number

End Get

End Property

Public Overrides Function ToString() As String

' Create a string representation of this object

Return Number.ToString

End Function

Public Shared Function FromString(ByVal descriptor As String) As Storage

' interpret a string representation of this object

Dim n As Integer

Call Integer.TryParse(descriptor, n)

Return New Storage(n)

End Function

End Class

End Class

Of course, this is possibly the hard way, but it utilizes the Settings class. Honestly, I don't do much with that class because I find it so restrictive. You could implement a streamreader/streamwriter to ceate your own file format.

However...you could mark the above (object) as serializable and 'save' it to an XML file, or a binary file.

There may be an easier way than the above, but thats' what I came up with Obviously, as your 'object' gets more complicated you have to explicitly convert it to/from a string (essentially providing your own serialization of the object - very versatile, but there may be built in mechanisms that would be more appropriate).






Re: Visual Basic Express Edition Type Array List

Kea

Thanks for correcting me there. I was really just guessing about the strings. Anyway, couldn't you just use the ToString() method
If you serialize it binary, you could store the bytes from the stream in the settings, too, I guess.
There's room for a lot of experiment.




Re: Visual Basic Express Edition Type Array List

djshades2004

Thanks SJ, that quite complicated then I thought it would be. but cheers for the time and code

Very much Appreciated cheers Wink





Re: Visual Basic Express Edition Type Array List

SJWhiteley

Kea wrote:
Thanks for correcting me there. I was really just guessing about the strings. Anyway, couldn't you just use the ToString() method
If you serialize it binary, you could store the bytes from the stream in the settings, too, I guess.
There's room for a lot of experiment.

Definately a lot of room to experiment . Once you discover that ListBoxes, ComboBoxes just display the ToString method you could just override the ToString Method; you are correct. It does simplify, I think (i'm used to overriding the ToString method to display a complete representation of the class, rather than a user-display-type string).