从窗口2另一个文本框获取Form1上从一个文本框的数据 [英] Get data from one textbox on form1 from another textbox on form2

查看:125
本文介绍了从窗口2另一个文本框获取Form1上从一个文本框的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种形式Form 1和Form。我想从窗口2的文本框获取文本时,点击Form1上放置一个按钮。我使用在Form1上:

I have two forms form1 and form2. I want to get the text from the textbox of form2 when a button is clicked on form1. I am using on form1:

private void but_Click(object sender, EventArgs e)
{
    Form2 f2=new Form2();
    txtonform1=f2.fo;
}



窗口2 我有这个方法返回从文本框中的文本:

and on form2 I have this method to return the text from the textbox:

public string fo
{            
    get { return textBox1.Text; }
    set { textBox1.Text = value; }
}

现在的问题是,它返回。请告诉我我是新的C#问题的任何人可以帮我请!

Now the problem is that it returns null. Whats the problem I am new to c# can anybody help me please!

推荐答案

您有一个单一的形式工作,否则你创建新实例每次:

You have to work with one single form, otherwise you create new instance every time:

Form2 f2 = new Form2();

private void but1_Click(object sender, EventArgs e)
{
    f2.fo=txtonform1.Text;
}

private void but2_Click(object sender, EventArgs e)
{
    MessageBox.Show(f2.fo);
}

这篇关于从窗口2另一个文本框获取Form1上从一个文本框的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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