日期:2014-05-18  浏览次数:21186 次

在主介面的lable1里的值,在进入B画面时,B画面的lable1里有显示这个值
在主介面的lable1里的值,在进入B画面时,B画面的lable1里有显示这个值,怎么做?
我的: public partial class Form1 : Form
  {
  public Form1()
  {
  InitializeComponent();
  }

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

  private void button2_Click(object sender, EventArgs e)
  {
  label1.Text = "123456";
  }

   
  }
}
B介面:
  public partial class Form2 : Form
  {
  public Form2()
  {
  InitializeComponent();
  }

  private Form1 a1;
  public Form2(Form1 fr1)
  {
  InitializeComponent();
  if (a1 != null) 
  a1=fr1;
  }

  private void Form2_Load(object sender, EventArgs e)
  {
   
  label2.Text=a1.label1.Text;
  }
但运行时出现:
   
  label2.Text=a1.label1.Text;这一段有问题:提示什么未处理NullReference.....
我在主介面
Lable1属性要更改成public

















------解决方案--------------------
将label值公开为属性
调用页面new一个上级窗体


去搜搜 winform传值
------解决方案--------------------
http://blog.pconline.com.cn/article/2222461.html 如何传值

1 全局变量
form1 public static string strLabel ;
private void button2_Click(object sender, EventArgs e)
{
label1.Text = "123456";
strLabel =label1.Text;
}
Form2
textBox1.Text = Form1.strLabel .ToString();
------解决方案--------------------
以下正解:
1、在FORM2中设置LABEL为PUBLIC
2、在FORM1中这样调用:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.label1.Text = label1.Text;
f2.ShowDialog();
}