Multiple Forms

  • Thread starter Thread starter Matthew Morvant
  • Start date Start date
M

Matthew Morvant

I have a C# application that starts off at a main form. On this form are
several private variables with public properties (get/set). One task
envolves a second form poping up and the data from that form is written out
to one of the public properties on the main form. When a third form is
popped up the data that the second form set is no longer set. Is there
something wrong with my thinking? Anyone have any recomendations for a
better method? I just need variables that I can access/set from a number of
different locations. This is my first major (GUI) application in C#, so be
gentle.

Matthew
 
Hello,

Would you be binding the properties in the main form to some data in the
other forms. If so, when that third form pops up, there might not be data
and the binding would set the main form properties to nothing. Other than
that I would need more info on how you are coding this.

Possible Solutions:

1) You could create Global properties in a seaparate class (ie
static/shared) then have all the forms get and set those. The first
consideration here is that if you have multiple threads that can access
these variables you will need to make them thread safe. The other thing is
if you want to have your forms update as the values change on these
properties you will need to setup an event to tell you when the value has
changed. So in the set you will set the value of the property and call
PropertyNameChanged event which you created. You could make it more generic
and call it DataChanged and have all your properties call a single event so
that your form only needs to listen to the one.

2) Add and event to the Second and Third form that you call when you want
data changed on the main form. Have the main form listen to the events on
the second and third form. You will probably want to create you own
EventArgs class that contains the data you need to pass back to the main
form.

3) The way you were doing it would seem fine. Main form opens Second form.
Second form calls public set properties on the Main form. Main form open
Third form. Third form call properties on Main form. If it is not the
binding issue I stated earlier, I will need more details to help.

Hope that helps,

Tom

--
Tom Krueger

My Blog - http://weblogs.asp.net/tom_krueger
Smart Client DevCenter - http://msdn.microsoft.com/smartclient/
Mobile DevCenter - http://msdn.microsoft.com/mobility

This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top