请大师看这个问题
aw214 2011-06-13 08:24:54 第一段代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public string str = "hfghh";
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
form2.Invalidate();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
Form1 form1 = new Form1();
private void Form2_Load(object sender, EventArgs e)
{
}
private void Form2_Paint(object sender, PaintEventArgs e)
{
MessageBox.Show(form1.str); //弹出来了MessageBox.Show对话窗口
}
}
第二段代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Dictionary<int, string> dic4 = new Dictionary<int, string>();
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
dic4.Add(1, "HaHa");
dic4.Add(5, "HoHo");
dic4.Add(3, "HeHe");
dic4.Add(2, "HiHi");
dic4.Add(4, "HuHu");
form2.Show();
form2.Invalidate(); //重绘
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
Form1 form1 = new Form1();
private void Form2_Load(object sender, EventArgs e)
{
}
private void Form2_Paint(object sender, PaintEventArgs e)
{
foreach (var pair in form1.dic4)
{
MessageBox.Show(pair.Value); //为什么没弹出对话窗口?
}
}
}
问题:
第二段代码的结构组成和第一段代码应该是一样的,为什么第二段代码不能弹出MessageBox.Show对话窗口呢?
谢谢