Victor Charles
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
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
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
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}",se ttingsFile Prefix",us erName)
String.Format("{0}_{1}",se
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.
ASKER
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(For mSearch.C1 Screen1.Co lumns(1).V isible.ToS tring())
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.
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(For
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:
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.
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>
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.
ASKER
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(For mSearch.C1 Screen1.Co lumns(1).V isible.ToS tring())
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
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(For
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
Thank you very much for all the solutions, I will get back to you.
Victor
Thank you very much for all the solutions, I will get back to you.
Victor
ASKER
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
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.Setting s.CheckBox 20.ToStrin g)
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.Setting s.CheckBox 20.ToStrin g)
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.
----
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.Setting
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.Setting
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.
ASKER
Thanks, will try it when I get home.
V.
V.
ASKER
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,Checkb ox3,Checkb ox4 etc..
Thanks.
V.
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,Checkb
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.
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.
ASKER
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.
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.
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.
ASKER
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????
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????
ASKER
Please disregard last post. Thanks.
ASKER
Thank You.
Open in new window
I didn't compile this but it should be close