从一种形式传递到另一个值 [英] pass a value from one form to another

查看:161
本文介绍了从一种形式传递到另一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种形式,我需要从form1.textbox1值传递给form2.variable

I have two forms, and I need pass a value from form1.textbox1 to form2.variable

Form1中:

string Ed = "", En = ""; 

    public string En1
    {
        get { return En; }
        set { En = value; }
    }

    public string Ed1
    {
        get { return Ed; }
        set { Ed = value; }
    }
    private void button1_Click(object sender, EventArgs e)
    {

        Form2 F2 = new Form2();
        F2.Show();
        F2.textbox1value = Ed;
        F2.textbox2value = En;
    }



`
和窗体2:

` and Form2:

public string textbox1value
    {
        get { return textBox1.Text; }
        set { textBox1.Text = value; }
    }
    public string textbox2value
    {
        get { return textBox2.Text; }
        set { textBox2.Text = value; }
    }

private void button1_Click(object sender, EventArgs e)
    {
        Form1 F1 = new Form1();
        F1.Ed1 = textBox1.Text;
        F1.En1 = textBox2.Text;
    }

当我点击保存在窗口2并打开调试之我见ED = 3; EN = 5,但是当我点击打开窗口2在1并调试,我看到埃德= NULL; EN = NULL;并显示出无文本框值的空白表单。帮助请。

when I click "save" on form2 and open debug I see "ed = 3; en = 5", but when i click "open form2" on form1 and open debug, i see "Ed = null; En = null;" and shows a blank form without value in textboxes. help please.

推荐答案

您创建一个的形式,所以旧值将丢失。 ,默认值是空

You create a new form, so old values will be lost. Default values are null.

Form1 F1 = new Form1(); //I'm a new Form, I don't know anything about an old form, even if we are the same type

您可以使用静态瓦尔,这将是最简单的解决方案,归档你的目标,但也有类似的构造,容器,活动等其他方式。

You can use static vars, which would be the easiest solution to archive your goal, but there are other ways like constructors, containers, events etc.

public static string En1
    {
        get { return En; }
        set { En = value; }
    }

    public static string Ed1
    {
        get { return Ed; }
        set { Ed = value; }
    }

和在其他形式的

private void button1_Click(object sender, EventArgs e)
    {
        Form1 F1 = new Form1();
        Form1.Ed1 = textBox1.Text;
        Form1.En1 = textBox2.Text;
    }

请注意,静态变量只有一次的一类存在。所以,如果你有多个实例,你改变静态变量之一,变化也影响到所有其他实例。

Please be advised that a static variable exists only once for a class. So if you have multiple instances and you change the static variable in one, the change also affects all other instances.

这篇关于从一种形式传递到另一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