DEV控件问题,如何判断窗口中是否含有barManager控件,并遍历barManager中所有项

Jiedy 2015-08-24 03:23:45
现在主要是第一步遇到问题了,遍历barManager中的项这个好办,就是不知道如何得到barManager,这个barManager不是继承自Control类,所以用普通的 foreach (Control con in Control.ControlCollection) 是获取不到的,
...全文
347 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
愿出一分力 2017-05-26
  • 打赏
  • 举报
回复
foreach (Component com in this.components.Components) { }
Jiedy 2015-08-27
  • 打赏
  • 举报
回复
引用 9 楼 xuanbg 的回复:
[quote=引用 8 楼 Jiedy91 的回复:] [quote=引用 7 楼 xuanbg 的回复:] 为什么要判断有没有?设计器添加了就是有,没有添加就是没有呀?我一般都把这个放在窗体基类里面,对工具栏的处理函数也写在基类里面。继承窗体调用一下相关方法就可以了。压根不需要判断有没有,有没有自己还不知道?
。。。。。。 我是想遍历程序集里面的所有按钮跟所有Barmanager里面的按钮来控制权限的啊。。 你这个想当然 我去[/quote] 我是这样控制权限的:
        /// <summary>
        /// 初始化模块工具栏
        /// </summary>
        protected void InitToolBar()
        {
            using (var cli = new CommonsClient(MainForm.Binding, MainForm.Address))
            {
                _Actions = cli.GetAction(UserSession, ModuleId);
            }

            barMainToolBar.BarName = ModuleId.ToString();
            foreach (DataRow row in _Actions.Rows)
            {
                var item = new BarButtonItem(barManager, row["Alias"].ToString())
                {
                    CategoryGuid = (Guid) row["ModuleId"],
                    Tag = row["Enable"],
                    Name = row["Name"].ToString(),
                    Glyph = Image.FromStream(new MemoryStream((byte[]) row["Icon"])),
                    PaintStyle = (bool) row["ShowText"] ? BarItemPaintStyle.CaptionGlyph : BarItemPaintStyle.Standard,
                    Enabled = (bool) row["Enable"],
                    Visibility = (bool) row["Validity"] ? BarItemVisibility.Always : BarItemVisibility.Never
                };
                item.ItemClick += item_ItemClick;

                barManager.Items.Add(item);
                barManager.Bars[ModuleId.ToString()].ItemLinks.Add(item, (bool)row["BeginGroup"]);
            }
        }
按钮是根据数据动态加入的,而不是事先拖上去。这样控制权限就很简单了。[/quote] 动态添加菜单这个我也有用到 动态隐藏窗口中按钮我也有用到(我的权限是精确到按钮的,而不仅仅是菜单)
software_artisan 2015-08-26
  • 打赏
  • 举报
回复
引用 8 楼 Jiedy91 的回复:
[quote=引用 7 楼 xuanbg 的回复:] 为什么要判断有没有?设计器添加了就是有,没有添加就是没有呀?我一般都把这个放在窗体基类里面,对工具栏的处理函数也写在基类里面。继承窗体调用一下相关方法就可以了。压根不需要判断有没有,有没有自己还不知道?
。。。。。。 我是想遍历程序集里面的所有按钮跟所有Barmanager里面的按钮来控制权限的啊。。 你这个想当然 我去[/quote] 我是这样控制权限的:
        /// <summary>
        /// 初始化模块工具栏
        /// </summary>
        protected void InitToolBar()
        {
            using (var cli = new CommonsClient(MainForm.Binding, MainForm.Address))
            {
                _Actions = cli.GetAction(UserSession, ModuleId);
            }

            barMainToolBar.BarName = ModuleId.ToString();
            foreach (DataRow row in _Actions.Rows)
            {
                var item = new BarButtonItem(barManager, row["Alias"].ToString())
                {
                    CategoryGuid = (Guid) row["ModuleId"],
                    Tag = row["Enable"],
                    Name = row["Name"].ToString(),
                    Glyph = Image.FromStream(new MemoryStream((byte[]) row["Icon"])),
                    PaintStyle = (bool) row["ShowText"] ? BarItemPaintStyle.CaptionGlyph : BarItemPaintStyle.Standard,
                    Enabled = (bool) row["Enable"],
                    Visibility = (bool) row["Validity"] ? BarItemVisibility.Always : BarItemVisibility.Never
                };
                item.ItemClick += item_ItemClick;

                barManager.Items.Add(item);
                barManager.Bars[ModuleId.ToString()].ItemLinks.Add(item, (bool)row["BeginGroup"]);
            }
        }
