473,399 Members | 4,177 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,399 software developers and data experts.

Passing values between forms

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

May 31 '06 #1
5 1410
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<le******@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
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

May 31 '06 #2
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
May 31 '06 #3
Ewww... Nicholas, that's the nasty way to do it!

Marc gave the better answer, IMO.

May 31 '06 #4
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Ewww... Nicholas, that's the nasty way to do it!

Marc gave the better answer, IMO.

May 31 '06 #5

Marc Gravell wrote:
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

May 31 '06 #6

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

Similar topics

8
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
0
by: Neelima Godugu | last post by:
Hi All, I have developed a windows forms user control, which I am going to host in Internet Explorer.. I am familiar with the security settings requirement inorder to do the above. I have...
0
by: Neelima Godugu | last post by:
Hi All, I have developed a windows forms user control, which I am going to host in Internet Explorer.. I am familiar with the security settings requirement inorder to do the above. I have...
2
by: o1j2m3 | last post by:
I created 2 forms, A and B using VB.NET 2003 in A, the codes are : ========================== public AsName as string public AsPrice as double public sub setValue(ByVal sname as string,...
13
by: Deano | last post by:
Apparently you can only do this with one value i.e Call MyAssetLocationZoom(Me!txtLocation, "Amend data") This runs; Public Sub MyAssetLocationZoom(ctl As Control, formName As String) On...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.