C # .NET equivalent of Java Swing Actions

While programming the Java GUI, I heavily used the Action class. An equivalent action class was passed to several element or menu element constructors, so you only had to encode the logic in one place.

Each time you clicked a button / icon / menu item associated with an action, the actionPerformed method started and executed the code.

It was a great time saver and allowed me to write logic only once.

Questions:

  • Is there a similar class in C # or .NET framework?
  • Is it really wrong for me, and is there another way to have one set of logics called from several buttons / icons / menu items?
+4
source share
2 answers

.Net makes heavy use of events, and you can do something similar if you have common functionality.

protected void buton_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; 
+4
source

C # typically uses events to map behavior to user actions. You can use one event handler to handle a click with several buttons or menu items.

BTW, C # is a language, not a graphical interface. There are several graphical interfaces that can be used with C # (Windows Forms, WPF, Silverlight, ASP.NET), and each of them is different. Therefore, your question is not related to C #, but rather to one of these frameworks.

+1
source

Source: https://habr.com/ru/post/1369004/


All Articles