Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to pass a string value(that getting value from database) from one form to another form..
Posted

You have pretty much answered this yourself:
Set up a property in the destination which accepts the string.
Then hand it over from the source to the destination instance.
public class MyForm
   {
   public string ValueFromDataBase { get; set; };
   }

   ...

   MyForm myNewForm = new MyForm();
   myNewForm.ValueFromDataBase = "hello there";
   myNewForm.Show();


What is giving you difficulties?


"i will close form1 but when i access it on another myNewForm it find null?"


Yes: they are separate instances, just as two different strings are:
string first = "First string";
string second = "Second String";
You would not expect them to contain the same text, would you?
MyForm myNewForm = new MyForm();
myNewForm.ValueFromDataBase = "hello there";
myNewForm.Show();
MyForm myNewForm2 = new MyForm();
myNewForm2.ValueFromDataBase = "hello there";
myNewForm2.Show();
Equally you would expect the two instances of the form to have separate ValueFromDataBase values!

If all instances of a form (or any other class) must access the same string (i.e. they will all use the same database and you want to pass the connection string to them all) then you can declare the property as static:
C#
public class MyForm
   {
   public static string ValueFromDataBase { get; set; };
   }
And then you only set it once for all instances of MyForm. Be carefull though: only use this when the value will allways be the same for all instances - any even then, think about why you need that. Overuse of static variable is a bad idea - it makes things difficult to change.
 
Share this answer
 
v2
Comments
vikki0286 15-Feb-11 7:29am    
i will close form1 but when i access it on another myNewForm it find null?
OriginalGriff 15-Feb-11 7:43am    
Answer updated
Sergey Alexandrovich Kryukov 15-Feb-11 17:48pm    
It's good, my 5, but, as always, the best method in my opinion is missing; please see my answer.
--SA
Manas Bhardwaj 15-Feb-11 7:45am    
+5
There are a number of ways to accomplish this:

0) Pass a refeerence to a variable from form 1 to form 2 via form 2's constructor

1) Use a static global class to hold variables that can be accessed from anywhere in your app

2) Make the data available through a public data member in form2 that form1 can access after form2 is closed

3) Have form2 post an event with the desired data as part of the EventArgs-derived parameter, and have form1 subsribe to theat event

4) Have form1 request the current value of the data via na event that form2 is subscribed to, and have form2 respond with an event of its own that contains the value of the data.

Each method has pros/cons, so pick your poison, and go forth and code.
 
Share this answer
 
v2
Comments
Manas Bhardwaj 15-Feb-11 7:45am    
vry nicely explained. +5
Sergey Alexandrovich Kryukov 15-Feb-11 17:48pm    
It's good, my 5, but, as always, the best method in my opinion is missing; please see my answer.
--SA
Nobody advices another way which I thing is the best; I want to add it:

1) Created the interface with properties and methods you need; make the interface.

2) Make a separate file representing the form you need. Important: you do not to reproduce base class of this form (so this unit does not even have to use System.Windows.Forms), instead, add the interface as a base:

C#
//Interface declaration
IMyInterace {
   string MyProperty { get; set; }
   //...
}

//In one file
public partial class MyForm : Form { }

//In your new file:
public partial class MyForm : IMyInterace {
    string IMyInterface.MyProperty { //don't make it public!
        get {/*...*/}
        set {/*...*/} }
    }
    //...


3) Make IMyInterface visible to your other form,
add a write property of the type IMyInterface.

4) When MyForm is created pass it to another form as an instance of the interface IMyInterface assigning to the property mentioned in item (3).

In this way, you have a way to make one form known to another form only through interface, so it would not be able to directly access the other form.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900