473,785 Members | 2,307 Online
Bytes | Software Development & Data Engineering Community
+ 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 1543
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
5943
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 contains: <?php $_SESSION = ""; $_SESSION = "";
1
7789
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: ---------------------------------------------------------------------------- <?php ini_set("session.use_cookies", "off"); ini_set("session.use_trans_sid", "on"); session_start(); $_SESSION = ""; $_SESSION = ""; echo "<form method='POST' action='login.php'>
1
7436
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 which users enter numbers to be calculated into a square footage cost. Upon submitting, the results page uses ASP to give the total and the chance to add the new totalled item to the cart or try a new calculation. Part of the cart's software has a...
3
5562
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 an array-variable and pass this variable to a subroutine in which I make all stored objects in the array-variable invisible. Can somebody explain me how to declare the correct variables, how to pass these to a sub routine and how to make these...
11
8131
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 number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub FirstDouble(ByVal array As Integer()) Dim i As Integer
13
2522
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 Error GoTo err_zoom strFormName = formName
5
7977
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 calendar should write to. I do this so I can use the same calendar form throughout my database. Does anyone know how to pass this info back to the form that called it? I'm trying to set the value of the control on a form using a Submit button...
6
2198
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 some scripts that run at the server for the form so I can't disable that statement. I followed Microsoft's instructions(I know..not the best thing to do) but still can't get the variables to pass. I created a class and set up an @reference...
1
2857
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 issue is that all of my dropdown menus are passing the last row to the next page as the session variable. The pages that I am talking about can be found at: Weekly13_Test The code that follows was operational until the change. Here is my...
0
10357
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10162
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6744
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
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 we have to send another system
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.