Passing values between forms

L

lextendo

Hi all,

I have a problem which seems it has to have a simple solution, though I
can't figure it out and I can't find a solution on usenet either.

Two forms: Form A contains a textbox where the user can enter a date.
When doubleclicking the textbox, a form B wil open with a
CalenderControl to pick a date.

Now I want to copy this date -selected in form B- in the textbox of
form A.
I specifically want the calander control on a seperate form, because I
want to use the "datepicker" throughout the application having the same
(customized) look without customizing it on every form (making it
'popup' by hiding/showing it).

I've seen quite a few messages and websites which refer to delegates to
solve this particular problem. But the "delegate-concept" is somewhat
vague to me. Example code would be much appreciated...

TIA,
Alex
 
N

Nicholas Paldino [.NET/C# MVP]

Alex,

For the constructor for Form B, take a parameter which is of the type of
Form A. Store that in your class.

On Form A, expose a method which will take the value to update the
textbox on Form A with. Of course, make the method actually set the text.

Then, in Form B, call the method on Form A to update the textbox
appropriately.

Hope this helps.
 
M

Marc Gravell

Well, I'm going to assume you are showing B with ShowDialog; if this is the
case, then you can simply expose a property on B to get at the selected date
(as so):

public class B : Form {
// blah
public DateTime SelectedDate {
get {return calendarControl.Value;} // or whatever the property is
}
}

Then A can get this by:

using(B b = new B()) {
if(b.ShowDialog() == DialogResult.Ok) {
DateTime theDate = b.SelectedDate;
// do something interesting
}
}

If you want to use the same instance (Show() instead of ShowDialog()), then
it is trickier for B to notify A. The default here would be a simple event
that A subscribes to, but how do we know when a user has given up and gone
somewhere else? (i.e. we should actually be notifying a different form). A
hack would be:

public class B : Form {
// blah
private EventHandler _dateSelected;
public event EventHandler DateSelected {
add {_dateSelected = value;} // hack; would normally be +=
remove {_dateSelected -= value;}
}
protected void OnDateSelected() {
EventHandler handler = _dateSelected;
if(handler!=null) handler(this, EventArgs.Empty);
}
}

Then A can do:
sharedB.DateSelected += somehandler;
sharedB.Show();
// "somehandler" uses sharedB.SelectedDate from the above to get the value

and we know that only the *last* subscriber to sharedB will be notified. I
don't like it though; IMO each should have their own instance using
ShowDialog().

0.02 over...

Marc
 
B

Bruce Wood

Ewww... Nicholas, that's the nasty way to do it!

Marc gave the better answer, IMO.
 
N

Nicholas Paldino [.NET/C# MVP]

Bruce,

I agree. Whether or not I did this in this particular way depends on
how tightly coupled the forms were. As a general piece of code, Marc's is
definitely the better way.

The one thing I will say is this, though. People seem to lose track
that forms are nothing more than objects, and you can do all the nifty
things with them that you can do with objects, such as passing them around,
storing references to them, etc, etc.
 
L

lextendo

Marc said:
using(B b = new B()) {
if(b.ShowDialog() == DialogResult.Ok) {
DateTime theDate = b.SelectedDate;
// do something interesting
}
}

Mark,
Thank you for your quick response. It did the trick nicely! I got
stuck on this for quite some time. You helped me a great deal :)

Btw thanx to ALL who responded. I appreciate it.

Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top