在表单之间传递表单值 [英] passing form values between forms
本文介绍了在表单之间传递表单值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两种形式;表格1和表格2.
1.我单击表格一中的按钮以打开表格二.
2.我在形式二的文本框中输入值.
3.我单击形式二中的保存.
4.值以格式一显示在文本框中.
我该如何实施?
在此先谢谢您.
I have two forms; form 1 and form 2.
1. I click on a button in form one to open form two.
2. I enter value into a textbox in form two.
3. I click on save in form two.
4. Value is displayed in textbox in form one.
How can I implement this?
Thanks in advance.
推荐答案
请不要执行АslamIqbal或Manivannan Shan所建议的操作:如果不建议将表单上的任何控件公开,则不要这样做.原因很简单:这样做时,您会将两个表单的设计联系在一起-无法更改form1,除非检查它不会影响form2.您也不能重用form3中的form2,因为它可能没有相同的布局或用途.
相反,有两种方法可以做到:
1)像对待OpenFileDialog一样对待form2:将其显示,等待用户按OK,然后关闭form2并在form1中获取值.这很容易:设置一个公共属性,该属性以与OpenFileDialog提供FileName
相同的方式返回值.
Please, do not do what Аslam Iqbal or Manivannan Shan are recommending: it if a bad idea to make any controls on your form public. The reason is simple: when you do that you ties the design of the two forms together - you cannot change form1 without checking that it will not affect form2. You also cannot re-use form2 from form3 because it may not have the same layout or purpose.
Instead, there are two ways to do it:
1) Treat form2 like you would a OpenFileDialog: Show it, wait for the user to press OK and the close form2 and fetch the value in form1. That''s easy: Set up a public property which returns the value(s) in the same way the OpenFileDialog supplies FileName
public string Value
{
get { return textboxUserInputTo.Text; }
set { textboxUserInputTo.Text = value; }
然后使用
显示您的表单
Then display your form with
form2 f2 = new form2();
if (f2.ShowDialog() == DialogResult.OK)
{
string s = f2.Value;
...
}
2)在form2中设置一个事件,说我对你有价值".
2) Set up an event in form2 which says "I have a value for you".
public event EventHandler Changed;
protected virtual void OnChanged(EventArgs e)
{
EventHandler eh = Changed;
if (eh != null)
{
eh(this, e);
}
}
private void DoSomethingToChangeData()
{
OnChanged(null);
}
Form1然后订阅该事件.
Form1 then subscribes to the event.
private void ShowChildForm()
{
// Link in his event so if it changes, we detect it.
form2 f2 = new form2();
f2.Change += new form2.ChangeHandler(Changed);
f2.Show();
}
//
// Fired when the file is changed at a lower level.
//
private void Changed(object sender, EventArgs e)
{
form2 f2 = sender as form2;
string s = f2.Value;
}
在from2中执行以下操作...(将Form1实例传递给form2构造函数并进行全局分配.当form2按钮单击将值分配给form1文本框时)
注意:将文本框控件访问说明符设为public.
In from2 do the following...(Pass Form1 instance to form2 constuctor and to assign in global.when form2 button click assign value to form1 text box)
Note:Make the textbox control access specifier to public.
public partial class Form2 : Form
{
Form1 frm1 = null;
public Form2(Form form1)
{
InitializeComponent();
frm1 = (Form1)form1;
}
private void button1_Click(object sender, EventArgs e)
{
frm1.textBox1.Text = "your value";
}
}
同样明智的是,在Form1中
Like wise, in Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(this);
frm2.Show();
}
请参见 ^ ]
这篇关于在表单之间传递表单值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文