1)在窗体Form1上有Panel控件panel1
2)Form1.cs(后台代码)
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 Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// 设置panel1的Visible
public void SetPanelVisible(bool visible)
{
this.panel1.Visible = visible;
}
private void button1_Click(object sender, EventArgs e)
{
// 显示窗体2
Form2 f2 = new Form2(this);
f2.Show();
}
}
}
3)Form2
4)Form2.cs
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 Form2(Form1 f1):this()
{
this.f1 = f1;
}
// 保存窗体Form1的实例
Form1 f1;
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
f1.SetPanelVisible(checkBox1.Checked);
}
}
}
5)运行