Hello @HO Kwok Piu (200257110) ,
A simplistic solution if the intent is to remember information between sessions and quick-n-easy way is to use a json file. Note there are other options e.g. saving to a text delimited file or xml while the following is strongly typed and a better solution.
In the following GitHub repository I've created a conceptual/working code sample meaning it demonstrates reading/writing to the json file and has comments.
https://github.com/karenpayneoregon/visual-basic-getting-started/tree/master/BasicJson_1
[
{
"Name": "First item",
"Value": 11.34
},
{
"Name": "Secondt item",
"Value": 1.33
}
]
Class for storing data
Imports System.ComponentModel
Imports System.Runtime.CompilerServices
Namespace Classes
Public Class ApplicationStorage
Implements INotifyPropertyChanged
Private _name As String
Private _value As Decimal
Public Property Name() As String
Get
Return _name
End Get
Set
_name = value
OnPropertyChanged()
End Set
End Property
Property Value() As Decimal
Get
Return _value
End Get
Set
_value = value
OnPropertyChanged()
End Set
End Property
Public Overrides Function ToString() As String
Return Name
End Function
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Protected Overridable Sub OnPropertyChanged(<CallerMemberName> Optional memberName As String = Nothing)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(memberName))
End Sub
End Class
End Namespace
File operations class
Imports System.IO
Imports Newtonsoft.Json
Namespace Classes
Public Class FileOperations
Public Shared FileName As String = "appSetting.json"
Public Shared Sub Save(storageList As List(Of ApplicationStorage))
Using streamWriter = File.CreateText(FileName)
Dim serializer = New JsonSerializer With {.Formatting = Formatting.Indented}
serializer.Serialize(streamWriter, storageList)
End Using
End Sub
Public Shared Function Load() As List(Of ApplicationStorage)
Using streamReader = New StreamReader(FileName)
Dim json = streamReader.ReadToEnd()
Return JsonConvert.DeserializeObject(Of List(Of ApplicationStorage))(json)
End Using
End Function
End Class
End Namespace
Form code
Imports System.ComponentModel
Imports System.IO
Imports BasicJson_1.Classes
Public Class Form1
''' <summary>
''' Storage container for ApplicationStorage data.
''' Using a BindingList and implementing INotifyPropertyChanged interface
''' permits real time updates to ListBox.
''' </summary>
Private bindingList as new BindingList(Of ApplicationStorage)
''' <summary>
''' Load from file if exists
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
If File.Exists(FileOperations.FileName) Then
Try
bindingList = New BindingList(Of ApplicationStorage)(FileOperations.Load())
Catch ex As Exception
MessageBox.Show($"Failed to load json{Environment.NewLine}{ex.Message}")
End Try
End If
ListBox1.DataSource = bindingList
End Sub
''' <summary>
''' Add new item if name is value and a valid decimal was entered
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub AddButton_Click(sender As Object, e As EventArgs) Handles AddButton.Click
Dim value as Decimal = 0
if Decimal.TryParse(ValueTextBox.Text, value) AndAlso Not string.IsNullOrWhiteSpace(NameTextBox.Text)
bindingList.Add(new ApplicationStorage() With{.Name = NameTextBox.Text, .Value = value})
End If
End Sub
''' <summary>
''' Save items to json
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub SaveAllButton_Click(sender As Object, e As EventArgs) Handles SaveAllButton.Click
if bindingList.Count >0
FileOperations.Save(bindingList.ToList())
End If
End Sub
''' <summary>
''' Get current item
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub CurrentButtonButton_Click(sender As Object, e As EventArgs) Handles CurrentButton.Click
if bindingList.Count >0
if ListBox1.SelectedIndex > -1
Dim currentItem = bindingList.Item(ListBox1.SelectedIndex)
MessageBox.Show($"Name: {currentItem.Name} Value: {currentItem.Value}")
End If
End If
End Sub
End Class
Screen shot