473,409 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

Properly sharing data between forms

I have program.cs, my "main" form and then a "settings" form.

My "main" form existed for awhile and I had constants, instantiations,
properties, etc within it created.

I went to create my "settings" form and constantly found myself
adjusting my "main" form entities to be static and inevitably just
passing in the "main" forms 'this' instance to the "settings"
constructor to simplify it all.

My question is, what is the proper way to be setting this all up. Is
there a way to avoid all of this, and give all forms implicit access
to this written common code in the "main" form? I have many forms to
come later and I don't want to have to tailor each to have a property
or constructor to accept one or more form instances holding
information they may need.
Jun 27 '08 #1
9 1974
On Sat, 21 Jun 2008 09:27:23 -0700, kirk <ki****@gmail.comwrote:
I have program.cs, my "main" form and then a "settings" form.

My "main" form existed for awhile and I had constants, instantiations,
properties, etc within it created.

I went to create my "settings" form and constantly found myself
adjusting my "main" form entities to be static and inevitably just
passing in the "main" forms 'this' instance to the "settings"
constructor to simplify it all.

My question is, what is the proper way to be setting this all up. Is
there a way to avoid all of this, and give all forms implicit access
to this written common code in the "main" form? I have many forms to
come later and I don't want to have to tailor each to have a property
or constructor to accept one or more form instances holding
information they may need.
I'm not sure I understand the question. For "settings" that aren't
actually specific to a form, they shouldn't be in a form in the first
place. Most likely, they'd be found in the project's Settings class, but
if not then at least some other static class that would be exposed to any
other code in the project. For "settings" that are specific to a form
instance, then obviously you would naturally have to pass a reference to
an instance of that form to control the settings for that instance.

Can you provide an example where you've got a setting that is not specific
to an instance of a form, but you feel that a correct design is to pass an
instance of a form anyway?

Pete
Jun 27 '08 #2
KS
"kirk" <ki****@gmail.comskrev i meddelelsen
news:bf**********************************@x1g2000p rh.googlegroups.com...
>I have program.cs, my "main" form and then a "settings" form.

My "main" form existed for awhile and I had constants, instantiations,
properties, etc within it created.

I went to create my "settings" form and constantly found myself
adjusting my "main" form entities to be static and inevitably just
passing in the "main" forms 'this' instance to the "settings"
constructor to simplify it all.

My question is, what is the proper way to be setting this all up. Is
there a way to avoid all of this, and give all forms implicit access
to this written common code in the "main" form? I have many forms to
come later and I don't want to have to tailor each to have a property
or constructor to accept one or more form instances holding
information they may need.
If I understand you right.

Just make an object instance with all you setting values and "store" it in
Properties.Settings.Default.

If you want to use some of the "global" settings in another form - just
declare a new object and populate it with the settings from
Properties.Settings.Default.

This way you can "implement" the VB-like global variables.

Best regards
KSor, Denmark

Jun 27 '08 #3
On Jun 21, 10:12*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Sat, 21 Jun 2008 09:27:23 -0700, kirk <kir...@gmail.comwrote:
I have program.cs, my "main" form and then a "settings" form.
My "main" form existed for awhile and I had constants, instantiations,
properties, etc within it created.
I went to create my "settings" form and constantly found myself
adjusting my "main" form entities to be static and inevitably just
passing in the "main" forms 'this' instance to the "settings"
constructor to simplify it all.
My question is, what is the proper way to be setting this all up. *Is
there a way to avoid all of this, and give all forms implicit access
to this written common code in the "main" form? *I have many forms to
come later and I don't want to have to tailor each to have a property
or constructor to accept one or more form instances holding
information they may need.

I'm not sure I understand the question. *For "settings" that aren't *
actually specific to a form, they shouldn't be in a form in the first *
place. *Most likely, they'd be found in the project's Settings class, but *
if not then at least some other static class that would be exposed to any*
other code in the project. *For "settings" that are specific to a form *
instance, then obviously you would naturally have to pass a reference to *
an instance of that form to control the settings for that instance.

Can you provide an example where you've got a setting that is not specific *
to an instance of a form, but you feel that a correct design is to pass an *
instance of a form anyway?

Pete- Hide quoted text -

- Show quoted text -
Below is short mockup of what I am running into today. I an
envisioned an architectural change of putting all of the common code
in the main program.cs file, instantiating all of the forms from
program.cs(currently it only starts MainForm), and all of those forms
would have access to common code to play with and share.

