[英]C# WindowsForms - Send textbox from form1 to form2 on visual studio
i have 2 forms on visual studio, 我在Visual Studio上有2种表单,
form1 have textbox1.text
form2, have textbox2.text and btnSave
obs: form2 open when i click on another button on form 1: obs:当我单击表单1上的另一个按钮时,form2打开:
Form new = new form2();
nova.Show();
how can i send textbox2 content from form2 to form1 (textbox1) clicking on btnSave ? 如何单击btnSave将textbox2内容从form2发送到form1(textbox1)? What code will be necessary inside this click button event. 在此单击按钮事件内,将需要什么代码。
Thanks 谢谢
Try this please: Step1: Create a constructor for form2 class as below: 请尝试以下操作:步骤1:为form2类创建一个构造函数,如下所示:
public Form2(string strTextBox)
{
InitializeComponent();
label1.Text = strTextBox;
}
Step2: Instantiate form2 class in form1's button click event handler as below: 步骤2:在form1的按钮单击事件处理程序中实例化form2类,如下所示:
private void button1_Click(object sender, EventArgs e)
{
Form2 obj1 = new Form2(textBox1.Text);
obj1.Show();
this.Hide();
}
Create an event on your second form that can be fired when the form is saved: 在第二个表单上创建一个事件,该事件可以在保存表单时触发:
public event Action Saved;
Then create a property on that form that allows the textbox's text to be accessed: 然后在该表单上创建一个属性,该属性允许访问文本框的文本:
public string SomeTextValue //TODO: rename to meaningful name
{ get{ return textbox2.Text;} }
Then you need to fire off the Saved
event when you save your form: 然后,您需要在Saved
表单时触发Saved
事件:
if(Saved != null)
Saved();
Then when you first create the form in Form1
attach an event handler to that event: 然后,当您第一次在Form1
创建表单时,将事件处理程序附加到该事件:
Form2 child = new Form2();
child.Saved += () => textbox1.Text = child.SomeTextValue;
child.Show();
Note that if you are also closing the second form right when you save it then you don't need a custom event, you can just utilize FormClosing
instead. 请注意,如果在保存时也要关闭第二个窗体,则不需要自定义事件,您可以直接使用FormClosing
。
researching i was able to make it work, lost some hours but now all is perfect, this is code that worked for me: 研究我能够使它工作,浪费了一些时间,但现在一切都很完美,这是对我有用的代码:
On form2: 在form2上:
public partial class form2 : Form
{
private string nome;
public string passvalue
{
get { return nome; }
set { nome = value; }
}
form2, button save: form2,按钮保存:
private void btnSalvar_Click(object sender, EventArgs e)
{
passvalue = txtMetragemcubica.Text;
this.Hide();
}
on form1 (this button open form2): 在form1上(此按钮打开form2):
private void btnMetragemcubica_Click(object sender, EventArgs e)
{
form2 n = new form2();
n.ShowDialog();
txtMetragem.Text = n.passvalue
}
Now it work this way: Open on form 1, then i click on button btnMetragemcubica and form2 open, then i insert values on different textbox and have result on txtMetragemcubica, when i click on save button (btnSalvar) it close form2 and send value to form1 in txtMetragem textbox. 现在,它以这种方式工作:在窗体1上打开,然后单击btnMetragemcubica按钮并打开form2,然后在不同的文本框中插入值,并在txtMetragemcubica上生成结果,当我单击保存按钮(btnSalvar)时,它将关闭form2并将值发送给txtMetragem文本框中的form1。
Working perfect here, hope help another persons too. 在这里工作完美,希望也能帮助别人。 Anyway thanks for all help 无论如何感谢所有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.