Saving / Loading List Box Values from specified file

mAtUxAz

Member
Joined
Nov 8, 2012
Messages
12
Programming Experience
1-3
Hello, I am creating my own money tracking program. I am in trouble at the part where save and load code of specific file starts.
I want to load and save my own created *.mmt files.
The files would be loaded and saved through Open / Save As window.
What code should I use to load these files?
What code should I use to save these files?

I'd like to save values of both textboxes into one file. Possible or not? If not - two files is not a problem though.

Also, if I can't use specific file extension (*.mmt), a general *.txt file won't ruin my app :tennis:

There is my code

=============================================================================================================================
Public Class Form1


Private Sub ImportToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ImportToolStripMenuItem.Click
OpenFileDialog1.ShowDialog()
End Sub


Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
SaveFileDialog1.ShowDialog()
End Sub


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text & " got on " & TextBox3.Text & "; " & TextBox2.Text)
End Sub


Private Sub Label13_Click(sender As Object, e As EventArgs) Handles Label13.Click


End Sub


Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox2.Items.Add(TextBox6.Text & " spent for " & TextBox5.Text & " on " & TextBox4.Text)
End Sub


Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
If MessageBox.Show("Have you saved your work?", "Wait!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Me.Close()
Else
MessageBox.Show("Please save your work!", "Wait!", MessageBoxButtons.OK, MessageBoxIcon.Information)
SaveFileDialog1.ShowDialog()
End If
End Sub


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


End Sub
End Class

=============================================================================================================================

And some photos


The listbox menu. Probably only Exit function is working without problems.
2.jpg

Open file window. Nothing happens even if I select *.mmt file (an empty file with *.mmt extension).
3.jpg

Added entries to both listboxes for a demonstration. Works perfectly.
1.jpg

Save As file window. No file is saved even if I enter file name and click save.
4.jpg


That's it.
Now, repeating again my questions:
What code should I use to load these files?
What code should I use to save these files?

Will be looking forward to answers!
 
K, guys, I have done all the most important work in my program! The left unprogrammed things are:
1) Total amount of income money
2) Total amount of expenditure money

The problem is that I am unable to write a code which could calculate it, as values are in Data Grid column. May somebody could help me with that? That would be very last thing to program in my app :)
Here's the photo to make the situation easier to understand
A.jpg
 
A simple way to do that is to add an expression column to your table. If your first column in table is Column1 then the Expression you set is "sum(Column1)". This column you don't add to grid, but instead add a Label control to form and in (DataBindings) in Properties window you bind the Text property by selecting the expression column.
 
Sorry, I was unable to understand this procedure. I don't understand how to set expression sum(Column1), as if I use it in code is gives me an error that sum is not declared.
Also, I've have never worked with label controls yet... If you can, please write step by step what should I do.
Adding program code. It works 100%. No errors or bugs:
Imports System.IO
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
If MessageBox.Show("Have you saved your work?", "Wait!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Me.Close()
Else
If MessageBox.Show("Would you like to save your work?", "Wait!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Dim xmlData1 As String = DataSet1.GetXml()
Dim xmlData2 As String = DataSet2.GetXml()
Dim filePath1 As String = "income.mmt"
Dim filePath2 As String = "expenditure.mmt"
DataSet1.WriteXml(filePath1)
DataSet2.WriteXml(filePath2)
MessageBox.Show("Successfully saved!", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
End Sub
Private Sub SaveIncomeValuesOnlyToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveIncomeValuesOnlyToolStripMenuItem.Click
Dim xmlData1 As String = DataSet1.GetXml()
Dim xmlData2 As String = DataSet2.GetXml()
Dim filePath1 As String = "income.mmt"
Dim filePath2 As String = "expenditure.mmt"
DataSet1.WriteXml(filePath1)
DataSet2.WriteXml(filePath2)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim INCXMLfile As String = "income.mmt"
Dim EXPXMLfile As String = "expenditure.mmt"
DataSet1.ReadXml(INCXMLfile)
DataGridView1.DataSource = DataSet1
DataGridView1.DataMember = "TableINC"
DataSet2.ReadXml(EXPXMLfile)
DataGridView2.DataSource = DataSet2
DataGridView2.DataMember = "TableEXP"
End Sub


Private Sub DeleteValueFilesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DeleteValueFilesToolStripMenuItem.Click
Shell("cmd /c del income.mmt")
Shell("cmd /c del expenditure.mmt")
End Sub
End Class
 
I don't understand how to set expression sum(Column1), as if I use it in code is gives me an error that sum is not declared.
No, don't write code, designer will do that for you. You added columns to your table in dataset already, right? Now add just one more column to the table. In that columns properties find the Expression property and set the expression there.
DataGridView2.DataSource = DataSet2
DataGridView2.DataMember = "TableEXP"
Don't write code. Select these properties in designer.
DataSet1.WriteXml(filePath1)
DataSet2.WriteXml(filePath2)
You are still using two datasets, still I ask why? You only need one dataset with two tables.
 
Thank you so much for all the help!
Yeah, two datasets, as I can see now can be converted into one and it is really better :)
I even don't know, why I created two (one for one table, second for second table) when I could use one dataset :)
 
Back
Top