C#发送到buttonclicks同一目标 [英] C# Sending buttonclicks to same Object
问题描述
我在我的项目有很多的按钮。为了使程序更短我需要获得一个按钮,每点击相同的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屋!