Saving user prefernces ( slider . check boxes... )

  • Thread starter Thread starter Dan DeConinck
  • Start date Start date
D

Dan DeConinck

Hello,

I want to save the position of all the controls on my form. I would like to
write them out to a preference file.

I have done this in DOS BASIC like this:

TO WRITE the preference
open "c:\preferences.txt" for input as #1
input #1 ,a,b,c
close #1

TO READ the preferences
open "c:\preferences.txt" for output as #1
write #1,a,b,c
c lose #1

What are the VB.NET commands to do this ?


Thanks
Dan
 
Hi try This,

Dim c As Control
Dim sw As StreamWriter = File.CreateText("C:\PREF.TXT")

For Each c In Me.Controls
sw.WriteLine(c.Name & ":" & c.Location.ToString())
Next

sw.Close()

Produces something like this.

Button1:{X=56,Y=56}
RadioButton1:{X=176,Y=240}
CheckBox1:{X=88,Y=184}
TextBox1:{X=72,Y=128}

Regards - OHM
 
* "One Handed Man said:
Produces something like this.

Button1:{X=56,Y=56}
RadioButton1:{X=176,Y=240}
CheckBox1:{X=88,Y=184}
TextBox1:{X=72,Y=128}

And how to read this data?

;-)
 
Your such a smart*ss sometimes Herfreid, this is just an example, he can CSV
it or something

:)

OHM
 
Hi OHM,

There was I time that I had fun prickling Herfried with that kind of answers
he did do to you. I stopped with it, I thought in the time of the Fergus <->
Herfried battle.

What do you think if I start it again a while and then we do it together
(Armin does it still sometimes)?

Cor
 
OK, why not, its about time that someone did this :), but dont tell anyone
as I dont want them to know OK!

:)

Regards - OHM
 
With some embarrasement, I have to admit I was thinking of asking the same
question, but not with any smartss tendency :-) so let me phrase it a
little differently...

I'm still kinda new to programming, and I am designing a new CF project
where I want to add standard controls at runtime, using some type of file
to store the control(s) names, types, location, content (comboboxes,
textboxes), state (radiobuttons, checkboxes), etc.

Users may edit these through my program, so when the program closes, I'll
probably update the file if needed.

The OP asked (more specifically) about writing a preference file; is this
the best approach for storing and uploading control info to be used at
runtime, or are there better built-in functions that I don't know about? Or
are there other (easier) ways to do the same, including possibly saving an
XML file with the same information? I assume I would be looking at the
form.load area of the program to open the file and pull in the information,
and use it to create the controls...

Even giving me an idea of where to look in the VS helpfile (VB.net) would
be great, I simply don't know enough yet to "know what I don't know"

Humbly,
Keith
 
Hi Keith,

I was also looking at this question but the OP did ask it in a very
specific way, which I did not know how to give an answer on but OHM did.

I saw that Herfried had also a question about this, but I think there is a
better approach.

If that does not answer the question from Herfried I will look if I can help
him with his problem how to read such a file.

To give you a more direct answer, the most simple way to save this settings
is in the registry or in an XML file.

Although it looks difficult a dataset is very simple to make (I did give a
lot of samples the last days with it in this newsgroup but if you need it I
can do it again).

That dataset you can easely writen with mydatset.writexml(mypath) and read
with mydataset.readxml(mypath)

If hope this helps, and if not feel free to ask?

Cor
 
Ken gave this URL which has a discussion on saving configuration data. by
Rocky Lochta or whatever his name is. Basically, the recommendations are use
the registry, XML or some preference file based on the current user.

This is the second time I have answered exactly what the poster asked for
and either got flamed for it or found that the OP wanted something different
entirely. Maybe Im just getting tired.

Regards - OHM


Keith said:
With some embarrasement, I have to admit I was thinking of asking the
same question, but not with any smartss tendency :-) so let me phrase
it a little differently...

I'm still kinda new to programming, and I am designing a new CF
project where I want to add standard controls at runtime, using some
type of file to store the control(s) names, types, location, content
(comboboxes, textboxes), state (radiobuttons, checkboxes), etc.

Users may edit these through my program, so when the program closes,
I'll probably update the file if needed.

The OP asked (more specifically) about writing a preference file; is
this the best approach for storing and uploading control info to be
used at runtime, or are there better built-in functions that I don't
know about? Or are there other (easier) ways to do the same,
including possibly saving an XML file with the same information? I
assume I would be looking at the form.load area of the program to
open the file and pull in the information, and use it to create the
controls...

Even giving me an idea of where to look in the VS helpfile (VB.net)
would be great, I simply don't know enough yet to "know what I don't
know"

Humbly,
Keith


One Handed Man said:
Your such a smart*ss sometimes Herfreid, this is just an example, he
can CSV it or something

:)

OHM
* "One Handed Man [ OHM# ]" <O_H_M{at}BTInternet{dot}com> scripsit:
Produces something like this.

Button1:{X=56,Y=56}
RadioButton1:{X=176,Y=240}
CheckBox1:{X=88,Y=184}
TextBox1:{X=72,Y=128}

And how to read this data?

;-)
 
Hi Herfried,
And how to read this data?

I know that when I have a question I never get an answer from you.

But I never answer bad with bad, and maybe I thought Herfried needs this
question for his studie where he is so busy with at the moment, so I made
an sample for you.

Look to it because I am almost sure you can learn something from it.

I hope it helps?

Cor

\\\
Public Module Main
Sub main()
Dim HKWstring() As String = {"Button1:{X=56,Y=56}",
"RadioButton1:{X=176,Y=240}", _
"CheckBox1:{X=88,Y=184}", "TextBox1:{X=72,Y=128}"}
'Herfried to make the sample simple for you I have first made an
array of strings
Dim sw As New IO.StreamWriter("C:\Hkw.txt")
For i As Integer = 0 To HKWstring.Length - 1
'For is another word as "during" however in Cobol it was
perform,
'I do not know if it is, but I see it always as an abbreviation
from that.

'The procedure has to be performed until i is equal to the
arraylength - 1
'maybe a little bit strange for you but an index starts on 0.

'I know you find it now normal that it is 1 and that would be
more normal,
'but this is an old habbit in programming.

sw.WriteLine(HKWstring(i))

'I use i as an index
'every time in a perform loop will the index set up by 1
Next
'the array will be written
sw.Flush()
'this flushes the buffer
sw.Close()
'the file will be given free to read

'Now the file is written we can start with your question how to read
this
Dim sr As New IO.StreamReader("C:\Hkw.txt")
Dim line As String
' Read and display the lines from the file until the end
' of the file is reached.
line = sr.ReadLine()
Do Until line Is Nothing
' I find this the nicest method
' Is nothing means that the line not exist
' if it was empty it had been = nothing or like others do
' = "", however than it is still a line (a string of zero
characters)
' placed on an existing virtual memory adres

MessageBox.Show(line)
' I placed this so that you can real check that it is been read
line = sr.ReadLine()
Loop
sr.Close()
End Sub

End Module
////
 
Hi Herfried,

It was at bottom line and it did cost me to much time that I did not want
that you would not read it.

:-))

Cor
 
* "Cor said:
It was at bottom line and it did cost me to much time that I did not want
that you would not read it.

I read your post. Very interesing, indeed!
 
Not intresting (the perform is maybe for you) only written for fun (but not
wrong) because you made that answer to OHM.

Cor
 
Back
Top