MainForm
{
// onload pulls data from registry to work with locally only
// instantiates a network class to watch for network disconnect/
connect
// event handler to network class to watch for disconnect/connect
// method here to sync local registry data back to registry as
needed

// button here to instantiate SettingsForm
}
SettingsForm
{
// contains controls to be populated based on pulled down registry
data in MainForm
// onload gets preserved data from MainForm local registry data
// clicking "ok"/"apply" updates MainForm local registry data
// clicking "ok"/"apply" calls MainForm method to sync local
registry data back to registry

}

FutureForm
{
// will run concurrent with MainForm later
// subscribe to same event handler to network class in MainForm
// do individual work pertinent to FutureForm on network disconnect/
connect
// call method in MainForm to sync local registry data
}
Jun 27 '08 #4
On Sat, 21 Jun 2008 11:07:27 -0700, kirk <ki****@gmail.comwrote:
Below is short mockup of what I am running into today. I an
envisioned an architectural change of putting all of the common code
in the main program.cs file, instantiating all of the forms from
program.cs(currently it only starts MainForm), and all of those forms
would have access to common code to play with and share.
There's nothing about the "short mockup" you posted that explains why
"MainForm" is managing the "data from registry".

In a .NET application, the kinds of things that normally would have been
stored in the registry are instead maintained using the Designer-provided
"Settings" class. But whether you use the registry or Settings, there's
nothing that would normally compel you to put the management of that data
in a particular form.

It seems to me that you need a shared class, probably static but maybe
singleton instead, where the settings are stored. Normally, this would
just be the "Settings" class, but if you really want to use the registry,
you could write a similar class yourself that uses the registry instead of
the normal .NET settings management techniques. Either way, all of that
code would exist outside of any of the forms. The form classes would
access the settings through the settings class, rather than maintain the
settings themselves.

Pete
Jun 27 '08 #5
On Jun 21, 4:24*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Sat, 21 Jun 2008 11:07:27 -0700, kirk <kir...@gmail.comwrote:
Below is short mockup of what I am running into today. *I an
envisioned an architectural change of putting all of the common code
in the main program.cs file, instantiating all of the forms from
program.cs(currently it only starts MainForm), and all of those forms
would have access to common code to play with and share.

There's nothing about the "short mockup" you posted that explains why *
"MainForm" is managing the "data from registry".

In a .NET application, the kinds of things that normally would have been *
stored in the registry are instead maintained using the Designer-provided*
"Settings" class. *But whether you use the registry or Settings, there's *
nothing that would normally compel you to put the management of that data*
in a particular form.

It seems to me that you need a shared class, probably static but maybe *
singleton instead, where the settings are stored. *Normally, this would*
just be the "Settings" class, but if you really want to use the registry,*
you could write a similar class yourself that uses the registry instead of *
the normal .NET settings management techniques. *Either way, all of that *
code would exist outside of any of the forms. *The form classes would *
access the settings through the settings class, rather than maintain the *
settings themselves.

Pete
Thank you for the details on the "Settings" class. Pretty fancy. I
have tried to keep my app .NET 1.1 compliant, hence my old-school
approach on using the registry for data preservation. I'm curious,
could that class also store and globalize unique types that i've
developed and use the services of the types(methods, event sinks,
etc)?

There was no reason in particular that MainForm is serving as a
centralized point for everything, i.e. registry management, object
instances to tap into. This is my first Forms-based application, and
I lack intelligence in the Forms world.

It sounds like all of the back pedaling i'm doing, making what is in
MainForm, static to give it global visibility to the other forms is
really what you are suggesting as one option. Just move it over to a
separate shared class.
Jun 27 '08 #6
On Sat, 21 Jun 2008 17:30:14 -0700, kirk <ki****@gmail.comwrote:
Thank you for the details on the "Settings" class. Pretty fancy. I
have tried to keep my app .NET 1.1 compliant, hence my old-school
approach on using the registry for data preservation. I'm curious,
could that class also store and globalize unique types that i've
developed and use the services of the types(methods, event sinks,
etc)?
It should. I admit that my experience doing that is non-existent, but my
understanding is that any type that is serializable can be included in the
Settings class, including your own. You would need to make those types
serializable, of course. :)
[...]
It sounds like all of the back pedaling i'm doing, making what is in
MainForm, static to give it global visibility to the other forms is
really what you are suggesting as one option. Just move it over to a
separate shared class.
If I've understood the question correctly, yes. I agree.

