2
Answers

Using Reflection.Assembly to Load Forms from a Table c#

mr imam

mr imam

10y
701
1

I have in my project mdiparent and some childs form, in this Mdiparent ihave create treeview and populate it from data base(works fine) .. all name of childforms are in database tableName"Forms and FormsGrp"
how i can call this child forms(by its name) on MouseDoubleClick on tree node using "Reflection", other way using if and looping for "formsname"s solve that but idont want this looping? i Generate this code but somthing error or missing in calling form! Get Message NullReferenceException!!

public partial class frmMainSys : Form {     frmDoctors Doctors;     (and all forms name here)     DataTable dt;     SqlDataReader dr;     TreeNode parentNode = null;      public frmMain()     {          InitializeComponent();         this.Tree.NodeMouseDoubleClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.Tree_MouseDoubleClick);      }      private void frmMainSys_Load(object sender, EventArgs e)     {         DataTable dt;         //// ************ load Tree***************//         DBAccess db = new DBAccess();         dt = db.ExcuteDataTable("SELECT ID,EngName,Rank FROM FormsGrp ");         foreach (DataRow dr in dt.Rows)         {             parentNode = Tree.Nodes.Add(dr["EngName"].ToString());             Populate(Convert.ToInt32(dr["ID"].ToString()), parentNode);             Tree.Parent.Font = new System.Drawing.Font("tahoma", 8, FontStyle.Bold);             Tree.ImageList = imgList;             // Set the TreeView control's default image and selected image indexes.             Tree.ImageIndex = 1;             Tree.SelectedImageIndex = 0;          }      }      //// ************ populate TreeView***************//      private void Populate(int parentId, TreeNode parentNode)     {          DBAccess db = new DBAccess();         DataTable dt;         dt = db.ExcuteDataTable("SELECT Frm_Code,frm_Name,FROM Forms WHERE Level=" + parentId + "");         foreach (DataRow ds in dt.Rows)         {              if (parentNode == null)             {                 parentNode.Tag = Tree.Nodes.Add(ds["Frm_Code"].ToString());                 parentNode = Tree.Nodes.Add(ds["frm_Name"].ToString());                 parentNode = Tree.Nodes.Add(ds["Frm_Code"].ToString());                 parentNode.Text = dr["EngName"].ToString();                 Tree.Nodes.Add(parentNode);               }              else             {                 //parentNode = Tree.Nodes.Add(ds["Frm_Code"].ToString());                 parentNode = Tree.Nodes.Add(ds["frm_Name"].ToString());                 parentNode = parentNode.Nodes.Add(ds["frm_Name"].ToString());                 //parentNode.Text = dr["frm_Name"].ToString();             }          }      }      private void Tree_MouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)     {         //MessageBox.Show(e.Node.Text);         Assembly ass = Assembly.GetExecutingAssembly();         Form frm = (Form)ass.CreateInstance(Application.Product+ "." + e.Node.Text);         if (frm == null)         {              frm.MdiParent = this;             frm.Show();            }
Answers (2)