views:

969

answers:

10

I have two forms, one is the main form and the other is an options form. So say for example that the user clicks on my menu on the main form: Tools -> Options, this would cause my options form to be shown.

My question is how can I send data from my options form back to my main form? I know I could use properties, but I have a lot of options and this seems like an tedious odd thing to do.

So what is the best way?

EDIT: These are some good answers, but I'm not a great programmer. Can you please provide some tutorials on how to do what your saying. Thanks!

A: 

i encapsulate the data in a class object

Martin Ongtangco
A: 

Properties is one option, shared static class - another option, events - another option...

Dani
Arguments to the constructor, method calls, ... :-)
Joey
MVP, MVC, MVVM...
Martinho Fernandes
+2  A: 

The best in this case would be to have some OptionsService class/interface that is accessible via IServiceProvider.

Just add an event when something changes, and the rest of the app can respond to it.

leppie
A: 

You might try AutoMapper. Keep your options in a separate class and then use AutoMapper to shuttle the data between the class and the form.

Andy S
A: 

Create a Class and put all your properties inside the class .. Create a Property in the parent class and set it from your child (options) form

Zuhaib
+1  A: 

You can have a function in Form B like so:

public SettingsResults GetNewSettings()
{
    if(this.ShowDialog() == DialogResult.Ok)
    {
         return new SettingsResult { ... };
    }
    else
    {
         return null;
    }
}

And you can call it like this:

...
using(var fb = new FormB())
{
    var s = fb.GetNewSettings();
    ...
    // Notify other parts of the application that settings have changed.
}
John Gietzen
A: 

This is probably sidestepping your problem a little bit, but my settings dialog uses the Application Settings construct. http://msdn.microsoft.com/en-us/library/k4s6c3a0.aspx

I can't find a good example that's similar to how I do it (which is actually having an actual class+object), but this covers another way of doing it:

http://stackoverflow.com/questions/49269/reading-default-application-settings-in-c

Oren Mazor
This is nice, but I'm not interested in storing the settings. I just want these settings to be relayed to the main form.
Bob Dylan
A: 

A form is a class, just like any other class. Add some public variables to your form class and set them when they click the button to close the form (technically they are just hiding it).

A VB.NET example, but you'll get the idea -

In your OptionsForm class:

Public Option1 as String = ""

etc. Set them when they hit the "Ok" button.

So in your main form, when they hit the "options" button - you create your options form:

OptionsForm.ShowDialog()

when it exits, you harvest your option settings from the public variables on the form:

option1 = OptionsForm.Option1

etc.

Ron

Ron Savage
+4  A: 
this. __curious_geek
Awesome answer! Thats exactly what I'm looking for! Thanks! Also I love that black them on XP (it almost makes me miss XP).
Bob Dylan
It's the Zune theme for XP. :)
this. __curious_geek
+1  A: 

MVC, MVP, MVVM -- slight overkill for someone admittedly saying they want tutorials. Those are theories that have entire courses dedicated to them.

As already posted, passing an object around is probably easiest. If treating a class as an object (interchangeable in this sense) is new, then you may want to spend another 2-4 weeks figuring out properties and constructors and such.

I'm not a C# master by any means, but these concepts need to be pretty concrete if you want to go much further beyond passing values between two forms (also classes/objects in their own right). Not trying to be mean here at all, it just sounds like you're moving from something like VB6 (or any language with globals) to something far more structured.

Eventually, it will click.

Sarkazein