Hi Kevin: Here are the code blocks I use to save some user settings in one
of my apps:
First I built a class to hold the user settings:
It has several properties but the one I will comment on is set in a menu
item by the user.
I have a MenuStrip and in the ViewMenu I have a checkbox labled
ScreenReaderMenuItem.
The user can check this MenuItem if they want to use the accessibility
features of my program.
So I want to be able to save this value and ReLoad it when the user starts
up the program.
Note you could, and I have done it often, just use public variables instead
of all the code for properties.
For example:
Public ScreenReaderUserMenuItemValue As Boolean = False
Public ReportsDirectoryValue As String = ""
Etc...
I am not sure you can use Class Level variables inside a C# project so
properties would be the option in that case.
Below is the code for the class and the code I use in my RootForm to load
and save the properties in this class.
I am not sure this is the cleanest version, it is from an older project, but
it has worked well for some time now.
<BeginCopiedClassCode>
' You will use the Using instead of Imports
Imports System.Xml.Serialization
' You will use the C# version which uses brackets I think instead of < for
the attribute tag.
<Serializable()> _
Public Class BFIUserSettings
Dim _ReportsDirectoryValue As String = ""
Dim _ScreenReaderUserMenuItemValue As Boolean
Public Property ScreenReaderUserMenuItemValue As Boolean
Get
Return _ScreenReaderUserMenuItemValue
End Get
Set(ByVal Value As Boolean)
_ScreenReaderUserMenuItemValue = Value
End Set
End Property
Public Property ReportsDirectoryValue As String
Get
Return _ReportsDirectoryValue
End Get
Set(ByVal Value As String)
_ReportsDirectoryValue = Value
End Set
End Property
End Class
<EndCopiedClassCode>
Here, in my load program I see if the file has already been saved once and
if so I load it.
This way the default settings are used the very first the program is started
and after that the user settings will be used if the user changes anything.
Private Sub RootForm_Load( ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles MyBase.Load
MethodID = "RootForm_Load"
' Below I just call a subroutine I added to the RootForm Code file.
' The code for the sub is below.
' I have a FilePaths class where I hold all my file paths and url addresses:
If File.Exists( FilePaths.BFIUserSettingsFilePath) Then
LoadBFIUserSettings()
End If
End Sub
Below is the code to save the values.
Private Sub RootForm_FormClosing( ByVal sender As System.Object, ByVal e As
System.Windows.
Forms.FormClosingEventArgs) _
Handles MyBase.FormClosing
MethodID = "RootForm_FormClosing"
' here we dont save anything if the user presses escape, code not provided
here.
' We Otherwise we save the file.
If Not _EscapePressed Then
SaveBFIUserSettings()
End If
End Sub
' Here is the code to serialize and deserialize (load and save) the class.
Public Sub LoadBFIUserSettings()
MethodID = "LoadBFIUserSettings"
' Load the ProjectLevel Settings:
' Instantiate a copy of the class we created above.
Dim BFIUserSettings As New BFIUserSettings()
' I like to save to xml so use the .net XmlSerializer class.
Dim xs As New XmlSerializer(GetType(BFIUserSettings))
' I use a .net StreamReader to provide the datastream during the
DeSerialization.
Dim sr As New IO.StreamReader( FilePaths.BFIUserSettingsFilePath )
BFIUserSettings = CType(xs.Deserialize(sr), BFIUserSettings)
sr.Close()
' Below I set the ScreenReaderMenuItem to reflect the saved User Setting.
If BFIUserSettings.ScreenReaderUserMenuItemValue = True Then
ScreenReaderUserMenuItem.Checked = True
Globals.ScreenReaderUser = True
Else
ScreenReaderUserMenuItem.Checked = False
Globals.ScreenReaderUser = False
End If
Globals.ReportsDirectoryPath = BFIUserSettings.ReportsDirectoryValue
End Sub
Public Sub SaveBFIUserSettings()
MethodID = "SaveBFIUserSettings()"
' Save ProjectLevel User Settings:
' again instantiate the class.
Dim BFIUserSettings As New BFIUserSettings()
' Save the value of the check state of the ScreenReader User Setting as it
is in the MenuItem.
BFIUserSettings.ScreenReaderUserMenuItemValue = Globals.ScreenReaderUser
BFIUserSettings.ReportsDirectoryValue = Globals.ReportsDirectoryPath
Dim xs As New XmlSerializer(GetType(BFIUserSettings))
' Same as the .net objects but using a StreamWriter this time.
Dim sw As New IO.StreamWriter( FilePaths.BFIUserSettingsFilePath )
xs.Serialize(sw, BFIUserSettings)
sw.Close()
End Sub
Imports System.Xml.Serialization
<Serializable()> _
Public Class BFIUserSettings
Dim _ReportsDirectoryValue As String = ""
Dim _ScreenReaderUserMenuItemValue As Boolean
Public Property ScreenReaderUserMenuItemValue As Boolean
Get
Return _ScreenReaderUserMenuItemValue
End Get
Set(ByVal Value As Boolean)
_ScreenReaderUserMenuItemValue = Value
End Set
End Property
Public Property ReportsDirectoryValue As String
Get
Return _ReportsDirectoryValue
End Get
Set(ByVal Value As String)
_ReportsDirectoryValue = Value
End Set
End Property
End Class
----- Original Message -----
From: "Kevin Morales" <[email protected]>
To: <[email protected]>
Sent: Thursday, March 28, 2013 9:27 AM
Subject: Re: Question: Window-Eyes and .NET.
Thank you Rick! In fact, admittedly, I do happen to have a question:
How do you particularly deal with designing user settings? What tools
do you use? I found that I actually need to store user data and saveit
to disk; I found ways to do it, but I'm wondering how other .NET
programmers like you deal with this.
Do you use Visual Studio? Do you need a screen reader to program or
are you able to see the screen yourself?
On 3/28/13, RicksPlace <[email protected]> wrote:
Hi Kevin:
That sounds like a really excellent project.
I am pretty good with .net programming, vb.net in particular since I hate
curly braces, so if you need some help just post up any questions or
problems and I will see if I can help out with an answer or 2.
Rick USA