herz
herz

Reputation: 35

passing data from form2 into form1 and save it in a string variable

Im passing data from a textBox from Form2 into Form1 via a second constructor.

After that, I want to set the textbox value from Form2 into a global string variable in Form1.

If I do this, the global variable gets the value I want but not that long. After I close the Form2 Dialog (this.Close() after code has proceeded) the main-constructor executes one more time and I am losing the value for my global string variable...

I have an workaround where I save the textbox value into an textfile and read it if I want the value in it, but that´s not the solution Im looking for.

Upvotes: 1

Views: 6511

Answers (3)

CindyH
CindyH

Reputation: 3026

I think you need a public property in Form2. I've done this in the past (and it's easy and works well), but what I'm posting is not tested code. It should get you started though.

FORM 2 CODE:

public string CommunicationStuff {get;set;} // private set 
                                            // if one-way communication
CommunicationStuff = myTextBox.Text;

and then in form1 after you call form2 and it closes, you can say

FORM1 Code:

Form2 subform = new Form2;
subform.CommunicationStuff = "Default value based on program state";
subform.ShowDialog();
string results = subform.CommunicationStuff;

edit: when searching for an example, I found this SO post: How to return a value from a Form in C#?

Upvotes: 2

Bauss
Bauss

Reputation: 2797

There are a few ways that you can achieve this. You can store static properties in a static class, static properties / properties in your form classes or you store values in a collection.

Example 1

A static class with static properties.

Source:

public static class FormValues
{
    private static string _message;
    public static string Message
    {
        get { return _message; }
        set
        {
            // Do stuff with value ...
            // Handle any errors with the value ...
            // Throwing an exception here will tell you which form it was thrown at too ...
            _message = value;
        }
    }
}

Usage:

// Form1
FormValues.Message = "Hello Form2";

// Form2
string message = FormValues.Message;

Example 2

Properties in your form.

Source:

// Form1 etc.
public static string Form1GlobalMessage { get; private set; }
public string Form1Message { get; private set; }

Usage:

// Form1
Form1Message = "This message is unique to this instance.";
Form1GlobalMessage = "This message is shared by all Form1 instances.";

// Form2
var form1 = new Form1();
string uniqueMessage = form1.Form1Message;
string globalMessage = Form1.Form1GlobalMessage;

The above can be done for all the forms and be used however you want. This is only readable outside of the form, since only Form1 has access to modify the properties. If you want to share something like this with getting and setting from multiple forms go with Example 1.

Example 3

A value collection.

In my opinion this should probably be the least used solution and should only be used if you wish to get/set dynamic values with dynamic identifications. Example 1 and Example 2 can be used as a reference of where to implement the collection. Instead of the properties you just put a collection ex. Dictionary<TKey,TValue>

Source:

// Example for the static class
public static class FormValues
{
    private static Dictionary<string,string> _messages;
    static FormValues()
    {
        _messages = new Dictionary<string, string>();
    }

    public static Dictionary<string,string> Messages
    {
        get { return _messages; }
    }
}

Usage:

// Form1
FormValues.Messages.Add("Message", "Hello World!");
// Form2
string message = FormValues.Messages["Message"];

Of course there is a lot other was to achieve these kind of mechanisms and it all depends on what you're doing and wants to do, in which way you do this.

This however should help you understanding the concept.

Upvotes: 2

Keysharpener
Keysharpener

Reputation: 504

I advise you use a class dedicated to storing and passing configuration values. In the following example you have a singleton that you can use to save and retrieve the value of a variable "Environment" :

public class GUIHelper
{
    public static GUIHelper _instance = new GUIHelper();
    public static GUIHelper Instance { get { return _instance; }}
    public string Environment { get; set; }
}

public class Form2
{
    public TextBox TextBox = new TextBox();
    public Form2()
    {
        TextBox.Text = GUIHelper.Instance.Environment;
    }
}

You can even go further and bind the value of the textbox to the property of GUIHelper.

Upvotes: 0

Related Questions