主窗体顶部为菜单按钮,子窗体内嵌入Panel显示
界面如下:
第二步,主窗体离不开的几个方法
1,点击菜单功能,加载子窗体
private void btnOpenForm_Click(object sender, EventArgs e)
{
try
{
string frmName = ((ButtonX)sender).Tag.ToString();
CloseForm();
Form objForm =
(Form)Assembly.LoadFrom("CardManager.EXE").CreateInstance("CardManager." + frmName);
this.OpenForm(objForm);
}
catch (Exception ex)
{
MessageBox.Show("错误提示:" + ex.Message,"提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
2,打开窗体方法
private void OpenForm(Form objForm)
{
objForm.TopLevel = false;
objForm.Parent = this.pnlFill;
objForm.FormBorderStyle = FormBorderStyle.None;
objForm.StartPosition = FormStartPosition.CenterParent;
objForm.WindowState = FormWindowState.Maximized;
objForm.Show();
}
3,关闭窗体方法
private void CloseForm()
{
foreach (Control item in this.pnlFill.Controls)
{
if (item is Form)
{
Form objFrm = item as Form;
objFrm.Close();
this.pnlFill.Controls.Remove(item);
}
}
}
4,关闭系统事件的处理 ,用到窗体的FormClosing事件
private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result = MessageBox.Show("真的要关闭系统吗?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if(result==DialogResult.Cancel)
{
e.Cancel = true;
}
}