473,386 Members | 1,699 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,386 software developers and data experts.

passing variable from one form to another,in form2,form1 variable are initialized

Expand|Select|Wrap|Line Numbers
  1. Form1--
  2. public string val="Raj";
  3. form2 obj=new form2()
  4. obj.show();
  5.  
  6. Form2--
  7. method()
  8. {
  9.   form1obj=new form1()
  10.   textbox1.text=obj.val;
  11. }
  12.  
Jul 31 '10 #1

✓ answered by Aimee Bailey

In visual basic they have the ability to create modules where you could store all of your variables sharable between all forms, however in c# we have to be a tiny bit more clever, add a new class to your project and edit it as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. public static class MyVariables
  3. {
  4.   public static string val = "Raj";
  5. }
  6.  
  7.  
Now to access the variable, all you need to do is this:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  //Form2--
  3.  
  4.  void method()
  5.  {
  6.    textbox1.text=MyVariables.val;
  7.  }
  8.  
  9.  
happy days!

3 1710
Aimee Bailey
197 Expert 100+
In visual basic they have the ability to create modules where you could store all of your variables sharable between all forms, however in c# we have to be a tiny bit more clever, add a new class to your project and edit it as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. public static class MyVariables
  3. {
  4.   public static string val = "Raj";
  5. }
  6.  
  7.  
Now to access the variable, all you need to do is this:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  //Form2--
  3.  
  4.  void method()
  5.  {
  6.    textbox1.text=MyVariables.val;
  7.  }
  8.  
  9.  
happy days!
Jul 31 '10 #2
@AmzBee
Thanks AmzBee
Aug 2 '10 #3
MMcCarthy
14,534 Expert Mod 8TB
If you feel a post has answered your question, please remember to mark it as Best Answer.

Mary
Aug 4 '10 #4

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

Similar topics

2
by: HelLind | last post by:
Hello everyone ! Is there any way to pass session variables between two websites ( diff domains) ? I've two websites and I want to integrate them. I used to use the querystring but there is...
3
by: Peter | last post by:
Hello, Two newbie questions: 1) I have a javascript file with a function in it. From this function I want to access a variable in another javascript file -which is not inside a function. I...
3
by: Martin | last post by:
I have a calendar form that sends the inputted date back to a text box . Using the GET method, I would like to pass the date as a variable to another asp page. How do I do this? Thanks.
6
by: leppert | last post by:
Hello, I am trying to access a javascript variable on another site. The site requires the user to be logged in, so what I am doing is submitting a form and redirecting the output to an IFRAME,...
2
by: garyusenet | last post by:
I have a windows form project which originally contained one form, the standard form. Form1. I've added another form now, form2 which i'm experimenting with. I'd like to display form2 when i...
2
by: Harmony504 | last post by:
I am passing a form field in JavaScript to a PHP function. The problem is when the parameter gets inside the PHP function the value is set to 1 when it should be a relative path to a file. ...
1
by: menyki | last post by:
How do i declare a variable in a form if i want to reference to the variable from another form. Below is a code on a form called updatemortalityForm, some variable on this form, i want to make use of...
2
by: fusionfusion | last post by:
Hi I want to avoid hardcoding the filepath in my C program. I want to store the filepath in a variable in a text file. then i have to pass that variable to another file. ex....
1
by: arsha123 | last post by:
How can I change the value of empty session variable with another variable value defined in form. I get value of session value of id from a logon form , that is "". I have defined a variable in form...
1
by: blknmld69 | last post by:
I keep getting the error: Circle.java:18: variable radius might not have been initialized import java.util.Scanner; import java.io.*; public class Circle { public static...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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,...
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...

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.