一直以来,我都想看看别人家的工具栏重用(图1)到底是如何实现的,但在网上搜索了很久都没有找到过,即使找到一些程序,要么就是把这个工具栏写在具体的画面(图2),要么就是没有源代码的, 我在想,是否别人也有这个需求呢? 于是我决定把我自己的程序代码帖出来,一来可以分享给到需要的朋友,二来,希望有大侠经过,指点一二.

(图1)

(图2).

对于图2, 当然好实现了, 只是对当前画面的功能实现. 相当于单独画面的一个按钮而已.

现在,我们主要讲一下图1的实现方法.

在首先,在讲工具栏之前,讲一下菜单栏的实现(图3),在设计的时候,我们的菜单都是没有权限的. 通过在数据库里设定权限做了控制.

(图3)

首先在frmMain_Load的时候,加载下面的代码,这里主要是调用递归函数.

            MenuStrip ms = (MenuStrip)this.Controls["menuStrip1"];
ArrayList arr = new ArrayList();
dsright = C_BaseInfo.UserRight(frmLogin.C_UserInfo);
GetMenuAllName(arr, null, , ms);//调用递归函数

有一些每个用户都需要的菜单就直接置true了,如更改密码,报表工具等.(具体的报表权限还有地方控制了的.)

        private void GetMenuAllName(ArrayList arr, ToolStripMenuItem tsmi, int level, MenuStrip ms)
{
if (level == 0)//一级菜单
{
for (int i = 0; i < ms.Items.Count; i++)
{
ToolStripMenuItem item = (ToolStripMenuItem)ms.Items[i];
string Group = item.Text + ";" + level.ToString();//item.Text 菜单项的名字 level 此菜单的的级别
arr.Add(Group);
// item.Enabled = false;
GetMenuAllName(arr, item, level + 1, ms);
}
}
else//二级菜单
{
if (tsmi != null && tsmi.DropDownItems.Count > 0)
{
for (int i = 0; i < tsmi.DropDownItems.Count; i++)
{
if (tsmi.DropDownItems[i].GetType().ToString() != "System.Windows.Forms.ToolStripSeparator")
{
if (tsmi.Name != "mnuTools" && tsmi.Name != "mnuControl" && tsmi.Name != "mnuWindows" && tsmi.Name != "mnuSystem")
{ ToolStripMenuItem item = (ToolStripMenuItem)tsmi.DropDownItems[i];
string Group = item.Text + ";" + level.ToString();
arr.Add(Group);
//item.Enabled = false;
if (item.Name == "mnuChgPwd")
{
item.Enabled = true;
}
if (dsright.Tables[0].Rows.Count > 0)
{
for (int j = 0; j < dsright.Tables[0].Rows.Count; j++)
{ if (item.Name == dsright.Tables[0].Rows[j]["MenuName"].ToString())
{
item.Enabled = true;
}
//ToolStripMenuItem a = (ToolStripMenuItem)dsright.Tables[0].Rows[j]["MenuName"].ToString();
////a.Enabled = true;
//object a= (this.Controls.Find(dsright.Tables[0].Rows[j]["MenuName"].ToString(), true)[0]);
//ToolStripMenuItem b = (ToolStripMenuItem)a;
}
}
GetMenuAllName(arr, item, level + 1, ms);
}
}
}
}
}
}

首先看一下, 工具栏按钮的定义.

 //
