473,486 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Passing a variable from one form to the next

97 New Member
I'd like to know how I go about passing two variables from one form to the next.
For example, I can pass a variable like this:
This demonstrates how to pass a variable to a form when the form is opened.
Expand|Select|Wrap|Line Numbers
  1. private void OpenForm_Click(object sender, System.EventArgs e)
  2. {
  3. // Get the value to be passed
  4. string parentValue = this.UserResponse.Text;
  5.  
  6. // Create the child form and pass the value in the constructor
  7. this.ChildForm = new ChildForm(parentValue);
  8.  
  9. // Show the form which will display the user's value.
  10. this.ChildForm.Show();
  11. }
  12.  
Then in the new form:
Expand|Select|Wrap|Line Numbers
  1. public string ValueFromParent
  2. {
  3.                 set
  4.     {
  5.                            this.ParentValue.Text = value;
  6.     }
  7. }
  8.  
Since i want to pass two variables from two dateTime pickers I would like something like this:
Expand|Select|Wrap|Line Numbers
  1. private void OpenForm_Click(object sender, System.EventArgs e)
  2. {
  3. // Get the value to be passed
  4. string parentValue = this.dateTimePicker1.Text;
  5. string parentValue2 = this.dateTimePicker2.Text;
  6.  
  7.  
  8. // Create the child form and pass the value in the constructor
  9. this.ChildForm = new ChildForm(parentValue,parentValue2);
  10.  
  11. // Show the form which will display the user's value.
  12. this.ChildForm.Show();
  13. }
  14.  
Where i declare two parent values but that does not work...any help please
Jul 23 '08 #1
6 1520
Plater
7,872 Recognized Expert Expert
Create a constructor for your ChildForm that takes two DateTime objects as arguments?
Jul 23 '08 #2
gggram2000
97 New Member
Create a constructor for your ChildForm that takes two DateTime objects as arguments?
I'm sorry but I don't know how to do that, is there anything I can reference caz i cant find any example
Jul 23 '08 #3
TRScheel
638 Recognized Expert Contributor
I'm sorry but I don't know how to do that, is there anything I can reference caz i cant find any example
Your child form should have something similiar to:

Expand|Select|Wrap|Line Numbers
  1. public ChildForm()         
  2. {
  3.              InitializeComponent();         
  4. }
or at least would have when you started. Change that to:

Expand|Select|Wrap|Line Numbers
  1. public ChildForm(DateTime value1, DateTime value2)         
  2. {
  3.              InitializeComponent();     
  4.              // do stuff here    
  5. }

EDIT:

In your example you grabbed the text so you would actually want:
Expand|Select|Wrap|Line Numbers
  1. public ChildForm(string value1, string value2)         
  2. {
  3.              InitializeComponent();     
  4.              // do stuff here    
  5. }
Jul 23 '08 #4
adamrlee
2 New Member
It is not good to have too many public vars.
Try making more getter/setter methods.


For example if you want to pass var 'x' from Form1 to Form2 simply call Form2.setMyX(x)

in Form2 Sub setMyX(byVal new_x)
x=new_x
End Sub
Jul 24 '08 #5
gggram2000
97 New Member
Your child form should have something similiar to:

Expand|Select|Wrap|Line Numbers
  1. public ChildForm()         
  2. {
  3.              InitializeComponent();         
  4. }
or at least would have when you started. Change that to:

Expand|Select|Wrap|Line Numbers
  1. public ChildForm(DateTime value1, DateTime value2)         
  2. {
  3.              InitializeComponent();     
  4.              // do stuff here    
  5. }

EDIT:

In your example you grabbed the text so you would actually want:
Expand|Select|Wrap|Line Numbers
  1. public ChildForm(string value1, string value2)         
  2. {
  3.              InitializeComponent();     
  4.              // do stuff here    
  5. }
Ok I'll try that and let u kno...thanks
Jul 24 '08 #6
gggram2000
97 New Member
Hey just wanted say it worked great, so simple yet easy to miss...thanks a bunch!
Jul 25 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
5922
by: Paul | last post by:
I want to use sessions to cover myself in case the user switches off cookies so I am passing the session ID manually through a hidden input field. This is what I have so far. index.php page...
1
7766
by: Paul | last post by:
Hmmm, didn't seem to work. I have set session.use_cookies = 1 and session.use_trans_sid = 1 in my php.ini file. Index.php contains:...
1
7419
by: Newbie | last post by:
OK, this may be impossible since I'm using 3rd party shopping cart ASP software, but I've been able to finagle a lot of other stuff I thought wouldn't work, so here we go: I'm using a form in...
3
5521
by: SV | last post by:
Dear all, In my application I have a lot of hidden fields. I want to make them invisible for the users though for debugging reasons I want to make them visible. So I want to add these objects to...
11
8089
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
13
2457
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...
5
7964
by: manningfan | last post by:
I have a single form that I want to make as generic as possible. The form contains a Calendar control. Using OpenArgs I pass values from the form that's calling the calendar, and the field the...
6
2162
BezerkRogue
by: BezerkRogue | last post by:
This is the most fundamental action I am sure, but I can't seem to make it happen. I am familiar with passing variables in ASP. But that doesn't seem to be the preferred method in .NET. I have...
1
2843
SHOverine
by: SHOverine | last post by:
Recently my web host decided to "upgrade". This change rendered many of my pages useless and I am scrambling to fix the issues, so you may see several posts from me in the coming days. My first...
0
7100
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
6964
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
7175
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
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4865
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...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.