环境:XPsp2 + VS2005
说明:共两个窗体,每个窗体上有一个Button,点击Form1上的Button,则显示窗体Form2,同时Form1隐藏,点击Form2上的Button,则关闭Form2,同时Form1重现.Form2上的Button在鼠标进入和离开时会切换背景图片,背景图由一个imagelist保存.
代码:
//窗体Form1
public partial class Form1 : Form
{
private Form2 frm2;
public Form1()
{
InitializeComponent();
}
private void frm_Closed(object sender, FormClosedEventArgs e)
{
this.Show();
}
private void button1_Click(object sender, EventArgs e)
{
frm2 = new Form2 ();
frm2.FormClosed += new System.Windows.Forms.FormClosedEventHandler(frm_Closed);
frm2.Show();
this.Hide();
}
}
//窗体Form2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//MessageBox.Show("Before Button1_Click");
this.Close();
// MessageBox.Show("After Button1_Click");
}
private void button1_MouseEnter(object sender, EventArgs e)
{
this.button1.BackgroundImage = imageList1.Images[0];
}
private void button1_MouseLeave(object sender, EventArgs e)
{
//MessageBox.Show("Before Button1_MouseLeave");
//当点击Button1关闭窗体时,问题出在下面这句
//错误指向imagelist1,提示的信息是:InvalidArgument=“1”的值对于“index”无效。
//参数名: index.
//如果加上MessageBox语句,则不会出现错误.
this.button1.BackgroundImage = imageList1.Images[1]; //问题出在这里
//MessageBox.Show("After Button1_MouseLeave");
}
}
另外问下:Button1_Click事件是否一定将先触发Button1_MouseLeave事件.