Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

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
Avatar of MajorBigDeal
MajorBigDeal
Flag of United States of America image

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
Avatar of Victor  Charles

ASKER

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
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)
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.
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.
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.
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
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

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

Victor
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
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.
Thanks, will try it when I get home.
V.
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.
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.
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.
Avatar of Nasir Razzaq
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.
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????
Please disregard last post. Thanks.
Thank You.