Pete
Jun 27 '08 #7
On Jun 21, 5:39*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Sat, 21 Jun 2008 17:30:14 -0700, kirk <kir...@gmail.comwrote:
Thank you for the details on the "Settings" class. *Pretty fancy. *I
have tried to keep my app .NET 1.1 compliant, hence my old-school
approach on using the registry for data preservation. *I'm curious,
could that class also store and globalize unique types that i've
developed and use the services of the types(methods, event sinks,
etc)?

It should. *I admit that my experience doing that is non-existent, but my *
understanding is that any type that is serializable can be included in the *
Settings class, including your own. *You would need to make those types*
serializable, of course. *:)
[...]
It sounds like all of the back pedaling i'm doing, making what is in
MainForm, static to give it global visibility to the other forms is
really what you are suggesting as one option. *Just move it over to a
separate shared class.

If I've understood the question correctly, yes. *I agree.

Pete
Hmmm.

I tried making a custom type serializable and it does not show up in
the list of "types" in the Settings dialog, so i'm guess that means
unsupported?

I think i'm going to stick with my 1.1 compliance effort, dump all the
common code to a static shared class and call this a wrap. Thank you
for the help and your patience!
Jun 27 '08 #8
On Sat, 21 Jun 2008 19:09:19 -0700, kirk <ki****@gmail.comwrote:
I tried making a custom type serializable and it does not show up in
the list of "types" in the Settings dialog, so i'm guess that means
unsupported?
I'm not sure. You wrote later something about "1.1 compliance effort".
Are you trying to get your types into the Settings class in a 1.1
application? If so, maybe there is indeed some version issue. Otherwise,
I'm reasonable confident that you should be able to get it to work. I
haven't done it myself, but very reliable people have told me it would.
I think i'm going to stick with my 1.1 compliance effort, dump all the
common code to a static shared class and call this a wrap. Thank you
for the help and your patience!
You're welcome. I think your proposed solution should work just as well
as getting the Designer's Settings class to work. In fact, some times it
makes more sense to just stick with what's already working. :)

Pete
Jun 27 '08 #9
I tried making a custom type serializable and it does not show up in
the list of "types" in the Settings dialog, so i'm guess that means
unsupported?
I had this problem too, but as the Settings class is a partial class I just
extended it by adding another file with code similar to the following:

namespace MyProgram.Properties {
internal sealed partial class Settings :
global::System.Configuration.ApplicationSettingsBa se
{
[global::System.Configuration.UserScopedSettingAttr ibute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttr ibute()]
public MyClass MySetting {
get {
return (MyClass)this["MySetting"];
}
set {
this["MySetting"] = value;
}
}
}
}

Chris Jobson

Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Paul Schouten | last post by:
I was wondering if anyone could help me with this... I am having problems sharing data between child forms. the setup is as follows: mainFrm <--- MDIForm |----> ViewFrm <----ChildForm |---->...
6
by: varlagas | last post by:
We disabled the antivirus software but the problem persists. Any clues? Many thanks in advance! Panagiotis Varlagas ======================================================================= ...
2
by: Red Green | last post by:
I am trying to switch to C# from Delphi and I am having some trouble finding some information. What I need to do should be simple but I have been through a dozen books and done web searches that...
3
by: Robert W. | last post by:
I'm embarking on a project that will have both a desktop application and a Pocket PC application. It seems logical to have as much code as possible sitting in a shared project, which would be...
2
by: Mervin Williams | last post by:
I am using Infragistics UltraWebTab (a tab folder control for ASP.NET). My tab folder control will include five tab pages with a separate web form on each, and these web forms will share data. ...
1
by: Tod Birdsall, MCSD for .NET | last post by:
Hi All, I have two ASP.NET applications which I am trying to have share forms authentication. But I am running into problems. App A is an ASP.NET 2.0 Beta 2 application. App B is an ASP.NET...
3
by: Chris Thunell | last post by:
I have 2 forms, on 1 form i use the wizards to create a strong typed dataset with tables from an SQL database... and from that i can do stuff like: me.daEmployee.fill(me.dataset11.tblEmployee) ...
11
by: Nick | last post by:
Hello, Please pardon my ignorance as this is something I should know, but I'm a little unclear. I have an MDI based app. It is setup so the user opens a file and the main child form is created....
1
by: Bob Alston | last post by:
Having problems sharing an Access database on a Windows 2000 PC. The other PC is windows 98 (as I recall) but NOT windows 2000! The 2nd PC has a shortcut to the MDB on the primary PC. The MDB...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.