如何从Form 2更新Form 1中的文本框 [英] How to update the textbox in Form1 from Form2

查看:134
本文介绍了如何从Form 2更新Form 1中的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表单,Form1和Form2。



在Form1中我有文本框,链接标签和按钮。当我点击linklabel时,它会打开Form2。



在Form2中,我将从文本框中获取输入并将其存储在变量'str'中。当我单击Form2中的按钮时,它应关闭Form2并使用'str'中的值更新Form1中的文本框。



  public   partial   class  Form2:表单
{
private void button1_Click( object sender,EventArgs e)
{
string str = textbox1.Text;
// 此处的代码用于设置Form1中的文本框值
.close();
}

解决方案

在Form1中写一个属性,它保存文本框的值,如

< pre lang =c#> private string _busnamevar = string .Empty;
public string Busnamevar
{
get
{
return _busnamevar;
}
set
{
_busnamevar = value ;
textBox1.Text = Busnamevar;
}
}





Form1中的按钮点击事件

  private   void  button1_Click(对象发​​件人,EventArgs e)
{
Form2 fr = new Form2( this );
fr.Show();
}





在Form2中声明一个变量,如

  private  Form1 form1; 





From2构造函数

  public  Form2(Form1 f)
{
form1 = f;
InitializeComponent();
}





按钮点击Form2

  private   void  button1_Click( object  sender ,EventArgs e)
{
string str = textBox1.Text;
// 此处的代码用于设置Form1中的文本框值
form1.Busnamevar = str;
this .Close();

}


您可以将Form2显示为具有父Form1的模型对话框,并在关闭Form2时设置DialogResult 。检查Form1中的DialogResult并更新Form1的文本框。

下面是代码:



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

private void linkLabel1_LinkClicked( object sender,LinkLabelLinkClickedEventArgs e)
{
Form2 form2 = new Form2();
if (form2.ShowDialog()== DialogResult.OK)
{
textBox1.Text = str1;
}
}
}





表格2



  public   partial   class  Form2:表格
{
public string str;
public Form2()
{
InitializeComponent();

}

private void button1_Click(< span class =code-keyword> object sender,EventArgs e)
{
str = textBox1.Text;
Form1.str1 = str;
this .DialogResult = DialogResult.OK;
this .Close();

}
}


这是一个很好的例子..试试吧



http://www.daniweb.com/software-development/csharp/threads/358513/pass-the-text-box-value-from-one-form-to-another-form-textbox [ ^ ]


I have 2 forms, Form1 and Form2.

In Form1 i have textbox, linklabel and button. When i click on linklabel it opens Form2.

In Form2 i will take input from textbox and store it in the variable 'str'. When i click on button in Form2, it should close Form2 and update the textbox in Form1 with the value in 'str'.

public partial class Form2: Form
    {
 private void button1_Click(object sender, EventArgs e)
        {
string str=textbox1.Text;
// code here to set the textbox value in Form1
this.close();
}

解决方案

Write a property in Form1 which holds the value of textbox like

private string _busnamevar = string.Empty;
 public string Busnamevar
 {
     get
     {
         return _busnamevar;
     }
     set
     {
         _busnamevar = value;
         textBox1.Text = Busnamevar;
     }
 }



Button click event in Form1

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



Declare a variable in Form2 like

private Form1 form1;



From2 constructor

public Form2(Form1 f)
{
    form1 = f;
    InitializeComponent();
}



button click in Form2

private void button1_Click(object sender, EventArgs e)
{
    string str = textBox1.Text;
    // code here to set the textbox value in Form1
    form1.Busnamevar = str;
    this.Close();

}


You can show the Form2 as model dialog having parent Form1, and when closing the Form2 set DialogResult. Check DialogResult in Form1 and update textbox of Form1.
Below is the code:

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

       private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
       {
           Form2 form2 = new Form2();
           if(form2.ShowDialog()==DialogResult.OK)
           {
               textBox1.Text = str1;
           }
       }
   }



Form 2

public partial class Form2 : Form
    {
        public string str;
        public Form2()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            str = textBox1.Text;
            Form1.str1 = str;
            this.DialogResult = DialogResult.OK;
            this.Close();

        }
    }


Here a good example.. try it

http://www.daniweb.com/software-development/csharp/threads/358513/pass-the-text-box-value-from-one-form-to-another-form-textbox[^]


这篇关于如何从Form 2更新Form 1中的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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