// toolStripMenuItem32
//
this.toolStripMenuItem32.Image = ((System.Drawing.Image)(resources.GetObject("toolStripMenuItem32.Image")));
this.toolStripMenuItem32.Name = "toolStripMenuItem32";
this.toolStripMenuItem32.ShortcutKeys = System.Windows.Forms.Keys.F9;
this.toolStripMenuItem32.Size = new System.Drawing.Size(211, 22);
this.toolStripMenuItem32.Text = "查找";
this.toolStripMenuItem32.Click += new System.EventHandler(this.toolStripMenuItem32_Click);
//
// toolStripMenuItem33
//
this.toolStripMenuItem33.Name = "toolStripMenuItem33";
this.toolStripMenuItem33.ShortcutKeys = System.Windows.Forms.Keys.F5;
this.toolStripMenuItem33.Size = new System.Drawing.Size(211, 22);
this.toolStripMenuItem33.Text = "首笔";
this.toolStripMenuItem33.Click += new System.EventHandler(this.toolStripMenuItem33_Click);
//
// toolStripMenuItem34
//
this.toolStripMenuItem34.Name = "toolStripMenuItem34";
this.toolStripMenuItem34.ShortcutKeys = System.Windows.Forms.Keys.F6;
this.toolStripMenuItem34.Size = new System.Drawing.Size(211, 22);
this.toolStripMenuItem34.Text = "上一笔";
this.toolStripMenuItem34.Click += new System.EventHandler(this.toolStripMenuItem34_Click);
//
// toolStripMenuItem35
//
this.toolStripMenuItem35.Name = "toolStripMenuItem35";
this.toolStripMenuItem35.ShortcutKeys = System.Windows.Forms.Keys.F7;
this.toolStripMenuItem35.Size = new System.Drawing.Size(211, 22);
this.toolStripMenuItem35.Text = "下一笔";
this.toolStripMenuItem35.Click += new System.EventHandler(this.toolStripMenuItem35_Click);
//
// toolStripMenuItem36
//
this.toolStripMenuItem36.Name = "toolStripMenuItem36";
this.toolStripMenuItem36.ShortcutKeys = System.Windows.Forms.Keys.F8;
this.toolStripMenuItem36.Size = new System.Drawing.Size(211, 22);
this.toolStripMenuItem36.Text = "末笔";
this.toolStripMenuItem36.Click += new System.EventHandler(this.toolStripMenuItem36_Click);
//
// toolStripMenuItem37
//
this.toolStripMenuItem37.Name = "toolStripMenuItem37";
this.toolStripMenuItem37.ShortcutKeys = System.Windows.Forms.Keys.F11;
this.toolStripMenuItem37.Size = new System.Drawing.Size(211, 22);
this.toolStripMenuItem37.Text = "预备查询";
this.toolStripMenuItem37.Click += new System.EventHandler(this.toolStripMenuItem37_Click);
//
// toolStripMenuItem38
//
this.toolStripMenuItem38.Name = "toolStripMenuItem38";
this.toolStripMenuItem38.ShortcutKeys = System.Windows.Forms.Keys.F12;
this.toolStripMenuItem38.Size = new System.Drawing.Size(211, 22);
this.toolStripMenuItem38.Text = "新增记录";
this.toolStripMenuItem38.Click += new System.EventHandler(this.toolStripMenuItem38_Click);
//
// toolStripMenuItem39
//
this.toolStripMenuItem39.Image = ((System.Drawing.Image)(resources.GetObject("toolStripMenuItem39.Image")));
this.toolStripMenuItem39.Name = "toolStripMenuItem39";
this.toolStripMenuItem39.ShortcutKeys = System.Windows.Forms.Keys.F2;
this.toolStripMenuItem39.Size = new System.Drawing.Size(211, 22);
this.toolStripMenuItem39.Text = "保存";
this.toolStripMenuItem39.Click += new System.EventHandler(this.toolStripMenuItem39_Click);
//
// toolStripMenuItem40
//
this.toolStripMenuItem40.Image = ((System.Drawing.Image)(resources.GetObject("toolStripMenuItem40.Image")));
this.toolStripMenuItem40.Name = "toolStripMenuItem40";
this.toolStripMenuItem40.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.O)));
this.toolStripMenuItem40.Size = new System.Drawing.Size(211, 22);
this.toolStripMenuItem40.Text = "复制";
this.toolStripMenuItem40.Click += new System.EventHandler(this.toolStripMenuItem40_Click);
//
// toolStripMenuItem41
//
this.toolStripMenuItem41.Image = ((System.Drawing.Image)(resources.GetObject("toolStripMenuItem41.Image")));
this.toolStripMenuItem41.Name = "toolStripMenuItem41";
this.toolStripMenuItem41.ShortcutKeys = System.Windows.Forms.Keys.F4;
this.toolStripMenuItem41.Size = new System.Drawing.Size(211, 22);
this.toolStripMenuItem41.Text = "删除";
this.toolStripMenuItem41.Click += new System.EventHandler(this.toolStripMenuItem41_Click);
//
// toolStripMenuItem42
//
this.toolStripMenuItem42.Image = ((System.Drawing.Image)(resources.GetObject("toolStripMenuItem42.Image")));
this.toolStripMenuItem42.Name = "toolStripMenuItem42";
this.toolStripMenuItem42.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.P)));
this.toolStripMenuItem42.Size = new System.Drawing.Size(211, 22);
this.toolStripMenuItem42.Text = "打印";
this.toolStripMenuItem42.Click += new System.EventHandler(this.toolStripMenuItem42_Click);
//
// toolStripMenuItem43
//
this.toolStripMenuItem43.Image = ((System.Drawing.Image)(resources.GetObject("toolStripMenuItem43.Image")));
this.toolStripMenuItem43.Name = "toolStripMenuItem43";
this.toolStripMenuItem43.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Alt | System.Windows.Forms.Keys.Delete)));
this.toolStripMenuItem43.Size = new System.Drawing.Size(211, 22);
this.toolStripMenuItem43.Text = "关闭所有子窗口";
this.toolStripMenuItem43.Click += new System.EventHandler(this.toolStripMenuItem43_Click);

