Help reading control.location value back form text file

M

Marc

The first part of the below writes the name and location of all
buttons on a from to a text file. The second part reads that
information back in and recreates the buttons. My problem is reading
the location value back in where I marked ?????????????????????. As it
wont accept a string value?



Private Sub Save_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Save.Click
TW = System.IO.File.CreateText("C:\MyTextFile.txt")
Dim C As Control
For Each C In Me.Controls
If TypeOf C Is Button Then
TW.WriteLine(C.Text)
TW.WriteLine(C.Location)
End If
Next
TW.Close()
End Sub




Private Sub Load_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Load.Click
Dim TextFileStream As System.IO.TextReader
TextFileStream = System.IO.File.OpenText("C:\MyTextFile.txt")
Dim MyFileContents As String
Do
MyFileContents = TextFileStream.ReadLine
Dim test As New Button()
Me.Controls.Add(test)
test.Text = MyFileContents

test.Location = ????????????????????????????????

AddHandler test.MouseDown, AddressOf Me.Thebutton_MouseDown
AddHandler test.MouseMove, AddressOf Me.Thebutton_MouseMove
AddHandler test.MouseUp, AddressOf Me.Thebutton_MouseUp
Me.Controls.Add(test)
MsgBox(MyFileContents)
Loop Until MyFileContents = ""
TextFileStream.Close()
End Sub
 
T

Tom Shelton

Marc said:
The first part of the below writes the name and location of all
buttons on a from to a text file. The second part reads that
information back in and recreates the buttons. My problem is reading
the location value back in where I marked ?????????????????????. As it
wont accept a string value?



Private Sub Save_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Save.Click
TW = System.IO.File.CreateText("C:\MyTextFile.txt")
Dim C As Control
For Each C In Me.Controls
If TypeOf C Is Button Then
TW.WriteLine(C.Text)
TW.WriteLine(C.Location)
End If
Next
TW.Close()
End Sub




Private Sub Load_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Load.Click
Dim TextFileStream As System.IO.TextReader
TextFileStream = System.IO.File.OpenText("C:\MyTextFile.txt")
Dim MyFileContents As String
Do
MyFileContents = TextFileStream.ReadLine
Dim test As New Button()
Me.Controls.Add(test)
test.Text = MyFileContents

test.Location = ????????????????????????????????

AddHandler test.MouseDown, AddressOf Me.Thebutton_MouseDown
AddHandler test.MouseMove, AddressOf Me.Thebutton_MouseMove
AddHandler test.MouseUp, AddressOf Me.Thebutton_MouseUp
Me.Controls.Add(test)
MsgBox(MyFileContents)
Loop Until MyFileContents = ""
TextFileStream.Close()
End Sub

Marc,

I think a better approach to this would be serialization. Not of the
buttons it self, but of a container that controls the attributes...
something like (assuming 2005 here):

Option Strict On
Option Explicit On

<Serializable()> _
Friend Class AttributeContainer
Public Location As Point
Public Size As Size
Public Text As String

Public Sub New(ByVal Location As Point, ByVal Size As Size, ByVal
Text As String)
Me.Location = Location
Me.Size = Size
Me.Text = Text
End Sub
End Class

this class would hold the button values you wish to persist. Then, to
save the attributes:

' a container
Dim btnList As List(Of AttributeContainer) = New List(Of
AttributeContainer)

' grab the attributes
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is Button Then
btnList.Add(New AttributeContainer(ctrl.Location,
ctrl.Size, ctrl.Text))
End If
Next

' write the list in one chunk
Dim bf As New BinaryFormatter()
Using writter As FileStream = File.OpenWrite("myfile.dat")
bf.Serialize(writter, btnList)
End Using

to retrieve would look something like:

Using reader As FileStream = File.OpenRead("myfile.dat")
Dim bf As New BinaryFormatter
Dim btnAttrs As List(Of AttributeContainer) =
DirectCast(bf.Deserialize(reader), List(Of AttributeContainer))

For Each attr As AttributeContainer In btnAttrs
Dim btn As New Button
With btn
.Location = attr.Location
.Size = attr.Size
.Text = attr.Text
End With
Me.Controls.Add(btn)
Next
End Using

HTH
 
M

Marc

Thanks guys!

I think I might stick to the way Ive done it as serialization looks a
bit hardcore for me.

So now I have....

MyFileContents = TextFileStream.ReadLine
Dim test As New Button()
test.Text = MyFileContents
MyFileContents = TextFileStream.ReadLine
test.Location = New Point(1, 2)
Me.Controls.Add(test)