按钮是根据数据动态加入的,而不是事先拖上去。这样控制权限就很简单了。
Jiedy 2015-08-25
  • 打赏
  • 举报
回复
引用 7 楼 xuanbg 的回复:
为什么要判断有没有?设计器添加了就是有,没有添加就是没有呀?我一般都把这个放在窗体基类里面,对工具栏的处理函数也写在基类里面。继承窗体调用一下相关方法就可以了。压根不需要判断有没有,有没有自己还不知道?
。。。。。。 我是想遍历程序集里面的所有按钮跟所有Barmanager里面的按钮来控制权限的啊。。 你这个想当然 我去
software_artisan 2015-08-25
  • 打赏
  • 举报
回复
为什么要判断有没有?设计器添加了就是有,没有添加就是没有呀?我一般都把这个放在窗体基类里面,对工具栏的处理函数也写在基类里面。继承窗体调用一下相关方法就可以了。压根不需要判断有没有,有没有自己还不知道?
Jiedy 2015-08-24
  • 打赏
  • 举报
回复
引用 3 楼 Z65443344 的回复:
不能遍历components吗
求帮忙啊! 我通过调试的方法可以看得到barmanager的 也能直接看得到barmanager中的按钮,但是我怎么通过代码获得呢。。 怎么个反射法
Jiedy 2015-08-24
  • 打赏
  • 举报
回复
引用 1 楼 phommy 的回复:
如果是通过设计器添加的,窗体的components中会有BarManager的注册。如果是在窗体外部访问只好反射拿到components了
是通过设计器添加的 但是一般的控件都会Design代码里面都会有 this.Controls.Add(...),这个barManager就不是这样添加的,你可以试试看,这个是我的代码:

                Assembly asb = Assembly.Load("XXX");
                Type[] types = asb.GetExportedTypes();
                foreach (Type type in types)
                {
                    if (!new[] { "System.Windows.Forms.Form", "DevExpress.XtraEditors.XtraForm" }.Contains(type.BaseType.FullName) || type.ContainsGenericParameters) continue;
                    Form frm = asb.CreateInstance(type.FullName) as Form;

                    List<string> btnList = new List<string>();
                    GetbtnList(frm, frm.Controls,btnList);
                }


public void GetbtnList(object obj, Control.ControlCollection ctc,List<string> rst)
        {
            foreach (Control con in ctc)
            {
                if (!con.HasChildren)
                {
                    Control c1 = con as DevExpress.XtraEditors.SimpleButton;
                    if (c1 != null)
                        rst.Add(c1.Text);
                    if (con.GetType().ToString() == "DevExpress.XtraBars.BarManager")
                    {
                        string xx = "";//////////////////////////////////////////////////////永远都不会执行这段话!
                    }
                }
                else
                    GetbtnList(obj, con.Controls,rst);
            }
        }
Jiedy 2015-08-24
  • 打赏
  • 举报
回复
引用 3 楼 Z65443344 的回复:
不能遍历components吗
不行啊 递归也不行的
於黾 2015-08-24
  • 打赏
  • 举报
回复
遍历窗体控件,判断控件类型呗 不过如果你将控件又丢进了panel之类的容器里,那么就比较麻烦了,得用递归
於黾 2015-08-24
  • 打赏
  • 举报
回复
不能遍历components吗
phommy 2015-08-24
  • 打赏
  • 举报
回复
如果是通过设计器添加的,窗体的components中会有BarManager的注册。如果是在窗体外部访问只好反射拿到components了

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