Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 305
  • Last Modified:

Help with saving settings using VB.NET

Hi,

I'm using the code below to hide columns depending on checked ckeckboxes. How do I save my setting so when I log back in again I can us the same settings?

Private Sub setfields()
        On Error Resume Next
        With FormSearch.C1Screen1
             If Me.CheckBox1.Checked Then
            .Columns(1).Visible = True
            Else
            .Columns(1).Visible = False
            End If

            If Me.CheckBox2.Checked Then
            .Columns(2).Visible = True
            Else
            .Columns(2).Visible = False
            End If

            If Me.CheckBox3.Checked Then
             .Columns(3).Visible = True
             Else
            .Columns(3).Visible = False
             End If

            If Me.CheckBox4.Checked Then
                .Columns(4).Visible = True
            Else
                .Columns(4).Visible = False
            End If
        End With
End Sub


Thanks,

Victor
0
vcharles
Asked:
vcharles
  • 11
  • 4
  • 3
  • +1
1 Solution
 
MajorBigDealCommented:
Normally I would do this with a database but I'm assuming that is not easily available to you or you would not have asked the question.  So here is a way to do it with a file:

Dim settingsfile As TextWriter = New StreamWriter("pathtofile")
settingsfile.WriteLine(FormSearch.C1Screen1.Columns(1).Visible.ToString())
settingsfile.Close()

And to read it back in:

Dim settingsfile As TextReader = New StreamReader("pathtofile")
' convert the following to bool to set the property
settingsfile.ReadLine() 
settingsfile.Close()

Open in new window


I didn't compile this but it should be close
0
 
vcharlesAuthor Commented:
Hi,

Is there a way to associate the file saved with the Login information? For example if I login as userA to would like to save my setting for UserA, and when userB selects a different setting it would save and activate according to thye users settings.

Thanks,

Victor
0
 
MajorBigDealCommented:
A database which would be ideal for that.  But, again assuming you don't want to use a database, one way would be to append the use name  to the name of the file, for example

