Create Database driven MenuBar

***************************************************************************
Create Database Table
(1) Menu

SR int Not Null
MENU varchar(50) Not Null
MENUPROMPT varchar(50) Null
-------------------------------------------------------------------------------------
(2)Menu Item

SR int Not Null
ITEM varchar(50) Not Null
ITEMPROMPT varchar(50) Not Null
MENU varchar(50) Not Null
FORM varchar(50) Null
ICONPATH varchar(50) Null *****************************************************************************

Write this code
****************************************************************************

private void frmMAIN_Load(object sender, EventArgs e)
{
    try
    {
        timer1.Start();
        Flag = 0;
        frmBG ObjBg = new frmBG();
        ObjBg.MdiParent = ObjMainTemp;
        ObjBg.Show();
        DataTable dt = ObjGn.FillData("SELECT * FROM MENU ORDER BY SR");
        for (int i = 0; i < objmenu =" new" forecolor =" Color.White;" dropmenu =" ObjGn.FillData(" menu="'" j =" 0;" objsubmenu =" new" name =" DropMenu.Rows[j][" image =" Image.FromFile(DropMenu.Rows[j]["> 2)
        {
            Flag = 0;
        }
        Flag++;
        if (Flag == 2)
        {
            DataTable dt = ObjGn.FillData("SELECT * FROM MENUITEM WHERE ITEM='" + e.ClickedItem.Name + "'");
            CSharpCodeProvider c = new CSharpCodeProvider();
            ICodeCompiler icc = c.CreateCompiler();
            CompilerParameters cp = new CompilerParameters();
            cp.ReferencedAssemblies.Add("system.dll");
            cp.ReferencedAssemblies.Add("system.xml.dll");
            cp.ReferencedAssemblies.Add("system.data.dll");
            cp.ReferencedAssemblies.Add("system.windows.forms.dll");
            cp.ReferencedAssemblies.Add("system.drawing.dll");
            cp.ReferencedAssemblies.Add("WindowApplicationMenuTest.exe");
            cp.CompilerOptions = "/t:library";
            cp.GenerateInMemory = true;
            StringBuilder sb = new StringBuilder("");
            sb.Append("using System;\r\n");
            sb.Append("using System.Xml;\r\n");
            sb.Append("using System.Data;\r\n");
            sb.Append("using System.Data.SqlClient;\r\n");
            sb.Append("using System.Windows.Forms;\r\n");
            sb.Append("using System.Drawing;\r\n");
            sb.Append("using WindowApplicationMenuTest; \r\n");
            sb.Append("namespace WindowApplicationMenuTest{ \r\n");
            sb.Append("public class CSCodeEvaler{ \r\n");
            sb.Append("public object EvalCode(){ \r\n");
            sb.Append(dt.Rows[0]["FORM"].ToString() + " ObjTemp = new " + dt.Rows[0]["FORM"].ToString() + "();");
            sb.Append("ObjTemp.MdiParent = frmMAIN.ObjMainTemp; \r\n");
            sb.Append("ObjTemp.Show();\r\n");
            sb.Append("ObjTemp.WindowState = FormWindowState.Maximized;\r\n");
            sb.Append("ObjTemp.Title.Text = \"" + dt.Rows[0]["ITEMPROMPT"].ToString() + "\";\r\n");
            sb.Append(" try{");
            sb.Append("ObjTemp.TempPic.Image = Image.FromFile(\"" + dt.Rows[0]["ICONPATH"].ToString() + "\");");
            sb.Append("return ObjTemp; \r\n}");
            sb.Append("catch { return null; }");
            sb.Append("} \r\n");
            sb.Append("} \r\n");
            sb.Append("} \r\n");
            CompilerResults cr = icc.CompileAssemblyFromSource(cp, sb.ToString());
            if (cr.Errors.Count > 0)
            {
                MessageBox.Show("ERROR: " + cr.Errors[0].ErrorText, " Error evaluating cs code", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            System.Reflection.Assembly a = cr.CompiledAssembly;
            object o = a.CreateInstance("WindowApplicationMenuTest.CSCodeEvaler");
            Type t = o.GetType();
            MethodInfo mi = t.GetMethod("EvalCode");
            object s = mi.Invoke(o, null);
            Flag = 0;
        }
    }catch(Exception ex){}
}

No comments: