温控项目中使用反射可以动态的创建需要的窗体。

首先,我们在Load中启动执行:

RegisterPageBtnClick();
InitPageBtnTag();

RegisterPageBtnClick:

//设置分页按钮的Tag属性---页面关联
private void InitPageBtnTag()
{
    btnStorePage.Tag = typeof(store.FrmStoreList).FullName;//命名空间 +FrmStoreList
    btnSRegionPage.Tag = typeof(store.FrmStoreRegionList).FullName;
    btnProductPage.Tag = typeof(product.FrmProductList).FullName;
    btnProductInStorePage.Tag = typeof(product.FrmProductInStore).FullName;
    btnStoreTemperPage.Tag = typeof(storeTemper.FrmStoreRegionTemperatureList).FullName;
}

/// <summary>
/// 注册分页按钮的单击事件
/// </summary>
private void RegisterPageBtnClick()
{
     foreach(Control c in panelPageTop.Controls)
    {
         if(c is UPageButton)
        {
            UPageButton btn = c as UPageButton;
            btn.Click += btnPage_Click;
        }
    }
}

btnPage_Click使用Tag和反射创建新的实例。

//实例化
f = (Form)Activator.CreateInstance(Type.GetType(frmFullName));

/// <summary>
/// 分页按钮单击处理程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPage_Click(object sender, EventArgs e)
{
    //分页按钮文本颜色
    Color topSelColor = Color.FromArgb(45, 50, 116);//点击时或显示相关页面时
    Color topUnSelColor = Color.White;//默认或没有显示页面时

    UPageButton clickBtn = sender as UPageButton;//当前点击按钮

    foreach (Control c in panelPageTop.Controls)
    {
        if (c is UPageButton)
        {
            UPageButton btn = c as UPageButton;
            if (btn.Name != clickBtn.Name)
            {
                btn.ForeColor = topUnSelColor;
            }
        }
       
    }
    //切换页面的实现
    if(clickBtn.Tag!=null)
    {
        string frmFullName = clickBtn.Tag.ToString();
        int lastIndex = frmFullName.LastIndexOf('.');
        string frmName = frmFullName.Substring(lastIndex + 1);
        Form f = FormUtility.GetOpenForm(frmName);
        if(f==null)
        {
            //实例化
            f = (Form)Activator.CreateInstance(Type.GetType(frmFullName));
        }
        panelPage.AddPanelForm(f);
        //是否处理关闭按钮的显示
        ShowClosePageBtn();
    }
}
06-30 11:09