转流从窗体2(TextBox2中)数据到Form1(TextBox1中)? [英] Transfering data from Form2 (textbox2) to Form1 (textbox1)?

查看:129
本文介绍了转流从窗体2(TextBox2中)数据到Form1(TextBox1中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:结果
我想控制从Form2的Form1中






我是一个新手,以 C#,我无法找到我要找的在谷歌的答案,所以我希望这里有人可以帮助我。我只练传输数据的(或者通过,称它为但是你想)的从形式到另一个。



下面就是我有:



我有2种形式 - Form1中窗体2 。结果
Form1中包含一个文本框(名为 txtForm1 )和一个按钮(名为 btnForm1 )。结果
窗体2 包含一个文本框(名为 txtForm2 )和一个按钮(名为 btnForm2 )。



运行的应用程序,通过点击按钮 btnForm1 ,用户打开后窗体2 。用户在文本框中写入( txtForm2 )应该被转移到文本框( txtForm1 ,该按钮的文本禁用)在 Form1中



我怎么能这样做转移?请帮忙



编辑:结果
好吧,我需要明确的是,这是所有的代码,我有:



 <$ C:

Form1中(按钮,打开窗体2 ) $ C>私人无效btnForm1_Click(对象发件人,EventArgs五)
{
新窗体​​2()展()。
}



窗体2 (按钮,关闭窗体2 ):

 私人无效btnForm2_Click(对象发件人,EventArgs五)
{
本。关();
}



我没有别的。 (我是一个新手总额)


解决方案

Form1中

 公共无效SetTextboxText(字符串文本)
{
txtForm1.Text =文本;
}

私人无效btnForm1_Click(对象发件人,EventArgs五)
{
变种FRM =新的窗体2(本); //在构造函数中
frm.Show通父窗体(本)();
}

窗体2

 表格_parentForm; 

公共窗体2(表格形式)
{
_parentForm =形式;
}

私人无效txtForm2_TextChanged(对象发件人,EventArgs五)
{
_parentForm.SetTextboxText(txtForm2.Text); //改变Form1.txtForm1.Text
}


Possible Duplicate:
I would like to control Form1 from Form2

I'm a newbie to C# and I can't find the answer I'm looking for in google, so I'm hoping someone here could help me. I'm only practicing to transfer data (or pass, call it however you want) from a form to another.

Here's what i have :

I have 2 forms - Form1 and Form2.
Form1 contains a textbox (named txtForm1) and a button (named btnForm1).
Form2 contains a textbox (named txtForm2) and a button (named btnForm2).

After running the application, by clicking the button btnForm1, the user opens Form2. The text that the user writes in the textbox (txtForm2) should be transfered to the textbox (txtForm1, which button is disabled) in Form1.

How can I do this transfer ? Please help.

Edited:
Okay i need to be clear that this is all the code i have:

Form1 (button which opens Form2):

    private void btnForm1_Click(object sender, EventArgs e)
    {
        new Form2().Show();
    }

Form2 (button which closes Form2):

    private void btnForm2_Click(object sender, EventArgs e)
    {
        this.Close();
    }

I have NOTHING ELSE. (I'm a total newbie)

解决方案

in Form1:

public void SetTextboxText(String text)
{
    txtForm1.Text = text;
}

private void btnForm1_Click(object sender, EventArgs e)
{
    var frm = new Form2(this); // pass parent form (this) in constructor
    frm.Show();
}

in Form2:

Form _parentForm;

public Form2(Form form)
{
    _parentForm = form;
}

private void txtForm2_TextChanged(object sender, EventArgs e)
{
    _parentForm.SetTextboxText(txtForm2.Text); // change Form1.txtForm1.Text
}

这篇关于转流从窗体2(TextBox2中)数据到Form1(TextBox1中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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