String.Format("{0}_{1}",settingsFilePrefix",userName)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
MajorBigDealCommented:
That way, each user could have a separate file all located in the same folder.  The file names would all be the same except they would have the name of the user at the end.
0
 
vcharlesAuthor Commented:
Hi,

I'm using xml as my datasource. I assume I need to use the code below for every column?
will this have to be a .txt file?

Dim settingsfile As TextWriter = New StreamWriter("pathtofile")
With FormSearch.C1Screen1
             If Me.CheckBox1.Checked Then
            .Columns(1).Visible = True
            Else
            .Columns(1).Visible = False
            settingsfile.WriteLine(FormSearch.C1Screen1.Columns(1).Visible.ToString())
            End If

In FormLoad event use the following code?

Dim settingsfile As TextReader = New StreamReader("pathtofile")
' convert the following to bool to set the property
settingsfile.ReadLine()
settingsfile.Close()

Is there an easier way than saving column settings in multiple files?

Thanks,

V.
0
 
MajorBigDealCommented:
XML is a better choice for this then a text file.  I was just using a text file because I did not know you were already using XML.  

The code I gave you was not exact, for example you need to assign the response from ReadLine to a variable and of course, if there is more than one line you would need to put it in a loop.

Storing all the settings in a single file is cleaner but I don't think it is easier.    If you use a separate file for each user by simply appending the user name to the file name then your logic only has to consider a single user (the one whose file you are reading).  

I'm totally sympathetic with the idea that you don't want to have a bunch of user files.  If I were to do this in a single XML file, it would look something like this:

<user id="UserA">
<col id="1" visible="false" />
<col id="2" visible="true" />
<col id="3" visible="true" />
<col id="4" visible="true" />
</user>

<user id="UserB">
<col id="1" visible="true" />
<col id="2" visible="true" />
<col id="3" visible="false" />
<col id="4" visible="false" />
</user>

Open in new window


Of course, then you would need some logic to select the appropriate section based on the user but that is very easy to do in XML.
0
 
vcharlesAuthor Commented:
Hi,

Thanks for the explanation, should the code for one column be as follows?

Dim settingsfile As TextWriter = New StreamWriter("C:\usera.xml")
With FormSearch.C1Screen1
             If Me.CheckBox1.Checked Then
            .Columns(1).Visible = True
            Else
            .Columns(1).Visible = False
            settingsfile.WriteLine(FormSearch.C1Screen1.Columns(1).Visible.ToString())
            End If

In Form Load event use the following code?

if logim.text = usera then
Dim settingsfile As TextReader = New StreamReader("c:\Usera.xml")
' convert the following to bool to set the property
settingsfile.ReadLine()
settingsfile.Close()
End If
0
 
Jacques Bourgeois (James Burger)Commented:
The feature to do that is already built into Visual Studio.

In the project's Properties window, you go into the Settings tab and give a name for each value that you want to record. Could be the name of the control.

In your case, the settings should be set to Boolean

Make sure to define it as a User setting. User settings are read-write and are managed individually for each user. Application settings are read-only and shared by all the users.

Also give a default value that will be the one that will be read the first time a user start the application.

These settings are saved in the .config file (a XML file) that is generated along the application, and needs to be distributed with the application. When a user starts the application for the first time, the user settings are read from that file, but when you make changes later, they are saved in one of the user's personal directory, where they will be retrieved for future runs of the application.

Visual Studio, also created a class with typed properties corresponding to each of the settings that you created, so no need to read and write the XML or manage the files for different users yourself. In your code, when you want to save the settings, supposing that you have given them the name of your controls, you simply do the following.

My.Settings.CheckBox1 = CheckBox1.Checked
My.Settings.CheckBox2 = CheckBox2.Checked
...
My.Settings.Save

When you want to read back the settings, no need to Open, this is done automatically when you start the application and they are available whenever you need them. You simply read them back:

CheckBox1.Checked=My.Settings.CheckBox1
...
CheckBox4.Checked=My.Settings.CheckBox4
0
 
vcharlesAuthor Commented:
Hi,

Thank you very much for all the solutions, I will get back to you.

Victor
0
 
vcharlesAuthor Commented:
Hi,

I'm not sure what I'm doing wrong.

In Project Setting I set the following

Checkbox 20    User    True

In the Save Button I have the following code

With Form2.C1CTRY
If Me.CheckBox20.Checked Then
.Columns(20).Visible = True
Else
.Columns(20).Visible = False

My.Settings.CheckBox20 = CheckBox20.Checked
My.Settings.Save()
End With



 In Form Load Event I have:

CheckBox20.Checked = My.Settings.CheckBox20

Bu If I unckeck CheckboxBox and reload the application, column for Column20 is still visible.


Thanks,

Victor
0
 
Jacques Bourgeois (James Burger)Commented:
Have you set your entry as a Boolean in the settings?

----

Is the CheckBox checked on the screen after the Form_Load. If yes, then it may be that you did not call the code that sets the visibility of the column.

----

Make sure that you have the right value at the point where you are saving and that you are not saving in different places.

Try the following:

My.Settings.CheckBox20 = CheckBox20.Checked
MessageBox.Show(My.Settings.CheckBox20.ToString)
My.Settings.Save()

This will tell you what value will be saved in the configuration file.

Then do something similar in your Form_Load:

MessageBox.Show(My.Settings.CheckBox20.ToString)
CheckBox20.Checked = My.Settings.CheckBox20

This will tell you what value is returned from the configuration file. If the value is the one you are expecting, then the problem is in your code somewhere.
0
 
vcharlesAuthor Commented:
Thanks, will try it when I get home.
V.
0
 
vcharlesAuthor Commented:
Hi,

The test work, something else is wrong with the code, running more test.

Is there a way to loop through the checkboxes to achieve the same if they are named sequentially? in order to avoid writing the same code multiple times.
For example Checkbox1,Checkbox2,Checkbox3,Checkbox4 etc..

Thanks.

V.
0
 
Jacques Bourgeois (James Burger)Commented:
You could loop through the CheckBoxes themselves, because they can be referenced with a String: Me.Controls("CheckBox1").

But you could not do it through the Settings, because they are properties, and properties need to be called explicitely in the code.

It should be possible to build something in saving the Setting as a Collection, but as far as I know, the only Collection that can be used in the Settings in a StringCollection. The overhead of converting from Boolean to String and the reverse when reading the values might not be worth the trouble, unless you really have a great number of CheckBoxes.
0
 
vcharlesAuthor Commented:
Hi.

I have about 40 checkboxes one for each field so users can select which items they want to see, it may be worth it, if it won't slow down the runtime.

Running more test today, will get back to you.

Thanks,

V.
0
 
CodeCruiserCommented:
One option is to use a single string setting and then concatenate all combobox true/false or 1/0 using a separator like ;. Would require some string manipulation and parsing but would reduce your lines of code.
0
 
vcharlesAuthor Commented:
Hi,

I have another quick question hopefully I don't have to open another issue, my propoerties box just dissapeared, went to View and Clicked on Properties Window, but it still does not appear when I click on a control and press the properties Windows, any ideas what went wrong????
0
 
vcharlesAuthor Commented:
Please disregard last post. Thanks.
0
 
vcharlesAuthor Commented:
Thank You.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 11
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now