在我们使用其他表单时设置文本框值 [英] Set textbox value while we are on another form

查看:80
本文介绍了在我们使用其他表单时设置文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了两种形式:Form1,Form2.
在Form1上,我有一个新的按钮和文本框.当我们单击新按钮Form2打开并在Form2上时,我们将一个值保存到数据库中.

现在,我想在关闭Form2之后在Form1文本框中获取Form2文本框的值.
该怎么做...?

请帮助...

解决方案

选中此项.有很多方法可以做到这一点.
http://stackoverflow. com/questions/1559770/send-values-from-one-form-to-another-form-in-c-sharp-winforms-application [

Form1(获取变量的值)
textbox1.text = studentName;

请参阅以下链接变量和方法范围
http://msdn.microsoft.com/en-us/library/ms973875 .aspx [^ ]

希望这对您有帮助!


  public   partial   class  Form1:表单
   {
      公共 Form1()
      {
         InitializeComponent();
      }

      私有 无效 button1_Click(对象发​​件人,EventArgs e)
      {
         Form2 ChildFrm =  Form2( this );
         ChildFrm.ShowDialog();
      }
   } 



 公共 部分  class  Form2:表格
   {
      私有 Form1 ParentForm;
      公共 Form2(Form1 parentForm)
      {
         InitializeComponent();
         ParentForm = parentForm;
      }

      私有 无效 Form2_FormClosing(对象发​​件人,FormClosingEventArgs e)
      {
         ParentForm.Controls [" ].Text = ChildTextBox.Text;
      }
   } 


I have created two forms: Form1, Form2.
On Form1 I have a new button and textbox. when we click on new button Form2 open and on Form2 we put a value and save it to a database .

Now I want to get Form2 textbox value on Form1 textbox after closing of Form2.
how to do this...?

Please help...

解决方案

Check this. There are many ways to do this.
http://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form-in-c-sharp-winforms-application[^]


If you are creating Windows Application:

Create Global Variable :
string studentName;

Form2 (Set variable''s value in code)
studentName = "Your Name";

Form1 (Get variable''s value)
textbox1.text = studentName;

refer following link Variable and Method Scope http://msdn.microsoft.com/en-us/library/ms973875.aspx[^]

Hope this helps !!


public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }

      private void button1_Click(object sender, EventArgs e)
      {
         Form2 ChildFrm = new Form2(this);
         ChildFrm.ShowDialog();
      }
   }



public partial class Form2 : Form
   {
      private Form1 ParentForm;
      public Form2(Form1 parentForm)
      {
         InitializeComponent();
         ParentForm = parentForm;
      }

      private void Form2_FormClosing(object sender, FormClosingEventArgs e)
      {
         ParentForm.Controls["ParentFormTextBox"].Text = ChildTextBox.Text;
      }
   }


这篇关于在我们使用其他表单时设置文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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