从已打开的form1中的另一个表单中获取值 [英] Get value from another form in already opened form1

查看:62
本文介绍了从已打开的form1中的另一个表单中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

My code work...problem is ...
Form 1 button click open Form2 and find value, when return value open new form not existing? Some help
I think it is problem line 11..12...





我尝试过:





What I have tried:

//form1:    
 public partial class Form1 : Form    
    {    
        Form2 f2;    
        public Form1()    
        {    
            InitializeComponent();    
        }    
        private void button1_Click(object sender, EventArgs e)    
        {    
            if (f2 == null)    
                f2 = new Form2();    
            f2.ValueFromForm1(textBox1.Text);    
            f2.Show();    
        }    
    }    
//form2:    
 public partial class Form2 : Form    
    {    
        public Form2()    
        {    
            InitializeComponent();    
        }    
        public void ValueFromForm1(string value)    
        {    
            textBox1.Text = value;    
        }    
    }    

推荐答案

问题可能是当Form2关闭时,你不能再次显示。

为新实例添加处理程序:

The problem is likely to be that when Form2 is closed, you can't Show it again.
Add a handler to your new instance:
Form2 f2 = null;
private void button1_Click(object sender, EventArgs e)
    {
    if (f2 == null)
        {
        f2 = new Form2();
        f2.FormClosed += f2_FormClosed;
        }
    f2.ValueFromForm1(textBox1.Text);
    f2.Show();
    }

void f2_FormClosed(object sender, FormClosedEventArgs e)
    {
    f2 = null;
    }

它可能会开始工作。


我的解决方案



Hi, my solution

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

        private void btngetdata_Click(object sender, EventArgs e)
        {
            foreach (Form frm in Application.OpenForms)
            {
                if (frm is Form2)
                {
                    this.Text = ((Form2)frm).textBox1.Text;
                }
            }
        }



只有form2 textbox1修饰符应该是公开的


only form2 textbox1 modifiers should be public


这篇关于从已打开的form1中的另一个表单中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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