ApplicationSettingsBase and Usercontrols

S

Seth Gecko

Hi

I have a custom usercontrol (a sort of grid) in which I want to store
the width of all columns. I only want to do this during runtime, when
the user has adjusted them to whatever width he/she prefers.
Elsewhere in my application I use a custom class, which inherits
ApplicationSettingsBase and this works fine. For this usercontrol, I
would do same, so I have created the small class, listed below. Since
my usercontrol will reside on a number of forms, I have to have
different settings for each instance of the usercontrol. According to
MSDN, the SettingsKey is exactly what I need. Except, I can't get it
to work. Neither the constructor shown, nor setting
[UserSettingsInstance].SettingsKey = "SomeText" has any effect. The
SpreadWidth returned is the same no matter the value of SettingsKey.
MSDN mentions that usercontrols have to implement
IPersistComponentSettings, but that is a little vague and I only want
to store settings during runtime, not designtime.

Suggestions, please?

Private Class UserSettings
Inherits System.Configuration.ApplicationSettingsBase

Public Sub New(ByVal settingsKey As String)
MyBase.New(settingsKey)
End Sub

<Configuration.UserScopedSetting()> _
Public Property SpreadWidth() As List(Of Single)
Get
If (Me.Item("SpreadWidth") Is Nothing) Then
'This only happens the very first time we run this
application
Me.Item("SpreadWidth") = New List(Of Single)
End If
Return CType(Me.Item("SpreadWidth"), List(Of Single))

End Get
Set(ByVal value As List(Of Single))
Me.Item("SpreadWidth") = value
End Set
End Property
End Class
 
R

RobinS

See if this helps you at all.

Here is an example of saving and restoring settings that have their own
"section". In this example, I put FormSize and FormLocation in my Settings,
and then I can save different values for each form for those two settings.
I think this is what you are looking for -- to save the settings of your
usercontrol for each form on which it resides.

This resides in my base form and is inherited by all of my forms. It saves
and then later restores the same setting name, but using the Form Name as
the setting key.

Private _settings As My.MySettings
Private ReadOnly Property Settings() As _
System.Configuration.ApplicationSettingsBase
Get
If _settings Is Nothing Then
_settings = New My.MySettings
End If
Return _settings
End Get
End Property

'In my Form_Closing
SetSettings()

'In my Form_Load
ApplySettings()

Private Sub ApplySettings()
Settings.SettingsKey = Me.Name 'This is the section name.
Dim theSettings As My.MySettings
theSettings = DirectCast(Settings, My.MySettings)
If theSettings.FormSize <> Drawing.Size.Empty Then
Me.Size = theSettings.FormSize
End If
If theSettings.FormLocation <> Drawing.Point.Empty Then
Me.Location = theSettings.FormLocation
End If
End Sub

Private Sub SetSettings()
Settings.SettingsKey = Me.Name 'This is the section name.
Dim theSettings As My.MySettings
theSettings = DirectCast(Settings, My.MySettings)

If Me.WindowState = FormWindowState.Normal Then
theSettings.FormSize = Me.Size
Else
'if the form was maximized or minimized,
' return to the restore state
theSettings.FormSize = Me.RestoreBounds.Size
End If
theSettings.FormLocation = Me.Location
Settings.Save()
End Sub


And just to save you some trouble in case you want to look at the XML
created, this writes to the user.config file, and on my computer (XP), this
goes into

"C:\Documents and Settings\myusername\Local Settings\Application Data\
\myappname\myappname.exe_gobbledybook\versionnumber\user.config"

Good luck. Hope this helps in *some* way!
Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
-----------------------------------------------
Seth Gecko said:
Hi

I have a custom usercontrol (a sort of grid) in which I want to store
the width of all columns. I only want to do this during runtime, when
the user has adjusted them to whatever width he/she prefers.
Elsewhere in my application I use a custom class, which inherits
ApplicationSettingsBase and this works fine. For this usercontrol, I
would do same, so I have created the small class, listed below. Since
my usercontrol will reside on a number of forms, I have to have
different settings for each instance of the usercontrol. According to
MSDN, the SettingsKey is exactly what I need. Except, I can't get it
to work. Neither the constructor shown, nor setting
[UserSettingsInstance].SettingsKey = "SomeText" has any effect. The
SpreadWidth returned is the same no matter the value of SettingsKey.
MSDN mentions that usercontrols have to implement
IPersistComponentSettings, but that is a little vague and I only want
to store settings during runtime, not designtime.

Suggestions, please?

Private Class UserSettings
Inherits System.Configuration.ApplicationSettingsBase

Public Sub New(ByVal settingsKey As String)
MyBase.New(settingsKey)
End Sub

<Configuration.UserScopedSetting()> _
Public Property SpreadWidth() As List(Of Single)
Get
If (Me.Item("SpreadWidth") Is Nothing) Then
'This only happens the very first time we run this
application
Me.Item("SpreadWidth") = New List(Of Single)
End If
Return CType(Me.Item("SpreadWidth"), List(Of Single))

End Get
Set(ByVal value As List(Of Single))
Me.Item("SpreadWidth") = value
End Set
End Property
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top