NOOB Alert: Windows Forms in c#

  • Thread starter Thread starter rs.franken
  • Start date Start date
R

rs.franken

Hi all,

I am writing a little application. The main form presents a main menu
and within this menu there is an "options" part. This option will show
a new form, where some application settings can be made. When the user
presses on the OK button, I need the changed information in my main
form, so the code can act upon the (possible) changes made.

So what I do is: I instantiate the form, I show the form, data gets
changed, but how do I get the data back? Both forms are totally
different. I know how to pass data TO the form, but I need data FROM
the form. The only solution I know off, is to create a class for my
"set of options" and pass that object to the second form. I think there
is an easier way...

can anybody help me out here?

TIA

Richard
 
Hi all,

I am writing a little application. The main form presents a main menu
and within this menu there is an "options" part. This option will show
a new form, where some application settings can be made. When the user
presses on the OK button, I need the changed information in my main
form, so the code can act upon the (possible) changes made.

So what I do is: I instantiate the form, I show the form, data gets
changed, but how do I get the data back? Both forms are totally
different. I know how to pass data TO the form, but I need data FROM
the form. The only solution I know off, is to create a class for my
"set of options" and pass that object to the second form. I think there
is an easier way...

can anybody help me out here?

TIA

Richard
There are a number of ways you could do this.
You could put a public property on the form and set >> show >> get this
property.

I normally do it like this.
1. Overload ShowDialog and pass in a ref to your object.
2. On dialogresult.ok (in your form), update the object and all is good.

Cheers
JB
 
Well, I found a way to do it, that I think is nice and OO based...

The mainform will containt the code to act upon the settings made in
the options form. So, I have create a struct (rather then a class,
since it's (so far) only data and no methods) that contains all the
options settings.

So in mainform I have private variables that in the end will contain
the settings as thay are made in the struct.
I instantiate the struct in the mainform.
Then I launch the options form
Instantiate the struct here too.
Then when the form has the settings that the user needs, I pass them to
the struct.
Close the form
In the mainform I then feed my private variables with the data in the
struct.

Now all I need to do is write the code to actually act upon it (validty
checks are done in the form where the data is entered, so the data that
I will work with is ok).

Is this a good way to do it?

Regards,

Richard
 
Back
Top