有关C#数据在窗口之间传递的问题?

来源:百度知道 编辑:UC知道 时间:2024/06/06 10:24:58
form1的主要代码:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.lable1.Text = this.textBox1.Text;

}
}
}
form2中的主要代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public System.Windows.Forms.Label lable1;
private void Form2_Load(object sender, EventArgs e)

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2.XXX = this.textBox1.Text;
}
}

public partial class Form2 : Form
{
public static string XXX;

public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{
this.label.Text = XXX;
}
}
首先在窗体2中定义一个公共的字符串变量,就可以在窗体1中直接给它赋值,达到了传递数据的目的。以上代码将其定义为了静态的,这样就可以在窗体1的按钮事件中直接对其赋值,要不就只能在获取窗体2的实例后才能对其进行赋值。

先定义变量,在构造函数中赋值!

Form2还没有被加载啊