Creating new settings on the fly

  • Thread starter Thread starter lord.zoltar
  • Start date Start date
L

lord.zoltar

I have a problem which I think can be solved by creating new Settings
on the fly, but I'mnot sure if it's possible, or if a better solution
may exists.
Here's the situation: I have a project with many different
dataGridViews to display data, and many of them have 10-20 columns
(the actual number of displayed columns is user/client controlled, and
in a couple cases could be ~30). The client would like to be able to
resize the columns manually. This is not a problem, I set the
AllowUserResizeColumn property of the grids to True. The problem is
that the columns don't remember their width when the application is
shut down and restarted. I'm not sure the best way to solve this:

My first thought was to create a setting in the My.Settings for each
column. The problem is, there are many MANY columns, and there is the
potential that new columns will be created by the user at run-time.
I'd like to create these settings automatically (at run time: check if
there is an existing setting and create it if not), but I don't know
if it's possible to do this.
My second thought was to create a special "column width manager"
object, and serialize it to the disc and then read it when the program
loads.
I also thought about storing all this column-width data in the
database, but the program is not multiuser and all users would share
the column widths (not an acceptable solution).

Has anyone else here ever had a similar problem? Or can anyone give a
recomendation?
 
Well, you *could* define a table just for this purpose in the database and
add userid to the primary key, but that kind of seems like overkill.

I'd probably use the settings. I'm interested to see how you solve this,
because I also want to save the column widths of my grid columns.

Do you know how to create your own area in the settings and read/write to
it, so it has a key you can recognize (like CustomerGridColWidths)? I have
a code sample if you want one.

Robin S.
 
Well, you *could* define a table just for this purpose in the database and
add userid to the primary key, but that kind of seems like overkill.

Definitely... Especially since the people using this app like to all
log into all the target computers with the same username/account name.
And the app is not (yet) multiuser.
I'd probably use the settings. I'm interested to see how you solve this,
because I also want to save the column widths of my grid columns.

I'll let you know when I've got it working.
Do you know how to create your own area in the settings and read/write to
it, so it has a key you can recognize (like CustomerGridColWidths)? I have
a code sample if you want one.
Do you mean creating new Settings, such as creating
My.Settings.CustomerGridColWidths.ID on the fly? YES! this is
something I've been stuck on. Do you know how this can be done? I
don't need code (unless it's very short), just a description.
 
Definitely... Especially since the people using this app like to all
log into all the target computers with the same username/account name.
And the app is not (yet) multiuser.


I'll let you know when I've got it working.

Do you mean creating new Settings, such as creating
My.Settings.CustomerGridColWidths.ID on the fly? YES! this is
something I've been stuck on. Do you know how this can be done? I
don't need code (unless it's very short), just a description.

I do know how to do it, and will post a short code sample tomorrow. Sorry,
I'm on my way out the door...

Robin S.
 
I was thinking of another way to do: just have one big string called
"AllColWidths" formatted "col1:width1,col2:width2,..." But then for
every column I'd have to read the whole string and find the width I
need. I just don't find this solution very satisfying.
 
I was thinking of another way to do: just have one big string called
"AllColWidths" formatted "col1:width1,col2:width2,..." But then for
every column I'd have to read the whole string and find the width I
need. I just don't find this solution very satisfying.

Well, it *is* an idea. Here's another one.

This is an example of saving and restoring settings that have their own
"section".

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 under the Form Name as
the setting key. I also use a similar structure in a couple of forms to
keep the entries the user has made in the screen so I can repopulate them
when he comes back at another time.

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
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 key, i.e. 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

Unfortunately, you actually *do* have to have the settings in MySettings in
the Project area. I thought I had figured a way around that, but apparently
not.

I did find this article on MSDN about creating new application settings,
but it's not going to make you happy. It requires you to set up a property
to do it. So unless you can do that programmatically, you seem to be s.o.l.

http://msdn2.microsoft.com/en-us/library/ms171565.aspx

Here's a cheeky idea: You could make a setting called "ColumnWidth", then
set the column name to the key name. This would give you a separate area in
your user.config file for each column, but who cares? It would work. I'd
probably try that out of desperation, and if it didn't work, create a
dictionary (fieldname, column width) and save it in XML by itself.

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.
 
Well, it *is* an idea. Here's another one.

This is an example of saving and restoring settings that have their own
"section".

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 under the Form Name as
the setting key. I also use a similar structure in a couple of forms to
keep the entries the user has made in the screen so I can repopulate them
when he comes back at another time.

[SNIP!]


Unfortunately, you actually *do* have to have the settings in MySettings in
the Project area. I thought I had figured a way around that, but apparently
not.

I did find this article on MSDN about creating new application settings,
but it's not going to make you happy. It requires you to set up a property
to do it. So unless you can do that programmatically, you seem to be s.o.l.

http://msdn2.microsoft.com/en-us/library/ms171565.aspx

The article looked good, but you're right, I'm not sure how to add new
properties programmatically... It might be possible with the
PropertyBuilder class:
http://msdn2.microsoft.com/en-us/library/system.reflection.emit.propertybuilder.aspx
I have yet to try it out but I really want to!

Here's a cheeky idea: You could make a setting called "ColumnWidth", then
set the column name to the key name. This would give you a separate area in
your user.config file for each column, but who cares? It would work. I'd
probably try that out of desperation, and if it didn't work, create a
dictionary (fieldname, column width) and save it in XML by itself.

I just might end up doing that (saving widths to a file).
I probably won't have anymore time this week to really get into it,
unfortunately, but if/when I finish it, I'll post the code here. My
goal is to create a new dataGridView class that inherits the existing
one, but with the ability to remember the width of any column.
 
Well, it *is* an idea. Here's another one.

This is an example of saving and restoring settings that have their own
"section".

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 under the Form Name
as
the setting key. I also use a similar structure in a couple of forms to
keep the entries the user has made in the screen so I can repopulate
them
when he comes back at another time.

[SNIP!]


Unfortunately, you actually *do* have to have the settings in MySettings
in
the Project area. I thought I had figured a way around that, but
apparently
not.

I did find this article on MSDN about creating new application settings,
but it's not going to make you happy. It requires you to set up a
property
to do it. So unless you can do that programmatically, you seem to be
s.o.l.

http://msdn2.microsoft.com/en-us/library/ms171565.aspx

The article looked good, but you're right, I'm not sure how to add new
properties programmatically... It might be possible with the
PropertyBuilder class:
http://msdn2.microsoft.com/en-us/library/system.reflection.emit.propertybuilder.aspx
I have yet to try it out but I really want to!

Here's a cheeky idea: You could make a setting called "ColumnWidth",
then
set the column name to the key name. This would give you a separate area
in
your user.config file for each column, but who cares? It would work. I'd
probably try that out of desperation, and if it didn't work, create a
dictionary (fieldname, column width) and save it in XML by itself.

I just might end up doing that (saving widths to a file).
I probably won't have anymore time this week to really get into it,
unfortunately, but if/when I finish it, I'll post the code here. My
goal is to create a new dataGridView class that inherits the existing
one, but with the ability to remember the width of any column.

-----------

Cool. I'll look forward to seeing what you come up with!

Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
 
Back
Top