从Form2返回时需要将数据返回到Form1 Textbox1中 [英] Need data back in form1 Textbox1 while coming back from form2

查看:49
本文介绍了从Form2返回时需要将数据返回到Form1 Textbox1中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我是C#编码的新手,想知道如何从Form2返回时如何将数据保存在Form1中的文本框中.

其实朋友我有两种形式.在form1中,我有一个文本框和一个按钮.
在form2中,我只有一个Button(返回form1).

在form1s按钮中,我编写了
Form2 a = new Form2();
a.show();
This.Hide();

并在Form2按钮中我编写了
Form1 a =新的Form1();
a.show();
This.Hide();

但是,每当我单击Form2中的一个按钮时.它以新的Form.1形式启动Form1.意味着新的Form ...它没有向我显示文本框中的数据.请尝试解决该问题,并以某种方式编写,以便我可以理解它. knw ..:P

请理解我的英语我不好. :P
谢谢,
Sudesh ..

hi all
i m new to C# coding want to knw how we can hold the data in textbox in form1.while coming back from Form2.

Actually friends i m having two forms. in form1 i m having a Textbox and a button.
and in form2 i m having just a Button( to go back to form1).

in form1s button i hav written-
Form2 a = new Form2();
a.show();
This.Hide();

and in Form2 button i hav written
Form1 a = new Form1();
a.show();
This.Hide();

But whenver i clicked a Button in Form2. it starts Form1 as a new Form..means a Fresh Form...it is not showing me the Data in Textbox..Please try to resolve it.and write in a way so tht i can understand it..as i m new u knw..:P

and please understand my english i m not good in it. :P
Thanks,
Sudesh..

推荐答案

好,所以解决方案1适用于Asp.net,据我了解,这是Windows Form应用程序.因此,在这种情况下,最简单的方法是将引用从form1传递到form 2,如下所示:

形式1:

Ok so Solution 1 is for Asp.net and as I understand it this is a Windows Form App. So in that case the easiest way to do this is to just pass a reference from form1 to form 2 like so:

In form1:

private void button1_Click(object sender, EventArgs e)
{
    Form2 a = new Form2(this);
    a.Show();
    this.Hide();
}



并为form2:



and in form2:

public partial class Form2 : Form
{
    Form1 mainForm;

    public Form2(Form1 mainForm)
    {
        InitializeComponent();

        this.mainForm = mainForm;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        mainForm.Show();
        this.Hide();
    }
}



Form2中还有另一件事调用this.Hide();.该表单仍将存在于内存中,只有在您打算这样做的情况下,它才会对用户隐藏.但是,如果要处置form2,请调用this.Close().



Also one more thing in Form2 calling this.Hide(); the form will still exist in memory it will only be hidden from the user if this is what you intended that that is fine. However if you want to dispose of the form2 call this.Close() instead.


为此使用ViewState ...更多详细信息和示例,请查看下面的链接

http://asp.net-tutorials.com/state/viewstate/ [
Use ViewState for that...for more detail and example check the below link

http://asp.net-tutorials.com/state/viewstate/[^]


这篇关于从Form2返回时需要将数据返回到Form1 Textbox1中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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