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!
 

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi,

To answer your questions:-

1) You can save a file of any type that you like so "*.mmt" is totally fine. The thing to remember is that when you save a file you need to ensure that the file extension is of type "mmt".

2) You can save any information you want in your files but to use your needs as an example, you can of course use separate files, but to use a single file you would need to add a "delimiter" between your information when saving your data to check for later on when reading your file so that you know where to split your data for population of your different controls.

3) To read files in a structured manner so that you can interact with the data while reading the file then use the StreamReader class. See Here:-

StreamReader Class (System.IO)

4) To write files in a structured manner to accommodate your needs then use the StreamWriter class. See Here:-

StreamWriter Class (System.IO)

Hope that helps.

Cheers,

Ian
 

mAtUxAz

Member
Joined
Nov 8, 2012
Messages
12
Programming Experience
1-3
Thanks! I checked out the information on links you provided. I didn't understand anything and in visual basic it gives me million of errors.
May someone write a code for saving a file and for opening the file?

The code when I click save on menu strip at this time is:

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


The code when I click open on menu strip at this time is:

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


The save window code at this time is (empty):


Private Sub SaveFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk


End Sub




The open window dialog code at this time is (empty):

Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk


End Sub





What subs do I need to modify to make things work?
OpenFileDialog1
SaveFileDialog1
ImportToolStripMenuItem_Click
SaveToolStripMenuItem
 
Last edited:

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi,

Sorry, but you need to learn how to read and write files first which is why I posted the links. The fact that you state "I didn't understand anything" means you need to use the links to get the understanding to do this.

Good luck.

Cheers,

Ian
 

mAtUxAz

Member
Joined
Nov 8, 2012
Messages
12
Programming Experience
1-3
I have realised that for such things listboxes aren't good.
So I decided to change them to Data Grid Views and also faced with a problem:
How to add data to them? Does data grid view can only read (show) already written info and is unable to add new entries?
If so, what you'd recommend for me to use to display such information?

I was surfing the net for hours but didn't found anything helpful.

Here's the photo
Untitled.jpg
 
Last edited:

22-degrees

Well-known member
Joined
Feb 11, 2012
Messages
156
Location
South East Ireland
Programming Experience
1-3
As Ian Ryder said, you really need to learn about reading and writing files first.. When you know how, you will find there are lots of different ways you can transfer your application's output to file and lots of ways to import it as well..

The reason your surfing session didn't yield anything helpful is because you are missing that starting link.. Learning to read and write..

Make a new project with just one textbox and one button.. Learn how to write to a file and practice transferring the data from that one textbox to the file and back again.. When you are able to do this, then you can expand your horizons..

Good Luck
 

mAtUxAz

Member
Joined
Nov 8, 2012
Messages
12
Programming Experience
1-3
I have been made a project as you said - one textbox, but two button (one for saving, one for opening). I made to work them and this doesn't solve my problem: how I could add data to datagrids!
Does Data Grid ONLY SHOWS information and is unable to modify it without external app (say like MS Access)? I am thinking so as I even can't find a function which could do that! The function of listbox item add is:
ListBox1.Items.Add(TextBox2.Text)

However, I can't find a function which could add item to the datagrid. A pseudocode would look like that, but it's still useless, as this function is invalid:
DataGridView1.Items.Add(TextBox2.Text)

I think you got it what I want to say, so if you know, please post me a code of item add to the datagrid! :tennis:
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,603
Location
Norway
Programming Experience
10+
You can remove those Textboxes and let user enter data directly into the DataGridView. DataGridView is an input control and each cell is a textbox (for a textbox column anyway).
A simple way to load save that data is to bind to grid a DataSet (with DataTable) and simply use its WriteXml/ReadXml methods. You can configure all this in designer;
  • add a untyped Dataset from Toolbox, configure its tables and their columns.
  • Select DataSource/DataMember for DataGridView.
  • Write two lines of code to save/load
and you're all set :)
 

mAtUxAz

Member
Joined
Nov 8, 2012
Messages
12
Programming Experience
1-3
JohnH, You've really helped me! Now I am able to enter data to the datagrids! But..... faced with another problem - the data can be entered only at one row! How I could add more rows??
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,603
Location
Norway
Programming Experience
10+
AllowUserToAddRows=True by default for DataGridView. When user start entering data in the new row that is marked * a new row is automatically added.
 

mAtUxAz

Member
Joined
Nov 8, 2012
Messages
12
Programming Experience
1-3
This setting is already enabled. Today, unless I made to be possible to enter data to datagrids, I saw the *, but now I see "pencil" icon, and after I fill all the 3 columns I don't get new row.

Also, what's wrong with this save code????:

Dim xmlData1 As String = DataSet1.GetXml()
Dim xmlData2 As String = DataSet2.GetXml()
Dim filePath1 As String = "C:\income.xml"
Dim filePath2 As String = "C:\expenditure.xml"
DataSet1.WriteXml(filePath1)
DataSet2.WriteXml(filePath2)

It writes two xml files, but they are completely same and when opened with notepad show that:
<?xml version="1.0" standalone="yes"?>
<NewDataSet />
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,603
Location
Norway
Programming Experience
10+
what's wrong with this save code?
Nothing wrong, but there is no data in those datasets. But why do you need two datasets? A dataset can contain multiple tables, and from the looks of your previous screenshot you just need two tables.
Today, unless I made to be possible to enter data to datagrids
What?
I saw the *, but now I see "pencil" icon,
'pencil' icon displays when row is in edit.
after I fill all the 3 columns I don't get new row
When you start editing a cell in new row the next new row is added immediately. Are you sure you followed the instructions in post 8 to the point? Didn't have any leftovers from before that would mess things up?
 

mAtUxAz

Member
Joined
Nov 8, 2012
Messages
12
Programming Experience
1-3
Okay, I found a strange thing:
When I choose dataset as the data source, new rows don't appear.
BUT, IF I DO NOT CHOOSE ANY DATA SOURCE, NEW ROWS APPEAR!
I must have bound data source, else I won't be able to save and load data!
Is that a bug or not???
p.s.: it still saves only the body of the xml file.... no data!
 
Last edited:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,603
Location
Norway
Programming Experience
10+
When I choose dataset as the data source, new rows don't appear.
It sounds as you haven't selected DataMember.
 

mAtUxAz

Member
Joined
Nov 8, 2012
Messages
12
Programming Experience
1-3
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
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,603
Location
Norway
Programming Experience
10+
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.
 

mAtUxAz

Member
Joined
Nov 8, 2012
Messages
12
Programming Experience
1-3
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
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,603
Location
Norway
Programming Experience
10+
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.
 
Top Bottom