saving opening multiple file error

I have written the following code to save the text from multiple textboxes into a single txt file:

Dim sfdAsNew SaveFileDialog

Dim strAsString =""

'add the types you would like below

sfd.Filter ="Budget Files (*.bgt)|*.txt|All files (*.*)|*.*"

sfd.Title ="Title"

'show dialog

sfd.ShowDialog()

'use the code below for your next action

If sfd.ShowDialog() = Windows.Forms.DialogResult.OKThen

ForEach txtAs ControlInMe.Controls

IfTypeOf (txt)Is TextBoxThen

str += txt.Text.Trim() + vbTab

EndIf

Next

My.Computer.FileSystem.WriteAllText(sfd.FileName, str,False)

EndIf

After this, I wrote the following code to open the txt file with all the information from the text boxes, so that all information would return to its corresponding text box (after the text boxes were cleared). All the '.text' items below are the text items:

Dim ofdAsNew OpenFileDialog

Dim str()AsString

'add the types you would like below

ofd.Filter ="Budget Files (*.bgt)|*.txt|All files (*.*)|*.*"

ofd.Title ="Title"

'show dialog

ofd.ShowDialog()

'use the code below for your next action

If ofd.ShowDialog() = Windows.Forms.DialogResult.OKThen

str =My.Computer.FileSystem.ReadAllText(ofd.FileName).Split(vbTab)

ex1.Text = str(0)

ex2.Text = str(1)

ex3.Text = str(3)

ex4.Text = str(4)

ex5.Text = str(5)

ex6.Text = str(6)

dex1.Text = str(7)

dex2.Text = str(8)

dex3.Text = str(9)

dex4.Text = str(10)

dex5.Text = str(11)

dex6.Text = str(12)

ei1.Text = str(13)

ei2.Text = str(14)

ei3.Text = str(15)

ei4.Text = str(16)

ei5.Text = str(17)

ei6.Text = str(18)

dext1.Text = str(19)

dext2.Text = str(20)

dext3.Text = str(21)

dext4.Text = str(22)

dext5.Text = str(23)

dext6.Text = str(24)

in1.Text = str(25)

in2.Text = str(26)

in3.Text = str(27)

in4.Text = str(28)

in4.Text = str(29)

in5.Text = str(30)

in6.Text = str(31)

din1.Text = str(32)

din2.Text = str(33)

din3.Text = str(34)

din4.Text = str(35)

din5.Text = str(36)

din6.Text = str(37)

ni1.Text = str(38)

ni2.Text = str(39)

ni3.Text = str(40)

ni4.Text = str(41)

ni5.Text = str(42)

ni6.Text = str(43)

dint1.Text = str(44)

dint2.Text = str(45)

dint3.Text = str(46)

dint4.Text = str(47)

dint5.Text = str(48)

dint6.Text = str(49)

dextotal.Text = str(50)

surplust.Text = str(51)

dintotal.Text = str(52)

deficitt.Text = str(53)

EndIf

When I debug this programme, the all the texts from the textboxes which I saved into a single text file doesn't appear when the corresponding txt file is opened on notepad. I addition, when I try an open this txt file, the following error pops up on the lineex2.Text = str(1)saying 'Index was outside the bounds of the array'. even if I delete this line, the same error appears on the next line down. Why does this all happen? What should I do to fix this?

[4213 byte] By [Anonymousyyyy] at [2008-1-4]
# 1

Do all the textboxs belong to me.controls?

Or are some inside of panels or tab controls.

Your:

For Each txt As Control In Me.Controls

only handles textboxes on the main form.

TallDude at 2007-10-11 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2

Salaam,

try to make an array of textboxes and store these textboxes in it

for example :

suppose that you have n textboxes : ex1,ex2,...,exi,...exn where 1<=i<=n and n is positive integer >0

Code Snippet
Dim m_textArray() As New TextBox() {ex1,ex2,...,exi,...exn}

to Save the texts in the File ,do this

Code Snippet

Dim m_string As String

Foreach(text As TextBox In m_textArray)

m_string +=text.Text.Trim() + VbCrlf

Next

My.Computer.FileSystem.WriteAllText(sfd.FileName,m_string,False)

to Load the data from the File to the texts, do this

Code Snippet

Dim m_strings() as String

m_strings=My.Computer.FileSystem.ReadAllText(ofd.FileName).Split(VbCrlf)

For _index As Integer =0 To Ubound(m_strings)

m_textArray(_index).Text=m_strings(_index)

Next

regards

Hadi.84 at 2007-10-11 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 3

OK, lets just look at this code for a while. Your save routine simply iterates around the textboxes and attempts to write the contents of the to a file delimited by tabs. No specific order here, just iterating around the textboxes on the form (and wont pick up any that are contained within container controls such as tabs).

Then your load routine will load the textfile and split it on the tab delimiter and put specific elements into textboxes.

There are a couple of issues with this code.

1. The order of the save is not pre-determined, it is only determined by the order at which controls were added to the controls collection. If you delete a control and readd it the order would be different. This may not be an issue but when you load routine depends upon the specific order to place the items back - it looks like it would be very important to get the order the same.

2. What happens if the user enters a tab in a textbox. There is no code shown to potentially prevent this occuring so if I was to type in "a <Tab> b" in a textbox a tab is inserted - so you may want to consider ensuring that tabs are not part of the string.

3. Your save saves all the textboxes but the load is considering that there are 53 items loaded. If the file contained a line with no tabs or less tabs than expected then you will get a index outside of bounds or array error as the array would contain less than 53 items. So you may want to consider some validation of the data prior to using it.

Just some comments on the original code sample shown

spotty at 2007-10-11 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...