日期:2014-05-19  浏览次数:20750 次

虚心的请教一个问题!
我的程序是这样的,有俩个窗口,一个窗口要输入数据,另一个要输出这些数据.这样就
有俩个FORM类.我在第一个FORM类中定义了一个结构体数组用来储存输入的数据,而   想在第二个类中输出这些数据,但在第一个类中定义的结构体在第二个类中肯定不使用,这时我该怎么办.

------解决方案--------------------
在第一个FORM类中定义了一个结构体数组用来储存输入的数据

问题可能就在这里了。你应该在FORM类外面定义结构,比如
class Form1
{
}
struct DataStruct
{
}
class Form2
{
}

这样就可以在两个Form类中同时使用这个结构。如果写成
class Form1
{
struct DataStruct
{
}
}
class Form2
{
Form1.DataStruct struct1 = new Form1.DataStruct() // 必须如此引用
}
------解决方案--------------------
用构造函数可以实现,我弄了个简单的程序试了一下...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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

private void textBox1_TextChanged(object sender, EventArgs e)
{

}


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

}
}
}


///
sing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Form2(string id)
{
InitializeComponent();
this.textBox1.Text = id;
}

private void Form2_Load(object sender, EventArgs e)
{

}
}
}