Problem with my "Save" button
-
Mittwoch, 8. August 2012 18:06
Hi, I have a problem with my save button:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Problem: It's basically saying, if the world already exists, delete and save it again. I haven't tested that yet. But when it is creating a new one, on the line:
If Directory.Exists("C:\Games\Build\data\data.world\" & TextBox1.Text) Then
If MessageBox.Show("This world already exists. Would you like to save over it?", "Save as Continued World?", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
File.Delete("C:\Games\Build\data\data.world\" & TextBox1.Text & "\world.buildlvl")
Dim newWorldFile As New IO.StreamWriter("C:\Games\Build\data\data.world\" & TextBox1.Text & "\world.buildlvl")
newWorldFile.WriteLine(Form1.PictureBox1.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox2.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox3.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox4.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox5.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox6.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox7.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox8.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox9.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox10.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox11.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox12.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox13.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox14.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox15.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox16.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox17.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox18.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox19.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox20.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox21.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox22.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox23.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox24.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox25.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox26.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox27.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox28.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox29.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox30.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox31.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox32.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox33.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox34.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox35.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox36.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox37.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox38.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox39.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox40.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox41.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox42.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox43.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox44.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox45.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox46.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox47.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox48.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox49.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox50.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox51.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox52.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox53.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox54.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox55.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox56.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox57.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox58.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox59.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox60.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox61.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox62.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox63.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox64.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox65.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox66.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox67.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox68.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox69.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox70.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox71.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox72.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox73.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox74.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox75.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox76.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox77.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox78.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox79.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox80.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox81.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox82.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox83.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox84.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox85.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox86.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox87.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox88.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox89.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox90.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox91.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox92.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox93.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox94.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox95.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox96.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox97.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox98.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox99.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox100.ImageLocation)
newWorldFile.Close()
If MessageBox.Show("The world has successfully saved!", "Saved!", MessageBoxButtons.OK) = Windows.Forms.DialogResult.OK Then
Me.Close()
End If
Else
End If
Else
Directory.CreateDirectory("C:\Games\Build\data\data.world\" & TextBox1.Text)
File.Create("C:\Games\Build\data\data.world\" & TextBox1.Text & "\world.buildlvl")
Dim newWorldFile As New IO.StreamWriter("C:\Games\Build\data\data.world\" & TextBox1.Text & "\world.buildlvl")
newWorldFile.WriteLine(Form1.PictureBox1.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox2.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox3.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox4.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox5.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox6.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox7.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox8.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox9.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox10.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox11.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox12.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox13.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox14.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox15.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox16.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox17.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox18.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox19.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox20.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox21.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox22.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox23.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox24.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox25.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox26.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox27.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox28.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox29.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox30.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox31.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox32.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox33.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox34.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox35.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox36.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox37.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox38.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox39.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox40.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox41.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox42.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox43.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox44.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox45.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox46.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox47.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox48.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox49.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox50.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox51.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox52.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox53.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox54.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox55.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox56.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox57.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox58.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox59.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox60.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox61.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox62.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox63.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox64.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox65.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox66.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox67.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox68.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox69.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox70.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox71.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox72.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox73.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox74.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox75.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox76.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox77.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox78.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox79.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox80.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox81.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox82.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox83.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox84.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox85.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox86.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox87.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox88.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox89.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox90.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox91.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox92.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox93.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox94.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox95.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox96.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox97.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox98.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox99.ImageLocation)
newWorldFile.WriteLine(Form1.PictureBox100.ImageLocation)
newWorldFile.Close()
End If
Me.Close()
Form1.Close()
Form2.Close()
Form3.Close()
Form4.Close()
End SubDim newWorldFile As New IO.StreamWriter("C:\Games\Build\data\data.world\" & TextBox1.Text & "\world.buildlvl")It says it is being used by another process. I don't know why it says that, because it isn't. Why is this so?
Alle Antworten
-
Mittwoch, 8. August 2012 18:12
"It says it is being used by another process. I don't know why it says that, because it isn't. Why is this so?"
Because you didn't close and dispose it after the first use.
A better way would be to use a StringBuilder, build the text, then simply write it out as a single "blast", set to overwrite.
Please also put the following on the top of the form:
Option Strict On
Please call me Frank :)
-
Mittwoch, 8. August 2012 18:19
Apart from agreeing with Frank: Can't you create the Pictureboxes dynamically at runtime? Then you can write short loops to process all of them.
At least you can put the duplicate 100 lines (aren't they?) into a method and call it twice.
Armin
- Bearbeitet Armin Zingler Mittwoch, 8. August 2012 18:19
-
Mittwoch, 8. August 2012 18:19
Are there any other PictureBox controls on the form?
If not, you can make your coding a HECK of a lot easier by iterating through the controls looking for a PictureBox, then casting that to another one declared in the loop. From that, get the ImageLocation.
You still can do that even if there are other PictureBox controls on it, but then you'd have to qualify that it's the right one by the name (or a tag property if you have something in it).
Please call me Frank :)
-
Mittwoch, 8. August 2012 18:19What do you mean by I didn't close and dispose it after the first use? I closed it where it needed to be closed, right?
-
Mittwoch, 8. August 2012 18:22
What do you mean by I didn't close and dispose it after the first use? I closed it where it needed to be closed, right?
The best way (it supports IDisposable) would be in a Using block, but without it, you need to declare it, use it, close it, and then dispose it. The next time, you repeat that (which is another way that Using block comes in handy, so put it in a sub).
I think you're making this much too difficult on yourself though. ;-)
Please call me Frank :)
- Als Antwort markiert Mark Liu-lxfModerator Donnerstag, 16. August 2012 07:33
-
Mittwoch, 8. August 2012 18:34
Consider something like this:
Private Sub btn_SaveLocations_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles btn_SaveLocations.Click ' Dim sb As New System.Text.StringBuilder ' For Each ctrl As Control In Me.Controls If TypeOf ctrl Is PictureBox Then Dim pb As PictureBox = DirectCast(ctrl, PictureBox) sb.AppendLine(pb.ImageLocation) End If Next ' If sb.ToString.Length > 0 Then ' Now sb.ToString will have a line for each ' imagelocation of all the PictureBox controls End If ' End Sub
Can you take it from there and write the file out? It's just one more line (if you know where to save the file, that is).
Please call me Frank :)

