Click here to Skip to main content
15,881,690 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Hello,
I've got the following code, which almost works, but not completely.
From form 1 I open form 2, which should sent the Density from form 1 to two. In form 2 it is possible to adjust the density which is again sent back to form 1.

I can set the density from form 2 to form 1, this works. But i can not read the value from form 1 to set the numeric updown to the current value. Please take a look :)

Form 1
C#
public decimal Density = 21;

        private void printerDensityToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DensitySettingForm dsf = new DensitySettingForm();
            dsf.Show();

        }



Form 2(where the info is set)

C#
SerialProgrammer SP = new SerialProgrammer();
private void BTN_Set_Click(object sender, EventArgs e)
{
    SP.Density = nudDensity.Value;
    this.Close();
}

private void DensitySettingForm_Load(object sender, EventArgs e)
{
    nudDensity.Value = SP.Density; // this does not work .. (t is automaticly set back to 21
}
Posted

you can write an overloaded constructor for form 2 that takes in that value from form 1 and then populates its local properties with those and then when you want to send that new value back to form 1, you can expose a public property in form 1 and set into it. This is one way of doing it, but obviously you are having to duplicate the same properties at 2 different places.

So to improve on this, create a separate class that will have all the common data properties wrapping static variables, that you can write to and read from. Both forms would then just read and write to that property. So:

C#
public class CommonData
{
   private static int _data
   public static int DataValue
   {
      get {return _data;}
      set {_data = value;}
   }
}

Form 1:
C#
CommonData.DataValue = 2;

Form 2:
//at this point the DataValue will contain 2
C#
CommonData.DataValue = 5;

Back into form1:
//this will now be updated to 5
C#
int x = CommonData.DataValue


Let me know if this helps. I am sure this can be tweaked even better.
 
Share this answer
 
Comments
pieterjann 2-Oct-12 10:27am    
Thank you for your answer, I get what you are doing, and I'm sure this will work. But I am not yet satisfied. As you are saying this can be tweaked better. Could you take a look at my next question: http://www.codeproject.com/Questions/469358/Passing-data-between-forms-why-so-complicated This might be interesting too :)
I.explore.code 2-Oct-12 11:07am    
the reason i said that it can be tweaked further was that if you had more data that needed to be shared you can club together such data in a shared data class. This way no form needs to know that the other forms exist.
Create a property in Form2 called Density, and set it from Form1 when you create the Form2 instance, and before you call ShowDialog.
After ShowDialog returns (i.e. when the user closes Form2) read the new value back.

This way, Form2 does not need to know that Form1 even exists, and Form1 just provides Form2 with the info it needs.

If you are using Show instead of ShowDialog, then create an event in Form2 which says "Value changed" and add a handler in Form1 when you create the instance. Form1 then fetches the value via the Form2 instance property in the handler.
 
Share this answer
 
Comments
pieterjann 2-Oct-12 10:25am    
I guess this is the right answer, but this is ugly(not your answer, just the way of coding I'm trying to use here). I don't want to write 4 lines of code per form, create 4 variables to use one variable in 2 forms. What if I want to use the variable in 6 forms? Could you take a look at my new question : http://www.codeproject.com/Questions/469358/Passing-data-between-forms-why-so-complicated which goes a little further than this question. Thank you in advance!
OriginalGriff 2-Oct-12 10:36am    
It isn't really complicated - the idea is that it reduces overall complexity by strictly limiting the possible number of unnecessary interconnections between separate objects.
Looking at your previous question, it really isn't the same thing - what you are trying to do in the other question is emulate global variables, which C# does not support. To do that, you either use a static data class (and the one instance is shared across the whole application) or a singleton class as was suggested.

If that is what you want to pass, then using either method will provide you with the same thing, without the need to pass anything from form to form.
HI
Simply pass the value you want to get from Form1 as a parameter to Form2 when you are creating an object of the Form2. In the constructor of Form2 just change the parameters accordingly. Then you will easily get the value. Store the formal parameter's value in a public or friendly variable of the class. And... you are done with it.

Hope it was of some help.
 
Share this answer
 
use constructor overloading
form 1
C#
private void printerDensityToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DensitySettingForm dsf = new DensitySettingForm(3);
            dsf.Showdialog();
            string Destinyval = dsf.Destiny;

        }


form 2
C#
string Destiny;
public New(int DensityValue)
{
     InitializeComponent();
     nudDensity.Value =DensityValue;
     Density=DensityValue;
}


Note: Replace new with your class name

Happy Coding!
:)
 
Share this answer
 
v5
Comments
lukeer 2-Oct-12 8:29am    
Is that New(type parameter) something new in .NET 4? Or did you actually mean DensitySettingForm(int densityValue), like a constructor overload?
Aarti Meswania 2-Oct-12 8:31am    
yes it's constructor overload
Hi

To pass data from Form1 to Form2, you can expose a public property in Form2 and Pass the value just after creating the object like.....

Form2 form2 = new Form2()
form2.[public property] = [your value]
form2.Show();

And to pass value from Form2 to Form1, you can create a custom event in Form2 and Handle that event from Form1.

Please see the link for more details
Passing Data between Windows Forms[^]

Hope that will help you.
 
Share this answer
 
Comments
fjdiewornncalwe 2-Oct-12 10:42am    
My vote of 1: Please read the other answers before posting one that is pretty much exactly what someone else has already answered. The thing to do there would be to upvote the answer you agree with and possibly leave a comment to that answer. Cheers.
I.explore.code 2-Oct-12 11:14am    
I guess I couldn't type fast enough ;). Anyway, the OP has found the "plug-n-play" solution in their follow up question which is more complete than this question. I believe everyone who contributed here based on a partial question deserves atleast 2 stars. So i have done that.
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA
 
Share this answer
 
You may follow these links:Click hereclick here

After reading this ,if u face any problem please knock me.
 
Share this answer
 
If you want to really decrease coupling, use an event aggregator. The following should give you the basics: Prism EventAggregator Sample[^]
 
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