一直以来,我都想看看别人家的工具栏重用(图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. 部署K2 Blackpearl流程时出错(由于目标计算机积极拒绝,无法连接)

    转:http://www.cnblogs.com/dannyli/archive/2011/12/01/2270118.html 亲,如果你也遇到过这个问题,就请继续往下看哦 在部署K2 Blackp ...

  2. ASIFormDataRequest 登录

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL: [NSURL URLWithString: @"http: ...

  3. linearlayout 水平垂直居中

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...

  4. 嵌入式 hi3518平台uboot引导nfs文件系统

    首先贴出来我的bootargs的设置(注没有换行符!!!): setenv bootargs noinitrd mem=64M root=/dev/nfs init=/linuxrc rw nfsro ...

  5. SmartGit STUDY

    Git Concepts This section helps you to get started with Git and gives you an understanding of the fu ...

  6. Ubuntu 安装

    最近又有工作需要,又需要在虚拟机上工作了.记得上次使用Ubuntu的时候还是7,8年前呢 用的是vmware 7 ,buntu的版本记不清了.时隔多年又捡起来了,记忆还停留在过去,于是被折腾惨了. 1 ...

  7. 使用Firebug和FirePHP调试PHP

    大家都知道Firebug,可能不知大FirePHP,它也是FireFox插件用来调试PHP的,首先确保你安装了Firebug,然后再去安装FirePHP,这是你会看到Firebug多了一只蓝色的虫: ...

  8. 用ASP.Net写一个发送ICQ信息的程序

    用ASP.Net写一个发送ICQ信息的程序 这里我给大家提供一个很实用的例子,就是在线发送ICQ信息.想一想我们在网页上直接给朋友发送ICQ信息,那是多么美妙的事情啊.呵呵,在吹牛啊,其实ICQ本来就 ...

  9. (转载)OC学习篇之---类的延展

    前一篇文章我们介绍了类的类目概念和使用,那么这篇文章我们继续来介绍一下OC中的一个特性:延展. 其实说白了,延展就是弥补C语言中的前向申明,我们知道,在C语言中,如果你想调用一个函数的话,那么在此之前 ...

  10. 随手记录一个 firefox的backgroundPostion-x和-y的问题

    今天帮大师写了一天项目,后来在测试一个显示升序和降序的标签上面,我使用了一个backgroundPosition-y来判断当前icon的状态,却无法不管是使用闭包还是个钟手段,在 firefox下面总 ...