具体到某一个画面时,它们的新增,删除,修改的权限时又做了控制.

 for (int i = ; i < pParentWin.dsright.Tables[].Rows.Count; i++)
{
if (pParentWin.dsright.Tables[].Rows[i]["FormName"].ToString() == this.Name)
{ CanIns = Convert.ToBoolean(pParentWin.dsright.Tables[].Rows[i]["CanIns"]);
CanUpd = Convert.ToBoolean(pParentWin.dsright.Tables[].Rows[i]["CanUpd"]);
CanDel = Convert.ToBoolean(pParentWin.dsright.Tables[].Rows[i]["CanDel"]);
}
}
InitializeTools(); // 这里是初始化

// 初始化分为多种情况.

  private void InitializeTools(int Type)
{
if (Type == 0) //主窗体加载时,主菜单完全不可用
{
pParentWin.toolStrip1.Enabled = false;
ToolsStatue = 0; }
else if (Type == 1) //加载子窗体后或点预备查询后,查找 预备查询 新增 打印可用其余都不可用
{
pParentWin.toolStrip1.Enabled = true;
pParentWin.toolStripAddNew.Enabled = true;
pParentWin.toolStripPreSearch.Enabled = true;
pParentWin.toolStripSearch.Enabled = true;
pParentWin.toolStripPrint.Enabled = true; pParentWin.toolStripFirst.Enabled = false;
pParentWin.toolStripPre.Enabled = false;
pParentWin.toolStripNext.Enabled = false;
pParentWin.toolStripLast.Enabled = false;
pParentWin.toolStripSave.Enabled = false;
pParentWin.toolStripCopy.Enabled = false;
pParentWin.toolStripDelete.Enabled = false;
if (CanIns == false)
{
pParentWin.toolStripAddNew.Enabled = false; //新增按钮失效
} if (CanUpd == false)
{
pParentWin.toolStripSave.Enabled = false; //保存按钮失效
}
if (CanDel == false)
{
pParentWin.toolStripDelete.Enabled = false; //删除按钮失效
} ToolsStatue = 1; }
else if (Type == 2)//查询有结果后及保存后,所有工具条可用,如查询无结果,工具条状态为1
{
pParentWin.toolStrip1.Enabled = true;
pParentWin.toolStripAddNew.Enabled = true;
pParentWin.toolStripPreSearch.Enabled = true;
pParentWin.toolStripSearch.Enabled = true;
pParentWin.toolStripPrint.Enabled = true; pParentWin.toolStripFirst.Enabled = true;
pParentWin.toolStripPre.Enabled = true;
pParentWin.toolStripNext.Enabled = true;
pParentWin.toolStripLast.Enabled = true;
pParentWin.toolStripSave.Enabled = true;
pParentWin.toolStripCopy.Enabled = true;
pParentWin.toolStripDelete.Enabled = true;
if (CanIns == false)
{
pParentWin.toolStripAddNew.Enabled = false; //新增按钮失效
} if (CanUpd == false)
{
pParentWin.toolStripSave.Enabled = false; //保存按钮失效
}
if (CanDel == false)
{
pParentWin.toolStripDelete.Enabled = false; //删除按钮失效
}
ToolsStatue = 2;
}
else if (Type == 3)//点新增后,除新增不可用。其余工具条全部可用
{
pParentWin.toolStrip1.Enabled = true;
pParentWin.toolStripAddNew.Enabled = false;
pParentWin.toolStripPreSearch.Enabled = true;
pParentWin.toolStripSearch.Enabled = true;
pParentWin.toolStripPrint.Enabled = true; pParentWin.toolStripFirst.Enabled = true;
pParentWin.toolStripPre.Enabled = true;
pParentWin.toolStripNext.Enabled = true;
pParentWin.toolStripLast.Enabled = true;
pParentWin.toolStripSave.Enabled = true;
pParentWin.toolStripCopy.Enabled = true;
pParentWin.toolStripDelete.Enabled = true;
if (CanIns == false)
{
pParentWin.toolStripAddNew.Enabled = false; //新增按钮失效
} if (CanUpd == false)
{
pParentWin.toolStripSave.Enabled = false; //保存按钮失效
}
if (CanDel == false)
{
pParentWin.toolStripDelete.Enabled = false; //删除按钮失效
}
ToolsStatue = 3;
}
}

这里基本上就差不多了. 但如果打开多个画面之间,按钮是需要回到当时的状态的.

        private void frm_Activated(object sender, EventArgs e)
{
//自定义父窗口的工具栏
InitializeToolsEvent();
InitializeTools(ToolsStatue);
} private void frm_Deactivate(object sender, EventArgs e)
{
//自定义父窗口的工具栏
InitializeToolsEvent(); }

InitializeTools(ToolsStatue) 就是当时的状态了. 而InitializeToolsEvent就是注册和释放工具栏按钮了.

 private void InitializeToolsEvent(int Type)
{
if (Type == ) //得到焦点
{
pParentWin.toolStripSearch.Click += new EventHandler(this.Search);
pParentWin.toolStripPreSearch.Click += new EventHandler(this.PreSearch);
pParentWin.toolStripAddNew.Click += new EventHandler(this.AddNew);
pParentWin.toolStripSave.Click += new EventHandler(this.Save);
pParentWin.toolStripFirst.Click += new EventHandler(this.First);
pParentWin.toolStripPre.Click += new EventHandler(this.Pre);
pParentWin.toolStripNext.Click += new EventHandler(this.Next);
pParentWin.toolStripLast.Click += new EventHandler(this.Last);
pParentWin.toolStripDelete.Click += new EventHandler(this.Delete);
pParentWin.toolStripCopy.Click += new EventHandler(this.Copy);
pParentWin.toolStripPrint.Click += new EventHandler(this.Print);
}
else
{
pParentWin.toolStripSearch.Click -= new EventHandler(this.Search);
pParentWin.toolStripPreSearch.Click -= new EventHandler(this.PreSearch);
pParentWin.toolStripAddNew.Click -= new EventHandler(this.AddNew);
pParentWin.toolStripSave.Click -= new EventHandler(this.Save);
pParentWin.toolStripFirst.Click -= new EventHandler(this.First);
pParentWin.toolStripPre.Click -= new EventHandler(this.Pre);
pParentWin.toolStripNext.Click -= new EventHandler(this.Next);
pParentWin.toolStripLast.Click -= new EventHandler(this.Last);
pParentWin.toolStripDelete.Click -= new EventHandler(this.Delete);
pParentWin.toolStripCopy.Click -= new EventHandler(this.Copy);
pParentWin.toolStripPrint.Click -= new EventHandler(this.Print);
}
}

然后这里再对每一个功能进行重写就可以了.

想法很简单, 基本代码也有了. 希望对大家有用,如有不明白的地方,请留言, 有更好的实现方法也请指点一二.  本人不胜感激.

(原创作品)转载请注明出处...

  

浅析WINFORM工具条的重用实现的更多相关文章

  1. Winform开发中另一种样式的OutLookBar工具条

    很早的时候,曾经写了一篇随笔<WinForm界面开发之“OutLookBar”工具条>介绍了OutLookBar样式的工具条,得到很多同行的热烈反馈,我个人也比较喜欢这样的工具条布局,因此 ...

  2. 如何往IE工具条添加按钮(转载)

    如何往IE工具条添加按钮 问题提出:金山词霸.网络蚂蚁等软件安装后会向IE的工具条添加自己的按钮.按下按钮后还会作出相应的动作,这种功能是如何实现的呢?读完本文,您也可以将自己应用程序的按钮添加到IE ...

  3. Navisworks API 简单二次开发 (自定义工具条)

    在Navisworks软件运行的时候界面右侧有个工具条.比较方便.但是在二次开发的时候我不知道在Api那里调用.如果有网友知道请告诉我.谢谢. 我用就自己设置一个工具.界面比较丑!没有美工. 代码: ...

  4. arcengine中自定义工具和自带工具条(ICommand)点击后和其他工具使用的冲突

    自己系统中本身对于放大缩小等功能直接是单独重写的,但是如果在加一个工具条具有相同功能的话两者之间会有一些冲突,为解决该冲突可以重写工具条的OnItemClick事件 该工具条命名为axTool 我本身 ...

  5. 【代码笔记】iOS-页面调的时候隐藏工具条

    代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. se ...

  6. iOS开发小技巧--微博项目中的键盘工具条

    微博项目中的键盘工具条 项目中的键盘工具条不能使用inputAccessoryView,因为inputAccessoryView不能实现键盘隐藏的时候,工具条还显示在眼前,如图: 所以,果断决定将工具 ...

  7. 为Autodesk Viewer添加自定义工具条的更好方法

    上一篇文章中我介绍了使用Autodesk Viewer的UI API来给viewer添加自定义工具条的方法,看起来很简单是吧.不过有个问题,就是关于自定义工具条的信息(包括按钮的文本.图标.样式.ca ...

  8. 为Autodesk Viewer添加自定义工具条

    如果你参加过我们近期的活动,你就会频繁的听到我们现在正在做的Autodesk Viewer大模型浏览器,这是一个不需要下载任何插件,基于WebGL技术的浏览器,可以支持几十种数据格式.同时viewer ...

  9. QT学习之路--菜单、工具条、状态栏

    下面一些是 Menu Bar,用于显示菜单;再下面一点事 Toolbar areas,用于显示工具条,Status Bar,就是状态栏. Qt 提供了一个 QStatusBar 类来实现状态栏. Qt ...

随机推荐

  1. 线程----BlockingQueue (转)

    t java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.ut ...

  2. The Network Adapter could not establish the connection解决办法

    用 oracle net manager 将监听改为IP地址,将服务命名也改为IP地址,然后数据库连接改为IP地址方式不要用localhost

  3. MongoDB Auto-Sharding(自动分片)入门介绍

    MongoDB是10gen团队开发的一款面向文档的NoSQL数据库.最近一年多以来,MongoDB被越来越多的大型网站应用到生产环境中,比较著名的有Foursquare, bit.ly, Source ...

  4. Net判断一个对象是否为数值类型 z

    http://www.cnblogs.com/SkyD/p/4053461.html public static bool IsNumeric(this Type dataType) { if (da ...

  5. 代理抓取RSS信息

    最近工作很闲,就自己写了一个可以看RSS订阅的网站.话说,RSS阅读器到处都是,随便下一个就可以了,为什么还去做一个网站形式的呢?作为一个热(xian)爱(de)前(dan)端(teng)的程序员,我 ...

  6. opencv3.0 在 android 上的使用

    下载 OpenCV-3.0.0-android-sdk-1.zip 打开 intellj,新建立一个 android 工程后选择工程属性,导入模块(Import module from externa ...

  7. ansible官方文档翻译之变量

    Ansible变量 在使用ansible变量的时候,主要是因为各个系统的不同,从而需要使用不同的变量来进行设置,例如在设置一些配置文件的时候,有大部分内容是相同的,但是一部分内容是和主机的ip地址或者 ...

  8. Package inputenc Error: Unicode char \u8: not set up for use with LaTeX.

    用TexStudio编辑文档时,不知是多加了空格还是啥,总是提示如下错误: Package inputenc Error: Unicode char \u8: not set up for use w ...

  9. mysql 经典题目

    题目1:实现如下效果 CREATE TABLE IF NOT EXISTS tb_amount( `Id` INT NOT NULL AUTO_INCREMENT, `), `), `Amount` ...

  10. C# int.Parse()、int.TryParse()与Convert.ToInt32()的区别

    1.(int)是一种类型转换:当我们觟nt类型到long,float,double,decimal类型,可以使用隐式转换,但是当我们从long类型到int类型就需要使用显式转换,否则会产生编译错误. ...