C#发送到buttonclicks同一目标 [英] C# Sending buttonclicks to same Object

查看:140
本文介绍了C#发送到buttonclicks同一目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目有很多的按钮。为了使程序更短我需要获得一个按钮,每点击相同的Button_click的方法。这可能吗?



感谢


解决方案

是 - 这是完全可能的。

您不说,无论你的WinForms或WPF但基本的方法是创建的处理程序的私有方法,然后调用每个按钮的处理程序:

 私人无效ButtonHandler(一些参数)
{
}

私人无效OnButton1Click(对象发件人,EventArgs五)
{
ButtonHandler(一些参数);
}



不过,你可以订阅相同的处理每个按钮的点击事件:

  Button1.Click + = ButtonHandler; 
Button2.Click + = ButtonHandler;



或者将这些从设计师 - 只需选择从下拉列表中现有的方法,而不是创建。一个新的处理程序



通过WPF可以将每个按钮的点击很容易绑定到XAML相同的处理:

 <按钮X:NAME =Button1的点击=ButtonHandler... /> 
<按钮X:NAME =Button2的点击=ButtonHandler... />



同样的设计为您提供了选择现有的处理程序,以及创建一个新的选择。


I have a lot of buttons in my project. To make the program I shorter need to get every click of a button to the same "Button_click" method. Is this possible?

Thanks

解决方案

Yes - it's perfectly possible.

You don't say whether you're WinForms or WPF but the basic way is to create a private method that's the handler and then call that from each button handler:

private void ButtonHandler(some arguments)
{
}

private void OnButton1Click(object sender, EventArgs e)
{
    ButtonHandler(some arguments);
}

However, you can just subscribe the same handler to each button's click event:

Button1.Click += ButtonHandler;
Button2.Click += ButtonHandler;

Or set these from the designer - just pick the existing method from the drop-down list rather than creating a new handler.

With WPF you can quite easily bind each button click to the same handler in XAML:

<Button x:Name="Button1" Click="ButtonHandler" ... />
<Button x:Name="Button2" Click="ButtonHandler" ... />

Again the designer gives you the choice of selecting an existing handler as well as creating a new one.

这篇关于C#发送到buttonclicks同一目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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