简体   繁体   English

将数据从form2传递到form1并将其保存在字符串变量中

[英]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. 我通过第二个构造函数将文本从Form2传递到Form1。

After that, I want to set the textbox value from Form2 into a global string variable in Form1. 之后,我想将Form2中的文本框值设置为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... 关闭Form2对话框(代码进行后的this.Close()之后,主构造函数执行了一次,我丢失了全局字符串变量的值...

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. 我有一个解决方法,我将文本框值保存到文本文件中并读取它,如果我想要它的值,但这不是我正在寻找的解决方案。

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 例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 例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. 这只能在表单之外读取,因为只有Form1才能修改属性。 If you want to share something like this with getting and setting from multiple forms go with Example 1 . 如果您想要从多个表单中获取和设置这样的内容,请参阅示例1

Example 3 例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. 示例1示例2可以用作实现集合的位置的参考。 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. 然而,这应该有助于您理解这个概念。

I think you need a public property in Form2. 我认为你需要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; 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之后调用form2并关闭它,你可以说

FORM1 Code: Form2 subform = new Form2; subform.CommunicationStuff = "Default value based on program state"; subform.ShowDialog(); string results = subform.CommunicationStuff; FORM1代码: Form2 subform = new Form2; subform.CommunicationStuff = "Default value based on program state"; subform.ShowDialog(); string results = subform.CommunicationStuff; 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#? 编辑:在搜索示例时,我发现了这个帖子: 如何从C#中的表单返回一个值?

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" : 在下面的示例中,您有一个单例,可用于保存和检索变量“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. 您甚至可以进一步将文本框的值绑定到GUIHelper的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM