如何在工具条中为每个工具条按钮编写常见事件 [英] How do I write common event handeling foe each toolstripbutton in toolstrip

查看:80
本文介绍了如何在工具条中为每个工具条按钮编写常见事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void toolStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
       {
           string clickedtext = e.ClickedItem.Name;
           if (clickedtext.Contains("saveToolStripButton"))
           {
               MessageBox.Show(clickedtext);
           }

       }





我试图按上述方法解决,但我想要更高级的版本处理工具条中的每个工具条按钮如果可能的话,可以在menustrip中使用相同的事件



我尝试过:



ToolStripItemClickedEventArgs e)

{

string clickedtext = e.ClickedItem.Name;

if(clickedtext.Contains (saveToolStripButton))

{

MessageBox.Show(clickedtext);

}



}



我试图按上述方法解决,但我想要更高级的版本来处理工具条中的每个工具条,如果可能的话,可以在menustrip中使用相同的事件



I tried to solve as above but I want more advanced version to handle each toolstripbutton in toolstrip if possible same event can be used in menustrip

What I have tried:

ToolStripItemClickedEventArgs e)
{
string clickedtext = e.ClickedItem.Name;
if (clickedtext.Contains("saveToolStripButton"))
{
MessageBox.Show(clickedtext);
}

}

I tried to solve as above but I want more advanced version to handle each toolstripbutton in toolstrip if possible same event can be used in menustrip

推荐答案

如果你使用相同的方法来处理一堆按钮 - 在你的例子中是保存(并且可能是另存为,打开等等)喜欢) - 然后你需要任何一个d代码区分它们并执行适当的操作(这是杂​​乱的)或代码在ToolStripButton.Tag属性中设置一个Delegate方法并调用它 - 当你稍后再改变它时这是不明显的。

我会使用单独的处理程序方法来调用单个方法来执行请求操作,而不是采用任何方法并将它们组合在一个#region中,以便它们可以在编辑器中折叠。

但是......如果你必须有一个处理程序......

创建一个委托:

If you use the same method to handle a bunch of buttons - in your example the "Save" (and presumably "Save As", "Open", and such like) - then you need either need code to distinguish between them and perform the appropriate operation (which is messy) or code to set up a Delegate method in ToolStripButton.Tag property and call that - which isn't obvious when you come back to change it later.
I'd go with separate handler methods which call a single method to perform the request operation rather than either approach and group them together in a #region so they can be "collapsed" in the editor.
But...If you must have a single handler...
Create a delegate:
private delegate void ToolStripButtonMethod();



设置项目点击方法以使用它:


Set up your item clicked method to use it:

private void myToolStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
    ToolStripButton tsb = e.ClickedItem as ToolStripButton;
    if (tsb != null)
        {
        ToolStripButtonMethod d = tsb.Tag as ToolStripButtonMethod;
        if (d != null)
            {
            d();
            }
        }
    }



然后创建执行按钮操作的方法:


Then create the methods to perform the button operations:

private void DoTheSave() { Console.WriteLine("Save"); }
private void DoTheSaveAs() { Console.WriteLine("Save As"); }



最后将它们添加到按钮:


And finally add them to your buttons:

private void LoadEm()
    {
    tsbSave.Tag = new ToolStripButtonMethod(DoTheSave);
    tsbSaveAs.Tag = new ToolStripButtonMethod(DoTheSaveAs);
    }



凌乱?是。明显?没有。工作?是的......但是你必须记得在更换工具条时添加每个新按钮。

我?我将使用单独的ToolStripButton.Click处理程序...


Messy? Yes. Obvious? No. Works? Yes...but you will have to remember to add each new button when you change your toolstrip.
Me? I'd use the individual ToolStripButton.Click handlers...


这篇关于如何在工具条中为每个工具条按钮编写常见事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