C# .NET equivalent of Java Swing Actions
Whilst programming Java GUis I made heavy use of the Action class. The instantiated action class was passed to numerous button or menu item开发者_如何转开发 constructors so that you only had to code the logic in one place.
Each time you clicked on a button/icon/menuitem associated with the action the actionPerformed method would fire and execute the code.
This was a great time saver and allowed me to write the logic only once.
Questions:
- Is there a similar class in C# or .NET framework?
- Have I got this all wrong and there is a different way to have one set of logic called from multiple buttons/icons/menuitems?
.Net uses events heavily and you can do something like this if you have common functionality.
protected void button_click(object sender, EventArgs e)
{
// Common code here
// You can use sender parameter to distinguish b/w the buttons.
}
and
button1.Click += button_click;
button2.Click += button_click;
button3.Click += button_click;
C# usually uses events to associate behavior with user actions. You can use a single event handler to handle the click on several buttons or menu items.
BTW, C# is a language, not a GUI framework. There are several GUI frameworks that you can use with C# (Windows Forms, WPF, Silverlight, ASP.NET), and each one is different. So your question isn't really related to C#, but rather to one of these frameworks.
精彩评论