查找所有页面中的按钮

 protected void InitButtons()
        {
            string strSql = "";
            strSql = @"select MFormName  from MenuInfo where SeqNo=@SeqNo";
            DbCommand cmd12 = myGlobal.db.GetSqlStringCommand(strSql);
            myGlobal.db.AddInParameter(cmd12, "@SeqNo", DbType.Int32, 1);

            menuInfo = new List<MenuInfo>();
            try
            {
                using (IDataReader reader = myGlobal.db.ExecuteReader(cmd12))
                {
                    while (reader.Read())
                    {
                        menuInfo.Add(new MenuInfo()
                        {
                            MFormName = reader["MFormName"].ToString()
                        });
                    }
                }
            }
            catch
            {

            }


            if (menuInfo != null && menuInfo.Count > 0)
            {
                IList<ButtonInfo> buttList = new List<ButtonInfo>();

                foreach (MenuInfo m in menuInfo)
                {
                    BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic;
                    Assembly asm = Assembly.Load("mytext");
                    Form frm = (Form)asm.CreateInstance(m.MFormName);
                    //frmMeA frm = new frmMeA();
                    FieldInfo[] fields = frm.GetType().GetFields(flag);


                    foreach (var fi in fields)
                    {
                        if (fi.FieldType == typeof(Button)
                       || fi.FieldType == typeof(SimpleButton))
                        {
                            Control c = fi.GetValue(frm) as Control;

                            ButtonInfo bModel = new ButtonInfo();
                            bModel.ButtonType = 1;
                            bModel.ButtonTitle = c.Name;
                            bModel.ButtonPop = c.Text;
                            buttList.Add(bModel);
                        }
                        if (fi.FieldType == typeof(BarButtonItem))
                        {
                            BarButtonItem c = fi.GetValue(frm) as BarButtonItem;
                            if (c != null)
                            {


                                ButtonInfo bModel = new ButtonInfo();
                                bModel.ButtonType = 1;
                                bModel.ButtonTitle = c.Name;
                                bModel.ButtonPop = c.Caption;
                                buttList.Add(bModel);
                            }
                        }

                        if (fi.FieldType == typeof(ToolStripMenuItem))
                        {
                            ToolStripMenuItem c = fi.GetValue(frm) as ToolStripMenuItem;
                            if (c != null)
                            {

                                ButtonInfo bModel = new ButtonInfo();
                                bModel.ButtonType = 1;
                                bModel.ButtonTitle = c.Name;
                                bModel.ButtonPop = c.Text;
                                buttList.Add(bModel);

                            }
                        }
                    }
                }
                var buttList1 = buttList;
            }
        }

  

posted @ 2013-11-27 11:04  晨风清流  阅读(320)  评论(0编辑  收藏  举报