469,926 Members | 1,901 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

Help reading control.location value back form text file

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

Nov 24 '06 #1
13 1690

Marc wrote:
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

--
Tom Shelton

Nov 25 '06 #2
Marc,

First set Option Strict On in your program, than have a look at the property
"location".

http://msdn2.microsoft.com/en-gb/lib....location.aspx

You see it are points, so to put it back you need first to set what is
written to points,
probably the top and left property are easier to start with,

Cor
"Marc" <ma*********@hotmail.comschreef in bericht
news:11*********************@l39g2000cwd.googlegro ups.com...
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

Nov 25 '06 #3
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

Cor Ligthert [MVP] wrote:
Marc,

First set Option Strict On in your program, than have a look at the property
"location".

http://msdn2.microsoft.com/en-gb/lib....location.aspx

You see it are points, so to put it back you need first to set what is
written to points,
probably the top and left property are easier to start with,

Cor
"Marc" <ma*********@hotmail.comschreef in bericht
news:11*********************@l39g2000cwd.googlegro ups.com...
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
Nov 25 '06 #4

Marc wrote:
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.

--
Tom Shelton

Nov 25 '06 #5

Marc wrote:
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.

--
Tom Shelton

Nov 25 '06 #6
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

Tom Shelton wrote:
Marc wrote:
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.

--
Tom Shelton
Nov 25 '06 #7
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.BinaryForm atter()
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.BinaryForm atter
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
Marc wrote:
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

Tom Shelton wrote:
Marc wrote:
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.

--
Tom Shelton
Nov 25 '06 #8
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

"Marc" <ma*********@hotmail.comschreef in bericht
news:11**********************@14g2000cws.googlegro ups.com...
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

Tom Shelton wrote:
>Marc wrote:
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.

--
Tom Shelton

Nov 25 '06 #9
I see now that I have given that link somewhere else where you have stated
this question.

Cor

"Marc" <ma*********@hotmail.comschreef in bericht
news:11*********************@l12g2000cwl.googlegro ups.com...
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

Cor Ligthert [MVP] wrote:
>Marc,

First set Option Strict On in your program, than have a look at the
property
"location".

http://msdn2.microsoft.com/en-gb/lib....location.aspx

You see it are points, so to put it back you need first to set what is
written to points,
probably the top and left property are easier to start with,

Cor
"Marc" <ma*********@hotmail.comschreef in bericht
news:11*********************@l39g2000cwd.googlegr oups.com...
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

Nov 25 '06 #10
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?

Cor Ligthert [MVP] wrote:
I see now that I have given that link somewhere else where you have stated
this question.

Cor

"Marc" <ma*********@hotmail.comschreef in bericht
news:11*********************@l12g2000cwl.googlegro ups.com...
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

Cor Ligthert [MVP] wrote:
Marc,

First set Option Strict On in your program, than have a look at the
property
"location".

http://msdn2.microsoft.com/en-gb/lib....location.aspx

You see it are points, so to put it back you need first to set what is
written to points,
probably the top and left property are easier to start with,

Cor
"Marc" <ma*********@hotmail.comschreef in bericht
news:11*********************@l39g2000cwd.googlegro ups.com...
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
Nov 25 '06 #11
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

Tom Shelton wrote:
Marc wrote:
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

--
Tom Shelton
Nov 25 '06 #12

Marc wrote:
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.

--
Tom Shelton

Nov 25 '06 #13

Marc wrote:
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.

--
Tom Shelton

Nov 25 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Ben | last post: by
reply views Thread by mcp6453 | last post: by
2 posts views Thread by Sudheer Kareem | last post: by
7 posts views Thread by Trvl Orm | last post: by
2 posts views Thread by MyNameIsnt | last post: by
1 post views Thread by Michael D. Reed | last post: by
1 post views Thread by Rahul | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.