新项目刚结束,终于可以小小松口气,同时在这里与大家分享一些项目心得,会在本BLOG中不定期公布。首先分享的是动态创建MDIFORM的方法,当一个程序中包括有N十个MIDFORM时,你不会想着一个个去N行的代码去控制他显示与否吧?但这个项目中我们较多的程序员就是这样去控制的,在检查代码时,发现太多的冗余代码,不易于维护。由于本人比较懒,就想了以下方法去精简这个MIDFORM显示的过程。
方法代码:
/// <summary>
/// 根据登录权限判断弹出窗口
/// </summary>
/// <param name="strPrim">权限名</param>
/// <param name="fInput">弹出窗口名</param>
private void ShowForm( string strPrim, string fInput)
{
//判断是否已经打开,如果打开显示在最上面
foreach (Form childForm in this.MdiChildren)
{
if (childForm.Name == fInput)
{
childForm.Activate();
return;
}
}
//未打开
if (STAFF_JURISDICTION.Contains(strPrim.ToLower()))
{
string strClassName = fInput;
if (fInput.IndexOf(".") > 0)
strClassName = fInput;
Assembly assembly = Assembly.GetExecutingAssembly();
Type type = assembly.GetType(strClassName);
object obj = Activator.CreateInstance(type, null);
Form fm = (Form)obj;
//判断是否已经打开
fm.MdiParent = CommonControl.ActiveForm;
//样式设置
fm.StartPosition = FormStartPosition.CenterScreen;
if (fm.FormBorderStyle == FormBorderStyle.FixedToolWindow
|| fm.FormBorderStyle == FormBorderStyle.SizableToolWindow)
{
fm.FormBorderStyle = FormBorderStyle.FixedDialog;
fm.MaximizeBox = false;
}
if (fm.MaximizeBox == true)
fm.WindowState = FormWindowState.Maximized;
fm.Show();
}
else
{
MessageBox.Show("没有该项目操作权限,请联系相关管理人员赋权!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
/// 根据登录权限判断弹出窗口
/// </summary>
/// <param name="strPrim">权限名</param>
/// <param name="fInput">弹出窗口名</param>
private void ShowForm( string strPrim, string fInput)
{
//判断是否已经打开,如果打开显示在最上面
foreach (Form childForm in this.MdiChildren)
{
if (childForm.Name == fInput)
{
childForm.Activate();
return;
}
}
//未打开
if (STAFF_JURISDICTION.Contains(strPrim.ToLower()))
{
string strClassName = fInput;
if (fInput.IndexOf(".") > 0)
strClassName = fInput;
Assembly assembly = Assembly.GetExecutingAssembly();
Type type = assembly.GetType(strClassName);
object obj = Activator.CreateInstance(type, null);
Form fm = (Form)obj;
//判断是否已经打开
fm.MdiParent = CommonControl.ActiveForm;
//样式设置
fm.StartPosition = FormStartPosition.CenterScreen;
if (fm.FormBorderStyle == FormBorderStyle.FixedToolWindow
|| fm.FormBorderStyle == FormBorderStyle.SizableToolWindow)
{
fm.FormBorderStyle = FormBorderStyle.FixedDialog;
fm.MaximizeBox = false;
}
if (fm.MaximizeBox == true)
fm.WindowState = FormWindowState.Maximized;
fm.Show();
}
else
{
MessageBox.Show("没有该项目操作权限,请联系相关管理人员赋权!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
调用:
ShowForm(
"
A001
"
,
"
MySys_002
"
);
以上方法调用增加了权限的控制,因此大家要使用的话,需要做小的调整,怎么调就不用说了吧:)