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!
 
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
 
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:
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
 
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:
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
 
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:
 
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 :)
 
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??
 
AllowUserToAddRows=True by default for DataGridView. When user start entering data in the new row that is marked * a new row is automatically added.
 
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 />
 
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?
 
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:
When I choose dataset as the data source, new rows don't appear.
It sounds as you haven't selected DataMember.
 
Back
Top