大家请顶,如何动态加载form窗体?
已知窗体名叫:
"Form1"
"Form2"
"Form3"
string form2 = "Form2";
string from3 = "Form3";
Form名称将是字符串从数据库出来的
如何动态加载阿?
最终的目的是用于数据库记录窗体名称,动态加载
谢谢
已知窗体名叫:
"Form1"
"Form2"
"Form3"
string form2 = "Form2";
string from3 = "Form3";
Form名称将是字符串从数据库出来的
如何动态加载阿?
最终的目的是用于数据库记录窗体名称,动态加载
谢谢
第2种方法:先new然后放入hashTable,调用时候直接从hashTable里面取当然也有把这两种方法结合在一起滴做法,先查hashTable,如果有直接取出来,如果没有反射调用并存入hashTable同时也可以考虑有简单工厂类完成
switch(str){
case "form1":
frm = new form1();
break;
case "form2":
...
default:
frm = new formN();
break;
}
frm.show();
你必须知道程序集名和你窗体类的全名LoadForm("WindowsApplication1","WindowsApplication1.Form2").Show(); private Form LoadForm(string p_AssemblyName, string p_TypeForm)
{
Assembly _Assembly = Assembly.Load(p_AssemblyName);
Type _FormType = _Assembly.GetType(p_TypeForm);
object _LoadForm = _FormType.InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null);
return (System.Windows.Forms.Form)_LoadForm;
}
<Item>
<ModuleNo>10</ModuleNo>
<Type>1</Type>
<Name>tsmiBusiness</Name>
<Text>业务控制(&B)</Text>
<Width>65</Width>
<Height>20</Height>
<Order>1</Order>
<Level>0</Level>
<Group>0</Group>
<IsDisplayed>true</IsDisplayed>
</Item>
<Item>
<ModuleNo>1000</ModuleNo>
<Type>1</Type>
<Name>tsmiFrmInput</Name>
<Text>录入信息(&E)</Text>
<ClassName>ICCardManager.FrmInput</ClassName>
<Width>152</Width>
<Height>22</Height>
<ShortKey>F2</ShortKey>
<Order>1</Order>
<Level>1</Level>
<Group>0</Group>
<FatherNo>10</FatherNo>
<IsDisplayed>true</IsDisplayed>
</Item>
<Item>
<ModuleNo>100000</ModuleNo>
<Type>0</Type>
<Name>btnSL</Name>
<Text>提交受理(&S)</Text>
<Order>1</Order>
<Level>2</Level>
<Group>0</Group>
<FatherNo>1000</FatherNo>
<IsDisplayed>true</IsDisplayed>
</Item>
<Item>
<ModuleNo>1001</ModuleNo>
<Type>1</Type>
<Name>tsmiFrmCardOper</Name>
<Text>证卡处理(&C)</Text>
<ClassName>ICCardManager.FrmCardOper</ClassName>
<Width>152</Width>
<Height>22</Height>
<ShortKey>F3</ShortKey>
<Order>2</Order>
<Level>1</Level>
<Group>0</Group>
<FatherNo>10</FatherNo>
<IsDisplayed>true</IsDisplayed>
</Item>
...
</Menu>
//加载权限到MenuStrip
public static void FillMenus(ToolStripItemCollection tsic, DataTable dt, string parentid, byte[] b,EventHandler handler)
{
if (b == null ||b.Length == 0)
{
return;
}
DataView dv = new DataView(dt);
dv.RowFilter = string.IsNullOrEmpty(parentid) ? "FatherNo is null or FatherNo='' or FatherNo='-1'" : "FatherNo='" + parentid + "'";
dv.Sort = "Order";
for (int i = 0; i < dv.Count; i++)
{
string type = dv[i]["Type"].ToString();
if (int.Parse(type) == 0)
{
continue;//页面内的权限按钮
}
//加载权限
int l = Convert.ToInt32(dv[i]["Level"]);//从0开始
int g = Convert.ToInt32(dv[i]["Group"]);//从0开始
int p = GetPosisionInByteArray(l, g, i);
int ib = p / 8;//byte数
int io = p % 8; //在byte内的偏移位
bool flag = (b[ib] & (int)Math.Pow(2, io)) != 0;//与位运算
if (l == 0 && !flag)
{
continue;//0级菜单可用则添加
}
string name = dv[i]["Name"].ToString();
string txt = dv[i]["Text"].ToString();
int height = Convert.ToInt32(dv[i]["Height"]);
int width = Convert.ToInt32(dv[i]["Width"]);
string className = dv[i]["ClassName"].ToString();
string shortKey = dv[i]["ShortKey"].ToString();
ToolStripMenuItem tsmi = new ToolStripMenuItem();//Type类型相关,还可定义其他类型
tsmi.Name = name;
tsmi.Text = txt;
tsmi.Size = new Size(width, height);
tsmi.Tag = className;
tsmi.ShortcutKeys = string.IsNullOrEmpty(shortKey) ? Keys.None: (Keys)Enum.Parse(typeof(Keys), shortKey, true);
//tsmi.Image = string.IsNullOrEmpty(dv[i]["Image"].ToString()) ? null : CommonUtility.ImageFromByteArray(dv[i]["Image"] as byte[]);
tsmi.Image = Menu.ResourceManager.GetObject(name) as Image;
tsmi.Enabled = flag;
tsmi.Click += handler;//OnClick事件,对所有项目都进行了添加,(其实应该只对叶子节点添加,但这里不好判断)
if (!string.IsNullOrEmpty(dv[i]["HasSeparator"].ToString()) && Convert.ToBoolean(dv[i]["HasSeparator"]))
{
ToolStripSeparator tss = new ToolStripSeparator();
tss.Name = tsmi.Name + "Separator";
tss.Size = new Size(tsmi.Size.Width, 6);
tsic.Add(tss);
}
tsic.Add(tsmi);
FillMenus(tsmi.DropDownItems, dt, dv[i]["ModuleNo"].ToString(), b, handler);
}
}
当前了。上面有对页面权限的处理
页面中:
string className = tsmi.Tag.ToString();
string formName = className.Substring(className.LastIndexOf(".") + 1);
if (!CheckExistsForm(formName))
{
string assemblyName = className.Substring(0, className.IndexOf("."));
Form f = (Form)System.Reflection.Assembly.Load(assemblyName).CreateInstance(className);
if (f != null)
{
f.MdiParent = this;
f.WindowState = FormWindowState.Maximized;
f.Show();
}
}