在一个窗体中设置另一个窗体的控件属性

来源:互联网 发布:java编辑器哪个好 编辑:程序博客网 时间:2024/04/27 16:00

 最经有学生问我怎么样在一个窗体中设置另一个窗体的控件属性,问题如下:

有两个form.
一个form1,一个form2.
怎么样在form2中用代码设置form1的textbox的属性

我考虑了有如下两种方法:

 

一、

1、在form1类中定义一个静态数据成员,来保存当前窗体对象,
如下:  public static Form1 f1 = null;
2、  然后在form1窗体构造函数中,给上述静态成员初始化,如下:     
public Form1()
        {
            InitializeComponent();
            p1 = this;
        }
3、  在form2中调用form1,可以通过如下方式:
”Form1.f1”,
例如:  Form1.f1.textBox1.Text="你想设置的值";
以上所做的,只是让你能够访问当前form1对象或form2窗体对象,如果想操作控件,可以直接修改控件的成员访问符,即可以把”private”修改成”public”。当然最好的还是可以用增加公有属性的做法,这样不会破坏类本身的封装。
如下:
public string Text_Box
        {
            get { return textBox1.Text; }
            set { textBox1.Text = value; }
        }   

在form2的button_click事件中添加
Form1.p1.Text_Box = "你想设置的值";
这样就ok了!

 

源代码如下

Form1的代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace WindowsFormsApplication1
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public static Form1 p1 = null;
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.             p1 = this;
  18.         }
  19.         private void button1_Click(object sender, EventArgs e)
  20.         {
  21.             Form2 fr2 = new Form2();
  22.             fr2.Show();
  23.         }
  24.         public void setvalue(string a)
  25.         {
  26.             textBox2.Text = a;
  27.         }
  28.         public string Text_Box 
  29.         { 
  30.             get { return textBox2.Text; }
  31.             set { textBox2.Text = value; }
  32.         }
  33.     }
  34. }

Form2的代码:

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace WindowsFormsApplication1
  10. {
  11.     public partial class Form2 : Form
  12.     {
  13.         public Form2()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.         private void button1_Click(object sender, EventArgs e)
  18.         {
  19.             Form1.p1.Text_Box = "aaaa";
  20.         }
  21.     }
  22. }

 二、在Form1中定一个public方法和一个按钮事件(打开Form2)

  1. //public方法用于修改TextBox的值
  2. public void SetTextBox(string strValue)
  3. {
  4.     this.textBox1.Text = strValue;
  5. }
  6. //按钮事件
  7. private void button1_Click(object sender, System.EventArgs e)
  8. {
  9.     Form2 f = new Form2();
  10.     f.Tag = this//把Form1的实例传给Form2实例f的Tag属性
  11.     f.Show();
  12. }

Form2中的按钮事件

  1. private void button1_Click(object sender, System.EventArgs e)
  2. {
  3.     Form1 f = (Form1)this.Tag;//获取Form1实例
  4.     f.SetTextBox("去修改Form1中的TextBox的值");//调用Form1的public方法修改Form1中TextBox的值
  5. }

以上仅供参考,可能还有别的办法。