一般中大型程序的画面都是由一个主框架,以及若干业务功能画面组合而成。画面跳转过程中,主框架永远不变,业务功能画面跳转。主框架可以管理业务功能画面,控制其生成,使用,显示,消亡过程。现在给出自写的一个主框架代码。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace CommonComponent
{
public partial class FormBase : Form
{
#region 成员变量声明
private string m_CurrentKey = null;
#endregion
#region 构造函数
public FormBase()
{
InitializeComponent();
}
#region 方法
/// <summary>
/// 根据key值获取相应的画面 ,这个方法由子类执行
/// </summary>
/// <param name="panelKey"></param>
/// <returns></returns>
protected virtual SubForm GetForm(string panelKey)
{
return null;
}
/// <summary>
/// 显示出加载的画面
///mainPanel是主边框下的一个面板,原理是把业务画面上的控件向这个面板扔
/// </summary>
/// <param name="panelKey"></param>
protected void LoadPanel(string panelKey)
{
if (!panelKey.Equals(m_CurrentKey))
{
Panel panel = new Panel();
SubForm frm = GetForm(panelKey);
//设置画面上的按钮是否可用
frm.SetBtnEnable();
//对画面上其他空间进行预处理
frm.PreDealWith();
this.MainPanel.Hide();
this.MainPanel.Controls.Clear();
int count = frm.Controls.Count;
for (int i = 0; i < count; i++)
{
this.MainPanel.Controls.Add(frm.Controls[0]);
}
this.MainPanel.Show();
m_CurrentKey = panelKey;
}
#endregion
}
}
}
缺点:由于是控件移了过来,窗体本身没有显示,所以与业务窗体有关的方法都不能使用.