Which works fine. However I need a way for the test.location line to
read the string in form my text file? how can I do this?

i.e test.location = myfilecontents
 
T

Tom Shelton

Marc said:
The first part of the below writes the name and location of all
buttons on a from to a text file. The second part reads that
information back in and recreates the buttons. My problem is reading
the location value back in where I marked ?????????????????????. As it
wont accept a string value?


By the way, I'm going to second Cor's advice. If you aren't using
turning on Option Strict and Option Explicit, then do so. It will make
your code easier to debug, since many errors will be caught at compile
time, and in many cases make your code run faster.

I will note that there is at least one exception to the Option Strict
On. And that's doing late binding. But, that should be a in a
specific file so and a concious decision.
 
T

Tom Shelton

Marc said:
Thanks guys!

I think I might stick to the way Ive done it as serialization looks a
bit hardcore for me.

So now I have....

MyFileContents = TextFileStream.ReadLine
Dim test As New Button()
test.Text = MyFileContents
MyFileContents = TextFileStream.ReadLine
test.Location = New Point(1, 2)
Me.Controls.Add(test)


Which works fine. However I need a way for the test.location line to
read the string in form my text file? how can I do this?

i.e test.location = myfilecontents


Well, if you don't want to do seralize (which I can send you a simple
working project if you want), then you will need to parse the contents
of the line to get the values for the x and y. when you write out the
point like this, it will write out something like:

{x=100,y=2}

So, you might want to look into the strings Split method and then
Integer.Parse.
 
M

Marc

OK,

Can u send me that serialization code then Tom?

what are the advantage sof serialization? I am planning to allow the
user to save and reload the contents of the txtfile at any time
 
M

Marc

Tom,

I tried to insert your serialization code...

It has a few errors..one is that the type list is not defined (see ???
below) and the other is that file is not declared?

Private Sub Save_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Save.Click
' a container
Dim btnList As ???List??? (Of AttributeContainer) = New List(Of
AttributeContainer)
' grab the attributes
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is Button Then
btnList.Add(New AttributeContainer(ctrl.Location,
ctrl.Size, ctrl.Text))
End If
Next
' write the list in one chunk
Dim bf As New
Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Using writter As IO.FileStream =
???File???.OpenWrite("C:myfile.dat")
bf.Serialize(writter, btnList)
End Using

End Sub

Private Sub Load_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Load.Click
Using reader As IO.FileStream = File.OpenRead("C:myfile.dat")
Dim bf As New
Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim btnAttrs As List(Of AttributeContainer) =
DirectCast(bf.Deserialize(reader), List(Of
AttributeContainer))

For Each attr As AttributeContainer In btnAttr
Dim btn As New Button
With btn
.Location = attr.Location
.Size = attr.Size
.Text = attr.Text
End With
Me.Controls.Add(btn)
Next
End Using
 
C

Cor Ligthert [MVP]

Marc,

That serialization code is on our webside in the URL I gave you, I
simplified it from a longer one from Tom. Instead of an Arraylist you can
take any object as long as it is serializable.

Cor
 
C

Cor Ligthert [MVP]

I see now that I have given that link somewhere else where you have stated
this question.

Cor
 
M

Marc

OK i think im nealry there....the only error I am getting is with this
pice of code

Dim btnList As List = New List(Of AttributeContainer)


as is cannot find 'List', not sure waht to replace it with?
 
M

Marc

Fixed it, works great
added this..

Imports System
Imports System.Collections.Generic

Thanks again guys....BTW who are you guys? are you like the guardians
of VB code or something? I am over in the UK. Thanks again youve been a
big help
 
T

Tom Shelton

Marc said:
OK i think im nealry there....the only error I am getting is with this
pice of code

Dim btnList As List = New List(Of AttributeContainer)


as is cannot find 'List', not sure waht to replace it with?

What version of VB are you using? If it is 2005, then you need to
Import System.Collections.Generic. If it is 2003 or earlier, then
you'll need to change that to an array list, and then cast when
retrieving the objects - since there was no support for generics in
2003 or ealier.
 
T

Tom Shelton

Marc said:
OK,

Can u send me that serialization code then Tom?

what are the advantage sof serialization? I am planning to allow the
user to save and reload the contents of the txtfile at any time

Well the main advantage I see is that 1 you don't have to parse text.
But, it's up to you. The file in this case will not be a text file -
it will be a binary file. You know what mark, I can't really see your
full email address - so I can't send it to you. Can you see my email?
If you can, just drop a note to that address and I'll respond with an
attachment.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top