|
-
Apr 9th, 2013, 02:50 PM
#1
Thread Starter
Hyperactive Member
Working with text files to read/write configurations
I'm writting a program where I save the properties of the program in a text file.
In the 4 combo-boxes I select the properties and I want the program to save the index of the combobox in the txt file.
Example:
In the first combo box I select the 0
In the second... the 4
etc...
In the text file I want to save:
0
4
...
The first line correspond to the first combo box, and so on...
I've to said that the last week I stopped using VB6 and I started with VB.Net 2010, so I've found some differences.
-
Apr 9th, 2013, 03:01 PM
#2
Re: Working with text files to read/write configurations
Why are you saving it to a text file? why not property bind each combobox.
-
Apr 9th, 2013, 03:28 PM
#3
Re: Working with text files to read/write configurations
some thing like
vb Code:
Imports System.IO Public Class Form1 Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing Dim indexs As New List(Of Integer)((From cb In Me.Controls.OfType(Of ComboBox)() Select cb.SelectedIndex).Reverse) ' Handle how ever End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim n() As Integer = {0, 1, 2, 3} Dim comboboxes() As ComboBox = {Me.ComboBox1, Me.ComboBox2, Me.ComboBox3, Me.ComboBox4} Array.ForEach(Enumerable.Range(0, 4).ToArray, Sub(x) comboboxes(x).SelectedIndex = n(x)) End Sub End Class
-
Apr 9th, 2013, 03:33 PM
#4
Re: Working with text files to read/write configurations
Do you have to use a text file? In .NET you have a pretty nice option in My.Settings which will take care of the storage for you.
My usual boring signature: Nothing
 
-
Apr 9th, 2013, 03:53 PM
#5
Re: Working with text files to read/write configurations
As SH agree's. Property bind it using application settings.
-
Apr 9th, 2013, 03:57 PM
#6
Thread Starter
Hyperactive Member
Re: Working with text files to read/write configurations
 Originally Posted by ident
some thing like
vb Code:
Imports System.IO Public Class Form1 Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing Dim indexs As New List(Of Integer)((From cb In Me.Controls.OfType(Of ComboBox)() Select cb.SelectedIndex).Reverse) ' Handle how ever End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim n() As Integer = {0, 1, 2, 3} Dim comboboxes() As ComboBox = {Me.ComboBox1, Me.ComboBox2, Me.ComboBox3, Me.ComboBox4} Array.ForEach(Enumerable.Range(0, 4).ToArray, Sub(x) comboboxes(x).SelectedIndex = n(x)) End Sub End Class
I've thought about using a text file because it was the only way I knew, and also for the reason that the program has to show the values in a DataGridView control when I enter one value. For example I has 3 users and each user has 2 possible options (2 combo boxes). The first user is tall and blonde, the second is small and brown. Well, I select the characteristics of each user in 2 combo boxes. Then when I enter the user number in the text control, the datagridview show me for example... "User 1: tall - blonde".
I'll study the code you suggest because it's very new for me and it will take me some time to understand. Thx!
-
Apr 9th, 2013, 04:00 PM
#7
Re: Working with text files to read/write configurations
The example i showed could be worked into your application using a textfile.
My.settings requires no code. Select the control - properties - application settings & go from there
-
Apr 9th, 2013, 04:05 PM
#8
Thread Starter
Hyperactive Member
Re: Working with text files to read/write configurations
Thanks to both,
I'm just starting in vb 2010 and it's very different from vb6. More if I haven't read any kind of documentation before starting with it.
I'm reading both suggestions and I understand them.
-
Apr 9th, 2013, 05:23 PM
#9
Re: Working with text files to read/write configurations
I'm not so keen on all that LINQ that ident showed. I was into it at one time, but now I just feel that it confuses people for no particular gain. It's short code, but it's short, dense, and slow. Only two of those adjectives are desirable in code.
My.Settings is particularly easy. Take a look on the Project|Properties Settings tab. You can add things there of various basic types. They act like variables in your code (like member properties in My.Settings, actually), but they (usually) save automatically, and remain with the program. If you get into lots of them, I think that some other file option would be better, but for a relatively small number they are pretty good. Where that cutoff is I can't say, nor do I know whether or not there is an issue (other than the pure tedium of setting them up) of having a large number of settings.
My usual boring signature: Nothing
 
-
Apr 10th, 2013, 01:58 AM
#10
Thread Starter
Hyperactive Member
Re: Working with text files to read/write configurations
I'll try My.Settings because it's quite easy for me. Just one question... what kind of variable is a color code? String type?
I use a colordialog to show the colors in the runtime and select it.
-
Apr 10th, 2013, 03:02 AM
#11
Thread Starter
Hyperactive Member
Re: Working with text files to read/write configurations
Answer of the last question -> System.Drawing.Color variable
At the moment I've 40 users, each one with 5 different properties that I select in 5 individual combo-boxes.
If I use My.Settings... I would need to use 200 variables. I've made some test with 3 users and it's very easy to work with My.Settings, but not for 200 variables.
There is a way to use My.Settings..?
but instead of using:
Property1_User1
Property2_User1
(...)
Property5_User40
Use something like...
Property1_User(I) where(I) means the usernumber? Like a matrix in the my.settings. Is possible?
-
Apr 10th, 2013, 10:09 AM
#12
Re: Working with text files to read/write configurations
You can also readily use an Integer for a color. You do have some conversion to make, but that's usually not too hard since a color is either an RGB (three bytes) or an ARGB (four bytes). They generally have to be unsigned integers, but an integer is fundamentally an integer, whether it is signed or not.
200 variables would be a total pain to set up. You do have a couple options, though. One is to use the string collection, which is called something like Specialized String Collection, and acts like a List (of String), though I believe it predated the addition of generics. The problem with that solution is that you are just using strings, so you'd always be casting back to numbers, which is hardly ideal, but may be acceptable.
Another alternative is to make up a class that holds all the values you want. You can then serialize/deserialize that class to and from a Base64 encoded string and put the whole class into a single property. That's easier to do than it may sound, especially since I've posted the code to do exactly that a couple times. The serialize/deserialize methods are pretty simple (I may have gotten them from JMC, too, but by now I have forgotten), as is making a class suitable for serialization. However, changing your design so that it fills in members of a class and restores from a class would be a real change.
My usual boring signature: Nothing
 
-
Apr 12th, 2013, 01:08 PM
#13
Thread Starter
Hyperactive Member
Re: Working with text files to read/write configurations
I'll start with the string collection, because I tried to use My.Settings and at the moment I've 200 but maybe latter I would need to use a higher number.
-
Apr 12th, 2013, 01:57 PM
#14
Thread Starter
Hyperactive Member
Re: Working with text files to read/write configurations
I've use this thing because I thing it's more easy for me:

Uploaded with ImageShack.us
The question is... How can I write into the files? This code read the settings, but it doesn't write anything.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|