在同一解决方案中将值从1形式检索为其他形式 [英] Retrievng values from 1 form to other form in the same solution

查看:54
本文介绍了在同一解决方案中将值从1形式检索为其他形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从第一个表单文本框检索值到另一个表单文本框.

how to retrieve values from first form textboxes to another form textboxes.

推荐答案

1.在form2(Eg:Form1String)中创建公共变量或属性

1. Create a public variable or property in form2 (Eg:Form1String)

public partial class Form2 : Form
    {
        public string Form1String;

        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Form1String);
        }
    }



2.从form1调用form2时,设置form2属性.



2. Set for form2 property when calling form2 from form1

Form2 ofrm2 = new Form2();
           ofrm2.Form1String = textBox1.Text;
           ofrm2.Show();



3.单击表单2按钮时,它将显示您在form1中输入的文本

希望这对您有帮助



3. When you click on the form 2 button it will display the text you have entered in form1

Hope this helps


以第二种形式创建一个set属性,例如
Create one set property in second form like
public String FirstFormText
        {
           set { textBox1.Text = value; }

        }




将TextBox值分配给第二个表单属性,这将调用属性中的set方法,并将您的文本分配给第二个表单文本框




Assign TextBox value to second form property this will call set method in property and assign your text to second form text box

frm.FirstFormText = textBox1.Text; 


感谢BillWoodruff的想法,我现在修改了代码,因此不需要从form1打开form2,希望它可以解决此问题.

1.在form2类下定义以下内容

Thanks for the thoughts BillWoodruff, I have modified the code now so that the form2 does not needs to be opened from form1 and hope this resolves the issue

1. Define the following under the form2 class

public static Form IsFormAlreadyOpen(Type FormType)
        {
            foreach (Form OpenForm in Application.OpenForms)
            {
                if (OpenForm.GetType() == FormType)
                    return OpenForm;
            }

            return null;
        }



2.在表格2中添加一个按钮,后面的代码是



2.Add a button in form 2 and the code behind is

private void button1_Click(object sender, EventArgs e)
       {
           if ((Form1)IsFormAlreadyOpen(typeof(Form1)) != null)
           {
               var x = (Form1)IsFormAlreadyOpen(typeof(Form1));
               MessageBox.Show(x.textBox1.Text);

           } else {

               MessageBox.Show("The forms is not opened");
           }
       }



3.单击表单2上的按钮,将在form1 Textbox1中检索值.

这不需要从form1调用form2并在单击按钮时返回Textbox1值.

希望对您有帮助



3. click the button on form 2 will retrieve the value in form1 Textbox1.

This does not need the form2 to be called from form1 and returns the Textbox1 value at that time the button is clicked.

Hope this helps


这篇关于在同一解决方案中将值从1形式检索为其他形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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