通过简单的方法将一种形式的值传递给另一种形式 [英] Passing values one form to another form in simple method
问题描述
家庭作业
我正在使用带有C#代码的Windows窗体.
我有2种表格(分别名为frm1,frm2)
在那个frm1中,我有一个文本框(名为txtbx1)和一个按钮(名为btn_sub1)
在那个frm2中,我有一个文本框(名为txtbx2)和一个按钮(名为btn_sub2)
我运行应用程序,frm1将加载并单击btn_sub1按钮,它将打开frm2,我在txtbx2文本框中输入了一些值,然后单击btn_sub2按钮,txtbx2值将出现在frm1的txtbx1上.
问候
Vasanth
Homework
I am using windows form with c# code.
I have 2 Forms (named frm1, frm2)
In that frm1 i have one textbox (named txtbx1) and one button (named btn_sub1)
In that frm2 i have one textbox (named txtbx2) and one button (named btn_sub2)
I run the Application the frm1 will load and click the btn_sub1 button, it will opens the frm2, I have entered some values in that txtbx2 textbox and Click the button btn_sub2 that txtbx2 values will comes on the txtbx1 on the frm1
Regards
Vasanth
推荐答案
这是用于Form2
Hi,
This is for Form2
namespace FormCommunication
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
foreach (Form form in Application.OpenForms)
{
if (form.Name == "Form1")
{
(form.Controls["textBox1"] as TextBox).Text = textBox1.Text;
form.Activate();
}
}
}
}
}
现在是表格1:
Now for Form 1:
namespace FormCommunication
{
public partial class Form1 : Form
{
public string Username { get; set; }
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objSecondForm = new Form2();
objSecondForm.ShowDialog();
}
}
}
谢谢&问候,
Raghu
Thanks & Regards,
Raghu
http ://www.dreamincode.net/forums/topic/75208-passing-data-between-forms-in-c%23/ [在表单之间传递数据 [ http://stackoverflow.com/questions/1205195/how-to-pass-values-between-forms-in-c-sharp-windows-application [
http://www.dreamincode.net/forums/topic/75208-passing-data-between-forms-in-c%23/[^]
Passing Data Between Forms[^]
http://stackoverflow.com/questions/1205195/how-to-pass-values-between-forms-in-c-sharp-windows-application[^]
在Form1.cs中文件添加以下代码...
in Form1.cs file add following code...
public string val { get; set; }
private void btn_sub1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
method1();
}
public void method1()
{
if (val != "")
{
txtbx1.Text = val;
}
}
在Form2.cs文件上,添加以下代码...
On Form2.cs file add following code...
private void btn_sub2_Click(object sender, EventArgs e)
{
foreach (Form form in Application.OpenForms)
{
if (form.Name.Equals("Form1"))
{
((Form1)form).val = txtbx2.Text;
((Form1)form).method1();
}
}
this.Close();
}
享受编码...
Enjoy Coding...
这篇关于通过简单的方法将一种形式的值传递给另一种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!