Solved

Help with saving settings using VB.NET

Posted on 2014-03-04
19
296 Views
Last Modified: 2014-03-06
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
Comment
Question by:vcharles
  • 11
  • 4
  • 3
  • +1
19 Comments
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 39905019
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
 

Author Comment

by:vcharles
ID: 39905034
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
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 39905284
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
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 39905288
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
 

Author Comment

by:vcharles
ID: 39905313
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
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 39905411
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
 

Author Comment

by:vcharles
ID: 39905446
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
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 39905710
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
 

Author Comment

by:vcharles
ID: 39906086
Hi,

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

Victor
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:vcharles
ID: 39908093
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
 
LVL 40
ID: 39908136
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
 

Author Comment

by:vcharles
ID: 39908322
Thanks, will try it when I get home.
V.
0
 

Author Comment

by:vcharles
ID: 39908426
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
 
LVL 40
ID: 39908528
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
 

Author Comment

by:vcharles
ID: 39908918
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39909035
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
 

Author Comment

by:vcharles
ID: 39909294
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
 

Author Comment

by:vcharles
ID: 39909561
Please disregard last post. Thanks.
0
 

Author Closing Comment

by:vcharles
ID: 39911607
Thank You.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now