将值设置为另一种形式

时间:2014-01-15 11:16:30

标签: c#

我必须形成Form1Form2

Form1来源

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            frm.SetBtn = "teste test";
            frm.Show();
        }

        public string setLb
        {
            set
            {
                label1.Text = value;
            }
        }
    }
}

Form2来源

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 frm = new Form1();
            frm.setLb = "test test";
        }

        public string SetBtn
        {
            set
            {
                button1.Text = value;
            }
        }
    }
}

我尝试在Form1设置标签文字,在Form2设置按钮文字 我使用相同的方法设置值,但仅在设置值从Form1Form2时才有效。 Form2上的Button1.Text已更改为teste test,但Form1上没有任何内容

4 个答案:

答案 0 :(得分:4)

您需要将Form1实例的引用传递给Form2实例。

你可以这样做:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2(this); // <---- Pass a reference to this form to Form2
            frm.SetBtn = "teste test";
            frm.Show();
        }

        public string setLb
        {
            set
            {
                label1.Text = value;
            }
        }
    }
}

您还需要稍微更改Form2实现:

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        private Form1 other;

        //Empty constructor for the designer
        public Form2()
        {
            InitializeComponent();
        }

        public Form2(Form1 other)
        {
            this.other = other;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            other.setLb = "test test";
        }

        public string SetBtn
        {
            set
            {
                button1.Text = value;
            }
        }
    }
}

答案 1 :(得分:0)

使用Constructor方法,这里是示例

在form2中

public Form2(string strTextBox)
{
 InitializeComponent(); 
 label1.Text=strTextBox;
}

在form1点击事件

private void label1_Click(object sender, System.EventArgs e)
{
Form2 frm=new Form2(textBox1.Text);
frm.Show();
}

有关详细信息,请参阅Passing Data Between Forms

答案 2 :(得分:0)

在Form2中,您必须为Form1添加一个参数。

// Form1
...
private void button1_Click(object sender, EventArgs e)
{
    Form2 frm = new Form2(this);
    frm.SetBtn = "teste test";
    frm.Show();
}
...

// Form2
public Form2(Form frm1)
{
    InitializeComponent();
    frm1.setLb = "test test";
}

答案 3 :(得分:0)

public partial class Form1 : Form
{
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e){
Form2 frm = new Form2(this);
frm.Show();
}
}
public partial class Form2 : Form
{
Form _frm;
public Form2(Form frm)
{
_frm = frm;
InitializeComponent();
}`enter code here`

private void button1_Click(object sender, EventArgs e)
{
Form1 formVariable = (Form1)_frm;
formVariable.textBox.Text = "Hello";
}
}